aboutsummaryrefslogtreecommitdiff
path: root/aoc-2020-gleam/src/ext/setx.gleam
blob: 4a9c0c17e26e88d198132baab87228f2b0705b9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import gleam/list
import gleam/set.{type Set}
import gleam/iterator as iter
import ext/iteratorx as iterx

pub fn count(s: Set(a), satisfying predicate: fn(a) -> Bool) -> Int {
  s
  |> set.to_list
  |> iter.from_list
  |> iterx.count(satisfying: predicate)
}

pub fn map(s: Set(a), with fun: fn(a) -> b) -> Set(b) {
  s
  |> set.to_list
  |> list.map(with: fun)
  |> set.from_list
}

pub fn flat_map(s: Set(a), with fun: fn(a) -> Set(b)) -> Set(b) {
  s
  |> set.to_list
  |> list.map(with: fun)
  |> list.fold(from: set.new(), with: set.union)
}

pub fn toggle(in s: Set(a), this value: a) -> Set(a) {
  s
  |> case set.contains(in: s, this: value) {
    True -> set.delete(from: _, this: value)
    False -> set.insert(into: _, this: value)
  }
}