How to impress the locals...

Bert Freudenberg bert at impara.de
Mon Jul 18 14:25:19 UTC 2005


Am 17.07.2005 um 00:08 schrieb Randal L. Schwartz:

>>>>>> "Alan" == Alan Lovejoy <squeak-dev.sourcery at forum-mail.net>  
>>>>>> writes:
>>>>>>
>
> Alan>             #(9 2 4 12 -3 44) inject: Infinity positive into:  
> [:min :value |
> Alan> value >= min ifTrue: [min] ifFalse: [value]]
>
> Alan>             #(9 2 4 12 -3 44) inject: Infinity negative into:  
> [:max :value |
> Alan> value <= max ifTrue: [max] ifFalse: [value]]
>
> Those look overly complicated:
>
>     s := #(9 2 4 12 -3 44).
>     max := s allButFirst inject: s first into: [:r :i | r max: i]
>
> If you don't like the allButFirst copy, you can use the original
> set anyway, since the identity element of "s first" will max with
> itself just fine.

Using #max: is warranted, but using #first is not. The collection  
might be empty, it might not respond to #first, etc.

For readability, I'd name the second block argument "each". No good  
idea for the first argument, though. "prev" might work.

- Bert -




More information about the Squeak-dev mailing list