Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: HashSet.Raw.all/any #5591

Merged
merged 2 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/Std/Data/HashSet/Raw.lean
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,18 @@ instance {m : Type v → Type v} : ForM m (Raw α) α where
instance {m : Type v → Type v} : ForIn m (Raw α) α 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 : Raw α) (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 : Raw α) (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 : Raw α) : List α :=
m.inner.keys
Expand Down
16 changes: 16 additions & 0 deletions tests/lean/run/hashmap.lean
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,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
Expand Down
Loading