@@ -89,15 +89,10 @@ 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
96- @ compile { :inline , increment_func: 0 }
97-
98- @ doc false
99- def increment_func ( ) , do: & 1 + 1
100-
10196 defrecord Lazy , [ :enumerable , :fun , :acc ]
10297
10398 defimpl Enumerable , for: Lazy do
@@ -225,6 +220,27 @@ defmodule Stream do
225220 end ]
226221 end
227222
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+
228244 @ doc """
229245 Creates a stream that will apply the given function on
230246 enumeration.
@@ -267,27 +283,6 @@ defmodule Stream do
267283 end ]
268284 end
269285
270- @ doc """
271- Emit a sequence of values, starting with `start_value`. Successive
272- values are generated by calling `incr_func` on the previous value.
273- The default parameters generate a sequence of integers starting at 0.
274-
275- ## Examples
276-
277- iex> Stream.seq |> Enum.take(5)
278- [0,1,2,3,4]
279-
280- """
281-
282- @ spec seq ( ( element -> element ) , element ) :: Lazy . t
283- def seq ( incr_fun // & 1 + 1 , start_value // 0 ) do
284- do_seq ( incr_fun , start_value , & 1 , & 2 )
285- end
286-
287- defp do_seq ( incr_fun , start_value , acc , fun ) do
288- do_seq ( incr_fun , incr_fun . ( start_value ) , fun . ( start_value , acc ) , fun )
289- end
290-
291286
292287 @ doc """
293288 Lazily takes the next `n` items from the enumerable and stops
0 commit comments