Remove initial value for reduce() #5797

ivarne opened this Issue Feb 13, 2014 · 9 comments


None yet
8 participants

ivarne commented Feb 13, 2014

It is unclear how a initial value to reduce should be interpreted, as the associativity for reduce is undefined.



lindahua commented Feb 13, 2014

I support deprecating the reduce method with v0, and ask people to use foldl instead when they want to fold a sequence from an initial value.


kmsquire commented Feb 13, 2014 edited by StefanKarpinski

I believe the initial value allows one to reduce on an empty iterable, whereas without it, the default value may be unclear.


JeffBezanson commented Feb 13, 2014

I agree the initial value is silly given our definition of reduce. It can throw an error for an empty container.


stevengj commented Feb 15, 2014

I suppose that anyone who really needs a behavior for an empty container can do isempty(x) ? v0 : reduce(+, x). One question is whether this is a gotcha behavior. Also, isempty doesn't currently work for general iterables (though I suppose this could be fixed?), so the lack of a reduce(+, v0, x) method makes this behavior harder to implement in a general way.


quinnj commented Jun 6, 2014

What else is left to do here? Just deprecate the initial value variable? @lindahua


lindahua commented Jun 6, 2014

Currently, when v0 is present, we simply delegate to foldl. We may deprecate this method in favor of foldl. But I don't really have a strong opinion.

In 0.6, reduce and foldr/foldl are inconsistent for empty vectors:

julia> reduce(+,Float64[])

julia> foldr(+,Float64[])
ERROR: BoundsError: attempt to access 0-element Array{Float64,1} at index [0]
 [1] foldr(::Function, ::Array{Float64,1}) at ./reduce.jl:160

julia> foldl(+,Float64[])

Is this a separate issue?


stevengj commented Jan 20, 2017

@dlfivefifty, I think that's a separate issue.


TotalVerb commented Jan 20, 2017

v0 is still meaningful when the operation is both associative and commutative, and so I don't think it should be removed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment