@@ -151,7 +151,7 @@ function mapreduce(f::Callable, op::Function, v0, itr)
151
151
return v
152
152
end
153
153
154
- # mapreduce for associative operations , using pairwise recursive reduction
154
+ # mapreduce for random-access arrays , using pairwise recursive reduction
155
155
# for improved accuracy (see sum_pairwise)
156
156
function mr_pairwise (f:: Callable , op:: Function , A:: AbstractArray , i1,n)
157
157
if n < 128
@@ -165,12 +165,14 @@ function mr_pairwise(f::Callable, op::Function, A::AbstractArray, i1,n)
165
165
return op (mr_pairwise (f,op,A, i1,n2), mr_pairwise (f,op,A, i1+ n2,n- n2))
166
166
end
167
167
end
168
- function mapreduce_associative (f:: Callable , op:: Function , A:: AbstractArray )
168
+ function mapreduce (f:: Callable , op:: Function , A:: AbstractArray )
169
169
n = length (A)
170
170
n == 0 ? op () : mr_pairwise (f,op,A, 1 ,n)
171
171
end
172
- # can't easily do pairwise reduction without random access, so punt:
173
- mapreduce_associative (f:: Callable , op:: Function , itr) = mapreduce (f, op, itr)
172
+ function mapreduce (f:: Callable , op:: Function , v0, A:: AbstractArray )
173
+ n = length (A)
174
+ n == 0 ? v0 : op (v0, mr_pairwise (f,op,A, 1 ,n))
175
+ end
174
176
175
177
function any (itr)
176
178
for x in itr
192
194
193
195
max (f:: Function , itr) = mapreduce (f, max, itr)
194
196
min (f:: Function , itr) = mapreduce (f, min, itr)
195
- sum (f:: Function , itr) = mapreduce_associative (f, + , itr)
196
- prod (f:: Function , itr) = mapreduce_associative (f, * , itr)
197
+ sum (f:: Function , itr) = mapreduce (f, + , itr)
198
+ prod (f:: Function , itr) = mapreduce (f, * , itr)
197
199
198
200
function count (pred:: Function , itr)
199
201
s = 0
0 commit comments