Yes, #(0 0 0) is the additive identity in *that* case. But in #((1 2) (3 4) (5 6)) sum it is #(0 0). And in #(((1 2 3) (4 5 6)) ((9 8 7) (6 5 4))) sum ==> #(#(10 10 10) #(10 10 10)) it is #((0 0 0) (0 0 0)). I suppose you could have SequenceableCollection>>zero ^self collect: [:each | each zero] The point is that it cannot in general be based on the *class* of some element of the receiver but depends on the *value*. But then we run into the amusing cases like { 1 hour. DateAndTime now} sum where an additive identity for Durations makes sense but an additive identity for DateAndTimes does not, and similarly {Date today. 1} sum In my case, an additional problem is that I have Object Enumerable (#sum and #product go here) Collection where Enumerable holds everything that can be defined just using #do: without assuming that you can do it more than once, so it was necessary to come up with a streaming definition for #sum that examines each element once and only once. When you do that, you discover that you don't *need* a zero except when the collection is empty, and in that case you can't *tell* what sort of zero would be appropriate. On Thu, 26 Mar 2020 at 01:00, Sven Van Caekenberghe <sven@stfx.eu> wrote:
#(0 0 0) is the additive identity in that case
On 25 Mar 2020, at 12:38, Richard O'Keefe <raoknz@gmail.com> wrote:
There are situations where #+ and #sum make sense but there is no additive identity. Here is an example: #((1 2 3) (4 5 6) (7 8 9)) sum
On Tue, 24 Mar 2020 at 02:46, James Foster <Smalltalk@jgfoster.net> wrote:
On Mar 23, 2020, at 6:06 AM, Sven Van Caekenberghe <sven@stfx.eu> wrote:
What you found out now is that the clever trick used to avoid picking an additive identity (picking an element, counting it twice and then subtracting it) leads to a loss of precision when floating point numbers are involved. This is an important issue.
If this approach is to be preserved, then each class should have an additive identity so instead of adding and subtracting an object, we let the object tell us its zero.
James