Compare commits

...

1 Commits

Author SHA1 Message Date
Kim Morrison
b004f4b98b feat HashSet.partition (unverified) 2024-09-17 16:50:57 +10:00
3 changed files with 19 additions and 0 deletions

View File

@@ -186,6 +186,15 @@ section Unverified
(init : δ) (b : DHashMap α β) : δ :=
b.1.fold f init
/-- Partition a hashset into two hashsets based on a predicate. -/
@[inline] def partition (f : (a : α) β a Bool)
(m : DHashMap α β) : DHashMap α β × DHashMap α β :=
m.fold (init := (, )) fun l, r a b =>
if f a b then
(l.insert a b, r)
else
(l, r.insert a b)
@[inline, inherit_doc Raw.forM] def forM (f : (a : α) β a m PUnit)
(b : DHashMap α β) : m PUnit :=
b.1.forM f

View File

@@ -190,6 +190,11 @@ section Unverified
(m : HashMap α β) : HashMap α β :=
m.inner.filter f
@[inline, inherit_doc DHashMap.partition] def partition (f : α β Bool)
(m : HashMap α β) : HashMap α β × HashMap α β :=
let l, r := m.inner.partition f
l, r
@[inline, inherit_doc DHashMap.foldM] def foldM {m : Type w Type w}
[Monad m] {γ : Type w} (f : γ α β m γ) (init : γ) (b : HashMap α β) : m γ :=
b.inner.foldM f init

View File

@@ -158,6 +158,11 @@ section Unverified
@[inline] def filter (f : α Bool) (m : HashSet α) : HashSet α :=
m.inner.filter fun a _ => f a
/-- Partition a hashset into two hashsets based on a predicate. -/
@[inline] def partition (f : α Bool) (m : HashSet α) : HashSet α × HashSet α :=
let l, r := m.inner.partition fun a _ => f a
l, r
/--
Monadically computes a value by folding the given function over the elements in the hash set in some
order.