@@ -89,7 +89,7 @@ defmodule Stream do
8989 to `Stream.map/2`, we have automatically created a stream that will
9090 multiply the items in the list by 2 on enumeration.
9191
92- This module also provides other functions for creating streams, like
92+ This module also provides other functions for creating streams, such as
9393 `Stream.cycle/1`.
9494 """
9595
@@ -220,6 +220,27 @@ defmodule Stream do
220220 end ]
221221 end
222222
223+ @ doc """
224+ Emit a sequence of values, starting with `start_value`. Successive
225+ values are generated by calling `next_fun` on the previous value.
226+
227+
228+ ## Examples
229+
230+ iex> Stream.iterate(0, &1+1) |> Enum.take(5)
231+ [0,1,2,3,4]
232+
233+ """
234+
235+ @ spec iterate ( element , ( element -> element ) ) :: Lazy . t
236+ def iterate ( start_value , next_fun ) do
237+ do_iterate ( start_value , next_fun , & 1 , & 2 )
238+ end
239+
240+ defp do_iterate ( start_value , next_fun , acc , fun ) do
241+ do_iterate ( next_fun . ( start_value ) , next_fun , fun . ( start_value , acc ) , fun )
242+ end
243+
223244 @ doc """
224245 Creates a stream that will apply the given function on
225246 enumeration.
@@ -262,6 +283,27 @@ defmodule Stream do
262283 end ]
263284 end
264285
286+
287+ @ doc """
288+ Returns a stream generated by calling `generator_fun` repeatedly.
289+
290+ ## Examples
291+
292+ iex> Stream.repeatedly(function(:random.uniform/0)) |> Enum.take(3)
293+ [0.4435846174457203, 0.7230402056221108, 0.94581636451987]
294+
295+ """
296+ @ spec repeatedly ( ( -> element ) ) :: Lazy . t
297+ def repeatedly ( generator_fun )
298+ when is_function ( generator_fun , 0 ) do
299+ do_repeatedly ( generator_fun , & 1 , & 2 )
300+ end
301+
302+ defp do_repeatedly ( generator_fun , acc , fun ) do
303+ do_repeatedly ( generator_fun , fun . ( generator_fun . ( ) , acc ) , fun )
304+ end
305+
306+
265307 @ doc """
266308 Lazily takes the next `n` items from the enumerable and stops
267309 enumeration.
0 commit comments