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

Enumerable#sum for empty array #247

Open
boomerang opened this issue Dec 4, 2015 · 7 comments
Open

Enumerable#sum for empty array #247

boomerang opened this issue Dec 4, 2015 · 7 comments
Labels

Comments

@boomerang
Copy link

I use Enumerable#sum a lot and just upgraded from version 2.9.3 to 3.0 and the behaviour changed.

[].sum should == 0 but i get nil instead.

@trans
Copy link
Member

trans commented Dec 5, 2015

Hmm... was my last generalization a bit too much? Have a look at the progression:

  1. ca4aae4
  2. 206c1f8
  3. 302e000

However, there is a certain logic to it b/c now it accommodates any form of summation, i.e. any thing that responds to #+. So for instance [[1],[2],[3]].sum works too (it produces [1,2,3]). The price for this flexibility however is that it will sometimes be necessary to give the identity, e.g. [].sum(0).

What do you think?

@boomerang
Copy link
Author

I think is´t ok, but change the example documentation in the method :)
says that
[].sum => 0

I changed to active_support #sum it works as yours did before.

@trans
Copy link
Member

trans commented Dec 11, 2015

Ok, thanks. I updated the docs.

Honestly I am on the fence about whether Enumerable#sum should be generic or useful only for numerics.

@ioquatix
Copy link
Contributor

Ruby 2.4 now includes #sum so be aware that this may cause issues. https://bugs.ruby-lang.org/issues/12217

@trans
Copy link
Member

trans commented Oct 26, 2016

Thanks. This is a bit unfortunate b/c I really believe the more generalized form is better. Yes, it means passing an argument, e.g. sum(0), for the common case, but it makes you think about what you are doing, e.g. if summing floats. Ruby's version doesn't do that. Boo hiss! And b/c Ruby's is defined on Array instead of Enumerable it override Facets' method in the common cases. Not sure if Matz if right about this choice, it can go either way, it makes sense for some Enumerable classes and not others. In any case Facets will probably have to override Ruby's method to get the better behavior, but if so it has to be done in a compatible way, and that means parameterless sum has to return 0. ActiveSupport took an interesting approach and checked the final return value, if it is nil then it returns 0 instead. Maybe we can do that to, and with any luck there won't be any edge cases (i.e. Facets' sum will be a full superset of Ruby's). If not then the only choice is either convince Matz to change it in Ruby (hard to do) or rename the Facets method -- but to what?

@trans trans added the ruby label Oct 26, 2016
@ioquatix
Copy link
Contributor

Ruby does have a few weird things in it. I'd suggest that we file a bug report on Ruby.

To be honest, sum is a bit of a stupid method. It would even make more sense [].sum || "empty" but even this is not possible if as you say sum returns 0.

@ioquatix
Copy link
Contributor

Ah, found this: https://bugs.ruby-lang.org/issues/12902 could be a good idea for us to chime in.

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

No branches or pull requests

3 participants