@@ -89,55 +89,7 @@ defmodule Stream do
89
89
returns an anonymous function may return a struct in future releases.
90
90
"""
91
91
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
141
93
142
94
@ type acc :: any
143
95
@ type element :: any
@@ -538,7 +490,7 @@ defmodule Stream do
538
490
539
491
"""
540
492
@ 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: [ ] }
542
494
543
495
def take ( enum , n ) when n > 0 do
544
496
lazy enum , n , fn ( f1 ) -> R . take ( f1 ) end
@@ -590,7 +542,7 @@ defmodule Stream do
590
542
lazy enum , n , fn ( f1 ) -> R . take_every ( n , f1 ) end
591
543
end
592
544
593
- def take_every ( _enum , 0 ) , do: % Lazy { enum: [ ] }
545
+ def take_every ( _enum , 0 ) , do: % Stream { enum: [ ] }
594
546
595
547
@ doc """
596
548
Lazily takes elements of the enumerable while the given
@@ -1069,18 +1021,63 @@ defmodule Stream do
1069
1021
1070
1022
@ compile { :inline , lazy: 2 , lazy: 3 , lazy: 4 }
1071
1023
1072
- defp lazy ( % Lazy { funs: funs } = lazy , fun ) ,
1024
+ defp lazy ( % Stream { funs: funs } = lazy , fun ) ,
1073
1025
do: % { lazy | funs: [ fun | funs ] }
1074
1026
defp lazy ( enum , fun ) ,
1075
- do: % Lazy { enum: enum , funs: [ fun ] }
1027
+ do: % Stream { enum: enum , funs: [ fun ] }
1076
1028
1077
- defp lazy ( % Lazy { funs: funs , accs: accs } = lazy , acc , fun ) ,
1029
+ defp lazy ( % Stream { funs: funs , accs: accs } = lazy , acc , fun ) ,
1078
1030
do: % { lazy | funs: [ fun | funs ] , accs: [ acc | accs ] }
1079
1031
defp lazy ( enum , acc , fun ) ,
1080
- do: % Lazy { enum: enum , funs: [ fun ] , accs: [ acc ] }
1032
+ do: % Stream { enum: enum , funs: [ fun ] , accs: [ acc ] }
1081
1033
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 ) ,
1083
1035
do: % { lazy | funs: [ fun | funs ] , accs: [ acc | accs ] , done: done }
1084
1036
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
1086
1083
end
0 commit comments