HOM - Higher Order Messages
Jon Hylands
jon at huv.com
Thu Apr 20 12:50:20 UTC 2006
On Thu, 20 Apr 2006 07:40:39 +0200, "Klaus D. Witzel"
<klaus.witzel at cobss.com> wrote:
> newArray #at: eachOf: (1 to: n) #put:
> withEach: oldArray onlyIf: [:index :element | element notNil]
This is supposed to be more readable (and higher-order) than normal
Smalltalk syntax?
newArray := oldArray select: [:each | each notNil]
Personally, I think the main thing driving stuff like this must be that
people can't remember how #inject:into: works.
Many many years ago, Kent Beck proposed the following rules for collection
interation, and I have used these rules faithfully since:
- the iteration variable is always called 'each'. You can add to that, so
it is 'eachItem' or 'eachName' or 'eachClass' or whatever.
- for #inject:into:, the block arguments are always sum and each, in that
order.
#(1 2 3) inject: 0 into: [:sum :each | sum + each].
#(1 2 3)
inject: Dictionary new
into: [:sum :each |
sum
at: each put: 'Number ', each printString;
yourself].
I never have to look up #inject:into to remember, because I just remember
that the arguments are sum and each, and the rest is obvious.
It seems like this (HOM) is a solution for a simple language, where you
can't do anything complicated. Blocks in Smalltalk can have arbitrary code,
and I very often use them that way when combined with the specialized
collection iterators.
Later,
Jon
--------------------------------------------------------------
Jon Hylands Jon at huv.com http://www.huv.com/jon
Project: Micro Seeker (Micro Autonomous Underwater Vehicle)
http://www.huv.com
More information about the Squeak-dev
mailing list
|