aboutsummaryrefslogtreecommitdiff
path: root/aoc-2020-gleam/src/ext/setx.gleam
blob: f2c67e87b946c6e30c70b9a12db451343f846e7e (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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import gleam/list
import gleam/set.{type Set}
import gleam/iterator as iter
import ext/resultx as resx
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 arbitrary_union(of sets: List(Set(a))) -> Set(a) {
  list.fold(over: sets, from: set.new(), with: set.union)
}

pub fn arbitrary_intersection(of sets: List(Set(a))) -> Set(a) {
  sets
  |> list.reduce(with: set.intersection)
  |> resx.assert_unwrap
}

pub fn flat_map(s: Set(a), with fun: fn(a) -> Set(b)) -> Set(b) {
  s
  |> set.to_list
  |> list.map(with: fun)
  |> arbitrary_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)
  }
}

pub fn subtract(from a: Set(a), given b: Set(a)) -> Set(a) {
  set.drop(a, set.to_list(b))
}