stdlib icon indicating copy to clipboard operation
stdlib copied to clipboard

Feature request: `set.toggle(...)`

Open Norlock opened this issue 9 months ago • 1 comments

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)
  }
}

Norlock avatar May 19 '25 04:05 Norlock

Could you share your use cases, alternatives options, and prior art in other languages please 🙏

lpil avatar May 19 '25 11:05 lpil

@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}
}

abs0luty avatar Oct 28 '25 09:10 abs0luty

Going to close this as we don't have a use case yet. Thank you folks

lpil avatar Oct 28 '25 09:10 lpil