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

sum([]) vs reduce(+,[]) #6554

Closed
pwl opened this issue Apr 17, 2014 · 4 comments
Closed

sum([]) vs reduce(+,[]) #6554

pwl opened this issue Apr 17, 2014 · 4 comments

Comments

@pwl
Copy link
Contributor

pwl commented Apr 17, 2014

In the help, sum(x) is described as a "more convenient" notation for reduce(+,x). There is, however a use case when the two are not equivalent:

julia> reduce(+,[])
0

julia> sum([])
ERROR: no method convert(Type{None}, Int64)

The same goes for prod, but for any the behavior is inverted

julia> reduce(&,[])
ERROR: zero-argument & is ambiguous

julia> any([])
false

I suggest that sum([]) and prod([]) should return 0 and 1 respectively, just to conform with +() => 0 and *() => 1. I don't know what to do with any.

@pwl pwl changed the title sum([]) vs reduce(+,[]) sum([]) vs reduce(+,[]) Apr 17, 2014
@stevengj
Copy link
Member

I would tend to argue for the reverse. Having +() => 0 is just wrong if you don't know what type you are adding.

Maybe reduce(op, x) on an isempty(x) should return op(eltype(x)) rather than op(), and define +(T) to zero(T)+zero(T).

@stevengj
Copy link
Member

See also #6069.

For single-element collections, we could also define op(x[1]) to return the appropriate type (possibly promoted). This would also further simplify #6116.

@pwl pwl closed this as completed Apr 17, 2014
@JeffBezanson
Copy link
Sponsor Member

It seems like a good idea to get rid of +() etc. Calling operators with no arguments in some cases is probably not the right convention.

@pwl pwl reopened this Apr 17, 2014
@JeffBezanson
Copy link
Sponsor Member

Also, any and all are strictly boolean, so they are well-defined on empty collections.

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

3 participants