@@ -22,19 +22,18 @@ end Primes
2222
2323def hideEggs : IO Unit := do
2424 -- Repeat colors and locations indefinitely
25- let colors := IterM .unfold Id (0 : Nat) (· + 1 ) |>.map fun n =>
25+ let colors := Iter .unfold (0 : Nat) (· + 1 ) |>.map fun n =>
2626 #["green" , "red" , "yellow" ][n % 3 ]!
27- let locations := IterM .unfold Id (0 : Nat) (· + 1 ) |>.map fun n =>
27+ let locations := Iter .unfold (0 : Nat) (· + 1 ) |>.map fun n =>
2828 #["in a boot" , "underneath a lampshade" , "under the cushion" , "in the lawn" ][n % 4 ]!
2929 -- Summon the chickens
30- let chickens := ["Clucky" , "Patches" , "Fluffy" ].iter Id
30+ let chickens := ["Clucky" , "Patches" , "Fluffy" ].iter
3131 -- Gather, color and hide the eggs
32- let eggs := chickens.flatMap (fun x : String => IterM .unfold Id x id |>.take 3 )
32+ let eggs := chickens.flatMap (fun x : String => Iter .unfold x id |>.take 3 )
3333 |>.zip colors
3434 |>.zip locations
3535 -- Report the results (top secret)
36- let eggsIO := eggs.switchMonad (pure : ∀ {γ}, γ → IO γ) -- Obtain an IO-monadic iterator
37- for x in eggsIO do
36+ for x in eggs do
3837 IO.println s! "{x.1.1} hides a {x.1.2} egg {x.2}."
3938 -- Alternative : eggsIO.mapM (fun x => IO.println s!"{x.1.1} hides a {x.1.2} egg {x.2}.") |>.drain
4039
@@ -60,7 +59,7 @@ def firstOfEach (l : List (List Nat)) : List Nat :=
6059#eval ! firstOfEach [[1 , 2 ], [], [3 , 4 ]]
6160
6261def deepSum (l : List (List Nat)) : Nat :=
63- go (l.iter Id |>.flatMap fun l' => l'.iter Id ) 0
62+ go (l.iter |>.flatMap fun l' => l'.iter) 0
6463where
6564 @[specialize]
6665 go it acc :=
@@ -107,15 +106,15 @@ Additional diagnostic information may be available using the `set_option diagnos
107106-- as Rust
108107
109108def dependentFlatMap (l : List (List Nat)) : List Nat :=
110- let it := IterM .unfold Id 2 (· + 1 ) |>.zip l.iter
111- it.flatMapD (fun (x : Nat × List Nat) => ( x.2 .iter Id) .filter fun y => y % x.1 = 0 ) |>.toList
109+ let it := Iter .unfold 2 (· + 1 ) |>.zip l.iter
110+ it.flatMapD (fun (x : Nat × List Nat) => x.2 .iter.filter fun y => y % x.1 = 0 ) |>.toList
112111
113112/-- info: [2, 6, 9] -/
114113#guard_msgs in
115114#eval ! dependentFlatMap [[1 , 2 , 3 ], [4 , 5 , 6 , 9 ]]
116115
117116def addIndices (l : List Nat) : List (Nat × Nat) :=
118- IterM .unfold Id 0 (· + 1 ) |>.zip ( l.iter Id) |>.toList
117+ Iter .unfold 0 (· + 1 ) |>.zip l.iter |>.toList
119118
120119/-- info: [(0, 3), (1, 1), (2, 4), (3, 1), (4, 5), (5, 9)] -/
121120#guard_msgs in
@@ -126,7 +125,7 @@ def addIndices (l : List Nat) : List (Nat × Nat) :=
126125#eval ! [3 , 1 , 4 , 1 , 5 , 9 ].iter.filter (· % 2 = 0 ) |>.toList
127126
128127def printNums (l : List Nat) : IO Unit :=
129- l.iter IO |>.mapM (fun n => do IO.println (toString n); pure n) |>.drain
128+ l.iterM IO |>.mapM (fun n => do IO.println (toString n); pure n) |>.drain
130129
131130
132131/--
@@ -139,7 +138,7 @@ info: 1
139138
140139-- Same result as `printNums` but showcasing that we can use iterators in `for-in` constructs
141140def printNumsForIn (l : List Nat) : IO Unit := do
142- for n in l.iter IO do
141+ for n in l.iter do
143142 IO.println n
144143
145144def timestamps (n : Nat) : IO (List Std.Time.PlainTime) := do
@@ -162,7 +161,7 @@ def timestamps (n : Nat) : IO (List Std.Time.PlainTime) := do
162161
163162-- This example demonstrates that chained `mapM` calls are executed in a different order than with `List.mapM`.
164163def chainedMapM (l : List Nat) : IO Unit :=
165- l.iter IO |>.mapM (IO.println <| s! "1st { .} " ) |>.mapM (IO.println <| s! "2nd { ·} " ) |>.drain
164+ l.iterM IO |>.mapM (IO.println <| s! "1st { .} " ) |>.mapM (IO.println <| s! "2nd { ·} " ) |>.drain
166165
167166/--
168167info: 1st 1
0 commit comments