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

Ramon Leon ramon.leon at allresnet.com
Tue Aug 12 18:29:49 UTC 2008


> Not really.  It actually makes nil a special positional value.
> 
> { nil . 3 } sum => 3
> { 3 . nil } sum => "can't add 3 + nil"
> 
> I don't like it from that perspective.  Any/all leading nils would
> be stripped, but other nils would be included.  But what does 
> "leading"
> mean in an UnorderedCollection?  Exactly.

Ah, you're right, ordering shouldn't matter, this behaves as I expected...

sum
    "Answers the sum of the elements in the receiver, nil if empty"
    | sum |
    self do: 
        [ :value | 
        sum := sum 
            ifNil: [ value ]
            ifNotNil: 
                [ value 
                    ifNil: [ sum ]
                    ifNotNil: [ sum + value ] ] ].
    ^ sum

Ramon Leon
http://onsmalltalk.com




More information about the Squeak-dev mailing list