-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
Copy pathreduce.jl
258 lines (238 loc) · 5.1 KB
/
reduce.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
## reductions ##
function reduce(op::Function, itr) # this is a left fold
if is(op,+)
return sum(itr)
elseif is(op,*)
return prod(itr)
elseif is(op,|)
return any(itr)
elseif is(op,&)
return all(itr)
end
s = start(itr)
if done(itr, s)
return op() # empty collection
end
(v, s) = next(itr, s)
while !done(itr, s)
(x, s) = next(itr, s)
v = op(v,x)
end
return v
end
function maximum(itr)
s = start(itr)
if done(itr, s)
error("maximum: argument is empty")
end
(v, s) = next(itr, s)
while !done(itr, s)
(x, s) = next(itr, s)
v = scalarmax(v,x)
end
return v
end
function minimum(itr)
s = start(itr)
if done(itr, s)
error("minimum: argument is empty")
end
(v, s) = next(itr, s)
while !done(itr, s)
(x, s) = next(itr, s)
v = scalarmin(v,x)
end
return v
end
function sum(itr)
s = start(itr)
if done(itr, s)
return +()
end
(v, s) = next(itr, s)
while !done(itr, s)
(x, s) = next(itr, s)
v = v+x
end
return v
end
function prod(itr)
s = start(itr)
if done(itr, s)
return *()
end
(v, s) = next(itr, s)
while !done(itr, s)
(x, s) = next(itr, s)
v = v*x
end
return v
end
function reduce(op::Function, v0, itr)
v = v0
if is(op,+)
for x in itr
v = v+x
end
elseif is(op,*)
for x in itr
v = v*x
end
else
u = v0
for x in itr
u = op(u,x)
end
return u
end
return v
end
##
# generic map on any iterator
function map(f::Callable, iters...)
result = {}
len = length(iters)
states = [start(iters[idx]) for idx in 1:len]
nxtvals = cell(len)
cont = true
for idx in 1:len
done(iters[idx], states[idx]) && (cont = false; break)
end
while cont
for idx in 1:len
nxtvals[idx],states[idx] = next(iters[idx], states[idx])
end
push!(result, f(nxtvals...))
for idx in 1:len
done(iters[idx], states[idx]) && (cont = false; break)
end
end
result
end
function mapreduce(f::Callable, op::Function, itr)
s = start(itr)
if done(itr, s)
return op() # empty collection
end
(x, s) = next(itr, s)
v = f(x)
while !done(itr, s)
(x, s) = next(itr, s)
v = op(v,f(x))
end
return v
end
function mapreduce(f::Callable, op::Function, v0, itr)
v = v0
for x in itr
v = op(v,f(x))
end
return v
end
# mapreduce for random-access arrays, using pairwise recursive reduction
# for improved accuracy (see sum_pairwise)
function mr_pairwise(f::Callable, op::Function, A::AbstractArray, i1,n)
if n < 128
@inbounds v = f(A[i1])
for i = i1+1:i1+n-1
@inbounds v = op(v,f(A[i]))
end
return v
else
n2 = div(n,2)
return op(mr_pairwise(f,op,A, i1,n2), mr_pairwise(f,op,A, i1+n2,n-n2))
end
end
function mapreduce(f::Callable, op::Function, A::AbstractArray)
n = length(A)
n == 0 ? op() : mr_pairwise(f,op,A, 1,n)
end
function mapreduce(f::Callable, op::Function, v0, A::AbstractArray)
n = length(A)
n == 0 ? v0 : op(v0, mr_pairwise(f,op,A, 1,n))
end
function r_pairwise(op::Function, A::AbstractArray, i1,n)
if n < 128
@inbounds v = A[i1]
for i = i1+1:i1+n-1
@inbounds v = op(v,A[i])
end
return v
else
n2 = div(n,2)
return op(r_pairwise(op,A, i1,n2), r_pairwise(op,A, i1+n2,n-n2))
end
end
function reduce(op::Function, A::AbstractArray)
n = length(A)
n == 0 ? op() : r_pairwise(op,A, 1,n)
end
function reduce(op::Function, v0, A::AbstractArray)
n = length(A)
n == 0 ? v0 : op(v0, r_pairwise(op,A, 1,n))
end
function any(itr)
for x in itr
if x
return true
end
end
return false
end
function all(itr)
for x in itr
if !x
return false
end
end
return true
end
maximum(f::Function, itr) = mapreduce(f, scalarmax, itr)
minimum(f::Function, itr) = mapreduce(f, scalarmin, itr)
sum(f::Function, itr) = mapreduce(f, + , itr)
prod(f::Function, itr) = mapreduce(f, * , itr)
function count(pred::Function, itr)
s = 0
for x in itr
if pred(x)
s+=1
end
end
s
end
function any(pred::Function, itr)
for x in itr
if pred(x)
return true
end
end
return false
end
function all(pred::Function, itr)
for x in itr
if !pred(x)
return false
end
end
return true
end
function in(x, itr)
for y in itr
if isequal(y,x)
return true
end
end
return false
end
function contains(itr, x)
depwarn("contains(collection, item) is deprecated, use in(item, collection) instead", :contains)
in(x, itr)
end
function contains(eq::Function, itr, x)
for y in itr
if eq(y,x)
return true
end
end
return false
end