feat: HashSet.partition (unverified) (#5370)

`Aesop` is depending on (a custom-rolled) `Set` data structure with
`.partition`.
This commit is contained in:
Kim Morrison 2024-09-20 16:05:10 +10:00 committed by GitHub
parent 0a2d121e45
commit b41019e8e8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
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.