[ruby-core:78091] [Ruby trunk Feature#12902] How about Enumerable#sum uses initial value rather than 0 as default?
From:
jeremydaer@...
Date:
2016-11-11 22:16:06 UTC
List:
ruby-core #78091
Issue #12902 has been updated by Jeremy Daer. If we treated the first argument to `#sum` as the additive identity instead of "initial element," then it'd be very clear what to expect: When there are no elements in the Enumerable, return the identity. That works nicely for summing non-numeric/string/etc objects: ~~~ ruby payments.none? # => true payments.sum(Payment.new(0)) # => Payment.new(0) ~~~ Plus, it's backward compatible, doesn't break existing behavior. ---------------------------------------- Feature #12902: How about Enumerable#sum uses initial value rather than 0 as default? https://bugs.ruby-lang.org/issues/12902#change-61449 * Author: Aaron Lasseigne * Status: Feedback * Priority: Normal * Assignee: ---------------------------------------- In https://bugs.ruby-lang.org/issues/12217#note-3, Akira Tanaka, mentions that the default argument to sum is 0. This creates problems with non-numeric summations (e.g. strings). This would make the method more flexible. It also makes it behave more like `reduce`. I think using the initial value in the enumerable is less surprising than using 0. -- https://bugs.ruby-lang.org/ Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe> <http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>