Skip to content

Commit f40a63f

Browse files
committed
mapreduce_associative -> mapreduce (#4046)
1 parent 73153f5 commit f40a63f

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

base/reduce.jl

+8-6
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ function mapreduce(f::Callable, op::Function, v0, itr)
151151
return v
152152
end
153153

154-
# mapreduce for associative operations, using pairwise recursive reduction
154+
# mapreduce for random-access arrays, using pairwise recursive reduction
155155
# for improved accuracy (see sum_pairwise)
156156
function mr_pairwise(f::Callable, op::Function, A::AbstractArray, i1,n)
157157
if n < 128
@@ -165,12 +165,14 @@ function mr_pairwise(f::Callable, op::Function, A::AbstractArray, i1,n)
165165
return op(mr_pairwise(f,op,A, i1,n2), mr_pairwise(f,op,A, i1+n2,n-n2))
166166
end
167167
end
168-
function mapreduce_associative(f::Callable, op::Function, A::AbstractArray)
168+
function mapreduce(f::Callable, op::Function, A::AbstractArray)
169169
n = length(A)
170170
n == 0 ? op() : mr_pairwise(f,op,A, 1,n)
171171
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
174176

175177
function any(itr)
176178
for x in itr
@@ -192,8 +194,8 @@ end
192194

193195
max(f::Function, itr) = mapreduce(f, max, itr)
194196
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)
197199

198200
function count(pred::Function, itr)
199201
s = 0

0 commit comments

Comments
 (0)