How to impress the locals...
Brian Rice
water at tunes.org
Sun Jul 17 03:24:06 UTC 2005
On Jul 16, 2005, at 3:08 PM, Randal L. Schwartz wrote:
>>>>>> "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.
...or you can call reduce:. In Slate, "#(9 2 4 12 -3 44) reduce:
#max: `er."* is sufficient to do all of this.
*NB: `er is a macro which expands a selector into the minimum
appropriate block which calls it on the right number of arguments and
returns the result.
The other examples he mentioned are more concise using parametrizable
Slate streams (TraversalStream in particular, which I need to write
cookbook examples for), which support all relevant collection
protocols stream-for-stream (beyond Craig's excellent Stream
framework Flow).
--
-Brian
More information about the Squeak-dev
mailing list
|