Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove initial value for reduce() #5797

Closed
ivarne opened this issue Feb 13, 2014 · 10 comments
Closed

Remove initial value for reduce() #5797

ivarne opened this issue Feb 13, 2014 · 10 comments

Comments

@ivarne
Copy link
Sponsor Member

ivarne commented Feb 13, 2014

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

see https://groups.google.com/forum/#!topic/julia-users/7PDUKNcBtQA

@lindahua
Copy link
Contributor

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
Copy link
Member

kmsquire commented Feb 13, 2014

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

@JeffBezanson
Copy link
Sponsor Member

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

@stevengj
Copy link
Member

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
Copy link
Member

quinnj commented Jun 6, 2014

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

@lindahua
Copy link
Contributor

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.

@dlfivefifty
Copy link
Contributor

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

julia> reduce(+,Float64[])
0.0

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

julia> foldl(+,Float64[])
0.0

Is this a separate issue?

@stevengj
Copy link
Member

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

@TotalVerb
Copy link
Contributor

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

@simonbyrne
Copy link
Contributor

I think the ship has sailed on this one: v0 now is a keyword argument init, and we define reduce to have implementation dependent associativity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants