Skip to content

Commit e38279e

Browse files
committed
Stream: add 'iterate'
1 parent 1ef62d8 commit e38279e

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

libraries/common/stream.effekt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,16 @@ def replicate[A](number: Int) { action: () => A }: Unit / emit[A] =
173173
replicate(number - 1) {action}
174174
}
175175

176+
/// Creates an infinite iterated stream given by an `initial` seed and a `step` function:
177+
/// iterate(a){f} ~> a, f(a), f(f(a)), f(f(f(a))), ...
178+
def iterate[A](initial: A) { step: A => A }: Unit / emit[A] = {
179+
var current = initial
180+
while (true) {
181+
do emit(current)
182+
current = step(current)
183+
}
184+
}
185+
176186

177187
def sum { stream : () => Unit / emit[Int] }: Int =
178188
returning::sum[Unit]{stream}.second

0 commit comments

Comments
 (0)