From 482a0b454c28233de96a30891943db9e7bccc80c Mon Sep 17 00:00:00 2001 From: Tomasz Chojnacki Date: Sat, 23 Dec 2023 16:17:00 +0100 Subject: =?UTF-8?q?Finish=20all=20days=20from=202020=20=F0=9F=8E=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- aoc-2020-gleam/src/ext/setx.gleam | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'aoc-2020-gleam/src/ext/setx.gleam') 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)) +} -- cgit v1.2.3