aboutsummaryrefslogtreecommitdiff
path: root/aoc-2020-gleam/src/ext/setx.gleam
diff options
context:
space:
mode:
authorTomasz Chojnacki <tomaszchojnacki2001@gmail.com>2023-12-23 16:17:00 +0100
committerTomasz Chojnacki <tomaszchojnacki2001@gmail.com>2023-12-23 16:17:00 +0100
commit482a0b454c28233de96a30891943db9e7bccc80c (patch)
tree5f563b07f48edb907fe0e5d1d772c82e392cebfc /aoc-2020-gleam/src/ext/setx.gleam
parentd673400025f68968514cfec7205281745ea35667 (diff)
downloadgleam_aoc2020-482a0b454c28233de96a30891943db9e7bccc80c.tar.gz
gleam_aoc2020-482a0b454c28233de96a30891943db9e7bccc80c.zip
Finish all days from 2020 🎆
Diffstat (limited to 'aoc-2020-gleam/src/ext/setx.gleam')
-rw-r--r--aoc-2020-gleam/src/ext/setx.gleam17
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))
+}