@@ -89,55 +89,7 @@ defmodule Stream do
8989 returns an anonymous function may return a struct in future releases.
9090 """
9191
92- defmodule Lazy do
93- @ moduledoc false
94- defstruct enum: nil , funs: [ ] , accs: [ ] , done: nil
95- end
96-
97- defimpl Enumerable , for: Lazy do
98- @ compile :inline_list_funs
99-
100- def reduce ( lazy , acc , fun ) do
101- do_reduce ( lazy , acc , fn x , [ acc ] ->
102- { reason , acc } = fun . ( x , acc )
103- { reason , [ acc ] }
104- end )
105- end
106-
107- def count ( _lazy ) do
108- { :error , __MODULE__ }
109- end
110-
111- def member? ( _lazy , _value ) do
112- { :error , __MODULE__ }
113- end
114-
115- defp do_reduce ( % Lazy { enum: enum , funs: funs , accs: accs , done: done } , acc , fun ) do
116- composed = :lists . foldl ( fn fun , acc -> fun . ( acc ) end , fun , funs )
117- do_each ( & Enumerable . reduce ( enum , & 1 , composed ) ,
118- done && { done , fun } , :lists . reverse ( accs ) , acc )
119- end
120-
121- defp do_each ( reduce , done , accs , { command , acc } ) do
122- case reduce . ( { command , [ acc | accs ] } ) do
123- { :suspended , [ acc | accs ] , continuation } ->
124- { :suspended , acc , & do_each ( continuation , done , accs , & 1 ) }
125- { :halted , [ acc | _ ] } ->
126- { :halted , acc }
127- { :done , [ acc | _ ] = accs } ->
128- case done do
129- nil ->
130- { :done , acc }
131- { done , fun } ->
132- case done . ( fun ) . ( accs ) do
133- { :cont , [ acc | _ ] } -> { :done , acc }
134- { :halt , [ acc | _ ] } -> { :halted , acc }
135- { :suspend , [ acc | _ ] } -> { :suspended , acc , & ( { :done , elem ( & 1 , 1 ) } ) }
136- end
137- end
138- end
139- end
140- end
92+ defstruct enum: nil , funs: [ ] , accs: [ ] , done: nil
14193
14294 @ type acc :: any
14395 @ type element :: any
@@ -538,7 +490,7 @@ defmodule Stream do
538490
539491 """
540492 @ spec take ( Enumerable . t , non_neg_integer ) :: Enumerable . t
541- def take ( _enum , 0 ) , do: % Lazy { enum: [ ] }
493+ def take ( _enum , 0 ) , do: % Stream { enum: [ ] }
542494
543495 def take ( enum , n ) when n > 0 do
544496 lazy enum , n , fn ( f1 ) -> R . take ( f1 ) end
@@ -590,7 +542,7 @@ defmodule Stream do
590542 lazy enum , n , fn ( f1 ) -> R . take_every ( n , f1 ) end
591543 end
592544
593- def take_every ( _enum , 0 ) , do: % Lazy { enum: [ ] }
545+ def take_every ( _enum , 0 ) , do: % Stream { enum: [ ] }
594546
595547 @ doc """
596548 Lazily takes elements of the enumerable while the given
@@ -1069,18 +1021,63 @@ defmodule Stream do
10691021
10701022 @ compile { :inline , lazy: 2 , lazy: 3 , lazy: 4 }
10711023
1072- defp lazy ( % Lazy { funs: funs } = lazy , fun ) ,
1024+ defp lazy ( % Stream { funs: funs } = lazy , fun ) ,
10731025 do: % { lazy | funs: [ fun | funs ] }
10741026 defp lazy ( enum , fun ) ,
1075- do: % Lazy { enum: enum , funs: [ fun ] }
1027+ do: % Stream { enum: enum , funs: [ fun ] }
10761028
1077- defp lazy ( % Lazy { funs: funs , accs: accs } = lazy , acc , fun ) ,
1029+ defp lazy ( % Stream { funs: funs , accs: accs } = lazy , acc , fun ) ,
10781030 do: % { lazy | funs: [ fun | funs ] , accs: [ acc | accs ] }
10791031 defp lazy ( enum , acc , fun ) ,
1080- do: % Lazy { enum: enum , funs: [ fun ] , accs: [ acc ] }
1032+ do: % Stream { enum: enum , funs: [ fun ] , accs: [ acc ] }
10811033
1082- defp lazy ( % Lazy { done: nil , funs: funs , accs: accs } = lazy , acc , fun , done ) ,
1034+ defp lazy ( % Stream { done: nil , funs: funs , accs: accs } = lazy , acc , fun , done ) ,
10831035 do: % { lazy | funs: [ fun | funs ] , accs: [ acc | accs ] , done: done }
10841036 defp lazy ( enum , acc , fun , done ) ,
1085- do: % Lazy { enum: enum , funs: [ fun ] , accs: [ acc ] , done: done }
1037+ do: % Stream { enum: enum , funs: [ fun ] , accs: [ acc ] , done: done }
1038+ end
1039+
1040+ defimpl Enumerable , for: Stream do
1041+ @ compile :inline_list_funs
1042+
1043+ def reduce ( lazy , acc , fun ) do
1044+ do_reduce ( lazy , acc , fn x , [ acc ] ->
1045+ { reason , acc } = fun . ( x , acc )
1046+ { reason , [ acc ] }
1047+ end )
1048+ end
1049+
1050+ def count ( _lazy ) do
1051+ { :error , __MODULE__ }
1052+ end
1053+
1054+ def member? ( _lazy , _value ) do
1055+ { :error , __MODULE__ }
1056+ end
1057+
1058+ defp do_reduce ( % Stream { enum: enum , funs: funs , accs: accs , done: done } , acc , fun ) do
1059+ composed = :lists . foldl ( fn fun , acc -> fun . ( acc ) end , fun , funs )
1060+ do_each ( & Enumerable . reduce ( enum , & 1 , composed ) ,
1061+ done && { done , fun } , :lists . reverse ( accs ) , acc )
1062+ end
1063+
1064+ defp do_each ( reduce , done , accs , { command , acc } ) do
1065+ case reduce . ( { command , [ acc | accs ] } ) do
1066+ { :suspended , [ acc | accs ] , continuation } ->
1067+ { :suspended , acc , & do_each ( continuation , done , accs , & 1 ) }
1068+ { :halted , [ acc | _ ] } ->
1069+ { :halted , acc }
1070+ { :done , [ acc | _ ] = accs } ->
1071+ case done do
1072+ nil ->
1073+ { :done , acc }
1074+ { done , fun } ->
1075+ case done . ( fun ) . ( accs ) do
1076+ { :cont , [ acc | _ ] } -> { :done , acc }
1077+ { :halt , [ acc | _ ] } -> { :halted , acc }
1078+ { :suspend , [ acc | _ ] } -> { :suspended , acc , & ( { :done , elem ( & 1 , 1 ) } ) }
1079+ end
1080+ end
1081+ end
1082+ end
10861083end
0 commit comments