stdlib
stdlib copied to clipboard
Feature request: `set.toggle(...)`
Maybe it would be nice to have QoL function to toggle the key:
pub fn toggle(set: set.Set(a), key: a) {
case set.contains(set, key) {
True -> set.delete(set, key)
False -> set.insert(set, key)
}
}
Could you share your use cases, alternatives options, and prior art in other languages please 🙏
@Norlock I think in most scenarios you can just use symmetric difference, no? I don't see a reason why we need to add a new utility function:
import gleam/set
pub fn main() {
let my_set = set.from_list([1, 2, 3])
echo my_set |> set.symmetric_difference(set.from_list([4])) // {1, 2, 3, 4}
echo my_set |> set.symmetric_difference(set.from_list([3])) // {1, 2}
}
Going to close this as we don't have a use case yet. Thank you folks