[squeak-dev] Collection>sum fails on empty collection

Levente Uzonyi leves at caesar.elte.hu
Tue Jan 8 22:26:25 UTC 2019


Use #detectSum::

#() detectSum: #yourself. "==> 0"

It also gives you full control:

#(true false 1 2 3.5 'foo') detectSum: [ :each |
 	each isNumber "Yes, this won't work for complex numbers and quaternions"
 		ifTrue: [ each ]
 		ifFalse: [ 0 ] ]. "==> 6.5"


Levente

On Tue, 8 Jan 2019, tim Rowledge wrote:

> #() sum -> errorEmptyCollection.
>
> #sum uses #reduce: and I can see why #reduce might consider an empty collection an error - but I also see that #sum of an empty collection probably ought to be 0, at least in a lot of cases. And I'd note that using a simple #inject:into: - in the case of a million item Array, at least - took about 60% of the time of the current #sum.
>
> Why did this come up? In an MVC Project I tested
>  PluggableFileList getFolderDialog openLabel: 'foo'
> whilst seeing if PluggableFileList had any uses. It turns out that building a PluggableFileList in MVC doesn't work. Somewhere we end up trying to build a pluggablepanel with no children, which makes the logic in MVCToolBuilder>>setLayout:in: get upset. I don't care enough about making MVC stuff work to be willing to spend time on that.
>
> tim
> --
> tim Rowledge; tim at rowledge.org; http://www.rowledge.org/tim
> Fractured Idiom:- RIGOR MORRIS - The cat is dead


More information about the Squeak-dev mailing list