diff options
Diffstat (limited to 'aoc-2020-gleam/src/ext/setx.gleam')
-rw-r--r-- | aoc-2020-gleam/src/ext/setx.gleam | 17 |
1 files changed, 16 insertions, 1 deletions
diff --git a/aoc-2020-gleam/src/ext/setx.gleam b/aoc-2020-gleam/src/ext/setx.gleam index 4a9c0c1..f2c67e8 100644 --- a/aoc-2020-gleam/src/ext/setx.gleam +++ b/aoc-2020-gleam/src/ext/setx.gleam @@ -1,6 +1,7 @@ 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 { @@ -17,11 +18,21 @@ pub fn map(s: Set(a), with fun: fn(a) -> b) -> Set(b) { |> 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) - |> list.fold(from: set.new(), with: set.union) + |> arbitrary_union } pub fn toggle(in s: Set(a), this value: a) -> Set(a) { @@ -31,3 +42,7 @@ pub fn toggle(in s: Set(a), this value: a) -> Set(a) { 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)) +} |