Compare commits

...

2 Commits

Author SHA1 Message Date
Kim Morrison
7ad063675c add tests 2024-10-02 12:14:56 +10:00
Kim Morrison
12c29f5254 feat: HashSet.all/any 2024-10-02 12:06:17 +10:00
2 changed files with 28 additions and 0 deletions

View File

@@ -192,6 +192,18 @@ instance [BEq α] [Hashable α] {m : Type v → Type v} : ForM m (HashSet α) α
instance [BEq α] [Hashable α] {m : Type v Type v} : ForIn m (HashSet α) α where
forIn m init f := m.forIn f init
/-- Check if all elements satisfy the predicate, short-circuiting if a predicate fails. -/
@[inline] def all (m : HashSet α) (p : α Bool) : Bool := Id.run do
for a in m do
if ¬ p a then return false
return true
/-- Check if any element satisfies the predicate, short-circuiting if a predicate succeeds. -/
@[inline] def any (m : HashSet α) (p : α Bool) : Bool := Id.run do
for a in m do
if p a then return true
return false
/-- Transforms the hash set into a list of elements in some order. -/
@[inline] def toList (m : HashSet α) : List α :=
m.inner.keys

View File

@@ -424,6 +424,22 @@ def addKeyToState (k : Nat) : StateM Nat PUnit := do
ans := k :: ans
return ans
/-- info: true -/
#guard_msgs in
#eval m.any fun x => x > 4
/-- info: false -/
#guard_msgs in
#eval m.any fun x => x = 0
/-- info: true -/
#guard_msgs in
#eval m.all fun x => x < 2^30
/-- info: false -/
#guard_msgs in
#eval m.all fun x => x > 4
/-- info: [1000000000, 2, 1] -/
#guard_msgs in
#eval m.toList