HOM - Higher Order Messages

Bert Freudenberg bert at impara.de
Fri Apr 21 12:28:44 UTC 2006


Am 21.04.2006 um 06:42 schrieb Klaus D. Witzel:

> Hi Jon
>
> on Thu, 20 Apr 2006 14:50:20 +0200, you <jon at huv.com> wrote:
>
>> 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]
>>
> No. Since it was explicitly asked for the first n elements, the  
> correct translation is
>
> 	(1 to: n) with: oldArray do: [:index :each |
> 		each notNil ifTrue: [newArray at: index put: each]]
>
> I believe that a good syntax coloring would help HOM off the ground  
> and, as the example shows, helps saving message selectors and  
> nesting of blocks.
>
> But also and for sure, every good thing has its counterexamples ;)


TerseMan says

	(oldArray atAll: (1 to: n)) select: [:each | each notNil]

This is actual, working, regular Squeak code.

And it's a more Smalltalky way of expressing this, too - whenever I  
see #at: and #at:put: used directly in an iterative manner I suspect  
there must be a more elegant way to express it. This is the most  
severe shortcoming in your "translation", along with the necessity to  
create newArray before, which adds another superfluous line of code.

If you want to get rid of the block, you could implement #value: on  
Symbols as "^arg perform: self". Then you can write even more tersely:

	(oldArray atAll: (1 to: n)) select: #notNil

... which you might call a very lightweight HOM.

- Bert -




More information about the Squeak-dev mailing list