[squeak-dev] Re: Collection>>sum implementation

Jason Johnson jason.johnson.081 at gmail.com
Wed Aug 13 11:59:42 UTC 2008


On Tue, Aug 12, 2008 at 8:02 PM, Andreas Raab <andreas.raab at gmx.de> wrote:
>
> I think we'll have to agree that the result is undefined if the collection
> contains elements that can't be summed up. While one can rewrite the method
> to deal with the specific problem you are describing like here:
>
> Collection>>sum
>   "Answer the sum of elements, or nil if empty"
>   | first sum |
>   first := true.
>   self do:[:each|
>     first
>       ifTrue:[sum := each. first := false]
>       ifFalse:[sum := sum + each]].
>  ^sum
>
> such that
>
>  {nil. 3} sum. => Error
>  {3. nil} sum. => Error
>
> it just seems to me that the result of, e.g.,
>
>  #() sum => nil
>  {nil} sum => nil
>
> is just as bad. I think that every proposed implementation has problems
> similar to these and I don't think it's addressable without unreasonable
> burden on both elements as well as implementation.
>
> Cheers,
>  - Andreas

>From my side, I just see #sum as convenience math utility (like
#average which is also in there).  I just wanted to see #reduce
included so this "add a sample then take it out again" could go away.
Purely for a teaching point of view, that is, I would rather new
programmers learn to refactor and build the language to their needs
instead of making strange work arounds to deal with "close but not
quite right" methods.



More information about the Squeak-dev mailing list