@@ -106,25 +106,24 @@ defmodule Stream do
106106 end
107107
108108 defp do_reduce ( Lazy [ enum : enum , funs: funs , accs: accs ] , acc , fun ) do
109- do_reduce ( & Enumerable . reduce ( enum , & 1 , & 2 ) , :lists . reverse ( accs ) , funs , acc , fun )
109+ composed = :lists . foldl ( fn fun , acc -> fun . ( acc ) end , fun , funs )
110+ do_each ( & Enumerable . reduce ( enum , & 1 , composed ) , :lists . reverse ( accs ) , acc )
110111 end
111112
112- defp do_reduce ( _reduce , _accs , _funs , { :halt , acc } , _fun ) do
113+ defp do_each ( _reduce , _accs , { :halt , acc } ) do
113114 { :halted , acc }
114115 end
115116
116- defp do_reduce ( reduce , accs , funs , { :suspend , acc } , _fun ) do
117- { :suspended , acc , & do_reduce ( reduce , accs , funs , & 1 , & 2 ) }
117+ defp do_each ( reduce , accs , { :suspend , acc } ) do
118+ { :suspended , acc , & do_each ( reduce , accs , & 1 ) }
118119 end
119120
120- defp do_reduce ( reduce , accs , funs , { :cont , acc } , fun ) do
121- composed = :lists . foldl ( fn fun , acc -> fun . ( acc ) end , fun , funs )
122-
123- case reduce . ( { :cont , [ acc | accs ] } , composed ) do
121+ defp do_each ( reduce , accs , { :cont , acc } ) do
122+ case reduce . ( { :cont , [ acc | accs ] } ) do
124123 { reason , [ acc | _ ] } ->
125124 { reason , acc }
126125 { :suspended , [ acc | accs ] , continuation } ->
127- { :suspended , acc , & do_reduce ( continuation , accs , funs , & 1 , & 2 ) }
126+ { :suspended , acc , & do_each ( continuation , accs , & 1 ) }
128127 end
129128 end
130129 end
@@ -241,20 +240,21 @@ defmodule Stream do
241240 lazy enum , fn ( f1 ) ->
242241 fn ( entry , acc ) ->
243242 enum = f . ( entry )
244- do_flat_map ( & Enumerable . reduce ( enum , & 1 , & 2 ) , { :cont , acc } , f1 )
243+ fun = & do_flat_map_each ( f1 , & 1 , & 2 )
244+ do_flat_map ( & Enumerable . reduce ( enum , & 1 , fun ) , { :cont , acc } )
245245 end
246246 end
247247 end
248248
249- defp do_flat_map ( reduce , acc , f1 ) do
249+ defp do_flat_map ( reduce , acc ) do
250250 try do
251- reduce . ( acc , & do_flat_map_each ( f1 , & 1 , & 2 ) )
251+ reduce . ( acc )
252252 catch
253253 { :stream_flat_map , acc } -> acc
254254 else
255255 { :done , acc } -> { :cont , acc }
256256 { :halted , acc } -> { :cont , acc }
257- { :suspended , acc , c } -> { :suspend , acc , & do_flat_map ( c , & 1 , & 2 ) }
257+ { :suspended , acc , c } -> { :suspend , acc , & do_flat_map ( c , & 1 ) }
258258 end
259259 end
260260
@@ -400,20 +400,21 @@ defmodule Stream do
400400 end
401401
402402 defp do_concat( enumerables, acc , fun ) do
403+ fun = & do_concat_each ( fun , & 1 , & 2 )
403404 Enumerable. reduce ( enumerables , acc , fn x , acc ->
404- do_concat_reduce ( & Enumerable . reduce ( x , & 1 , & 2 ) , { :cont , acc } , fun )
405+ do_concat_reduce ( & Enumerable . reduce ( x , & 1 , fun ) , { :cont , acc } )
405406 end )
406407 end
407408
408- defp do_concat_reduce ( reduce , acc , fun ) do
409+ defp do_concat_reduce ( reduce , acc ) do
409410 try do
410- reduce . ( acc , & do_concat_each ( fun , & 1 , & 2 ) )
411+ reduce . ( acc )
411412 catch
412413 { :stream_concat , acc } -> acc
413414 else
414415 { :done , acc } -> { :cont , acc }
415416 { :halted , acc } -> { :cont , acc }
416- { :suspended , acc , c } -> { :suspend , acc , & do_concat_reduce ( c , & 1 , & 2 ) }
417+ { :suspended , acc , c } -> { :suspend , acc , & do_concat_reduce ( c , & 1 ) }
417418 end
418419 end
419420
@@ -439,26 +440,28 @@ defmodule Stream do
439440 """
440441 @ spec cycle ( Enumerable . t ) :: t
441442 def cycle ( enumerable ) do
442- reduce = & Enumerable . reduce ( enumerable , & 1 , & 2 )
443- & do_cycle ( reduce , reduce , & 1 , & 2 )
443+ fn acc , fun ->
444+ reduce = & Enumerable . reduce ( enumerable , & 1 , fun )
445+ do_cycle ( reduce , reduce , acc )
446+ end
444447 end
445448
446- defp do_cycle ( _reduce , _cycle , { :halt , acc } , _fun ) do
449+ defp do_cycle ( _reduce , _cycle , { :halt , acc } ) do
447450 { :halted , acc }
448451 end
449452
450- defp do_cycle ( reduce , cycle , { :suspend , acc } , _fun ) do
451- { :suspended , acc , & do_cycle ( reduce , cycle , & 1 , & 2 ) }
453+ defp do_cycle ( reduce , cycle , { :suspend , acc } ) do
454+ { :suspended , acc , & do_cycle ( reduce , cycle , & 1 ) }
452455 end
453456
454- defp do_cycle ( reduce , cycle , acc , fun ) do
455- case reduce . ( acc , fun ) do
457+ defp do_cycle ( reduce , cycle , acc ) do
458+ case reduce . ( acc ) do
456459 { :done , acc } ->
457- do_cycle ( cycle , cycle , { :cont , acc } , fun )
460+ do_cycle ( cycle , cycle , { :cont , acc } )
458461 { :halted , acc } ->
459462 { :halted , acc }
460463 { :suspended , acc , continuation } ->
461- { :suspended , acc , & do_cycle ( continuation , cycle , & 1 , & 2 ) }
464+ { :suspended , acc , & do_cycle ( continuation , cycle , & 1 ) }
462465 end
463466 end
464467
@@ -494,7 +497,19 @@ defmodule Stream do
494497 """
495498 @ spec repeatedly ( ( ( ) -> element ) ) :: t
496499 def repeatedly ( generator_fun ) when is_function ( generator_fun , 0 ) do
497- unfold ( :repeat , fn :repeat -> { generator_fun . ( ) , :repeat } end )
500+ & do_repeatedly ( generator_fun , & 1 , & 2 )
501+ end
502+
503+ defp do_repeatedly ( generator_fun , { :suspend , acc } , fun ) do
504+ { :suspended , acc , & do_repeatedly ( generator_fun , & 1 , fun ) }
505+ end
506+
507+ defp do_repeatedly ( _generator_fun , { :halt , acc } , _fun ) do
508+ { :halted , acc }
509+ end
510+
511+ defp do_repeatedly ( generator_fun , { :cont , acc } , fun ) do
512+ do_repeatedly ( generator_fun , fun . ( generator_fun . ( ) , acc ) , fun )
498513 end
499514
500515 @ doc """
@@ -553,8 +568,8 @@ defmodule Stream do
553568 & do_unfold ( next_acc , next_fun , & 1 , & 2 )
554569 end
555570
556- defp do_unfold ( next_acc , next_fun , { :suspend , acc } , _fun ) do
557- { :suspended , acc , & do_unfold ( next_acc , next_fun , & 1 , & 2 ) }
571+ defp do_unfold ( next_acc , next_fun , { :suspend , acc } , fun ) do
572+ { :suspended , acc , & do_unfold ( next_acc , next_fun , & 1 , fun ) }
558573 end
559574
560575 defp do_unfold ( _next_acc , _next_fun , { :halt , acc } , _fun ) do
0 commit comments