lean4-htt/tests/lean/run/forIn_phashset.lean
Leonardo de Moura 5cbeb22564
feat: add ForIn instance for PHashSet (#7214)
This PR adds a `ForIn` instance for the `PersistentHashSet` type.
2025-02-24 20:37:45 +00:00

35 lines
654 B
Text
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import Lean.Data.PersistentHashSet
open Lean
def sum (s : PHashSet Nat) : Nat := Id.run do
let mut r := 0
for a in s do
r := r + a
return r
def sumIf (s : PHashSet Nat) (p : Nat → Bool) : Nat := Id.run do
let mut r := 0
for a in s do
unless p a do
continue
r := r + a
return r
def mk [Hashable α] [BEq α] (f : Nat → α) (n : Nat) : PHashSet α := Id.run do
let mut s := {}
for i in [:n] do
s := s.insert (f i)
return s
/-- info: 45 -/
#guard_msgs in
#eval sum (mk id 10)
/-- info: 9900 -/
#guard_msgs in
#eval sum (mk (2*·) 100)
/-- info: 2450 -/
#guard_msgs in
#eval sumIf (mk id 100) (· % 2 == 0)