(1 to: self) inject: 1 into: #*

Dan Ingalls Dan.Ingalls at disney.com
Wed Jan 19 21:47:42 UTC 2000


Mats Nygren <nygren at sics.se> wrote:

>I find the following elegant:
>	aCollection inject: 1 into: #*
>(and similarly for all binary messages and single-keyword messages)
>more so than the normal:
>	aCollection inject: 1 into: [ :a :b | a * b]
>
>For example like this:

>  factorial
>	(1 to: self) inject: 1 into: #*
>
>The price for this seems to be simply:
>!Symbol methodsFor: 'converting' stamp: 'mn 1/19/2000 19:38'!
>value: i value: ii
>	 ^i perform: self with: ii! !
>
>And similarly for unary messages
>	aCollection collect: #negated

Well, it seems like January 2000 is the time to recapitulate the interesting discussions of the past.  This topic has come up a couple of tiems in the past -- I believe it was Ward Cunningham who first thought of endowing symbols with block-like behavior in this manner (back in Tektronix days, I think).  Then about a year ago, Marcel Weiher contributed a cool idea for allowing this kind of cascaded functionals that he called "trampolines" (see his msg of 12/13/1998).  The idea is that if a trampoline doesn't understand a message, then it turns around and sends it iteratively to all of its elements.

One of the reasons we didn't actually act on these suggestions on the last go-round was that we got distracted by extending Squeak's numeric coercions to handle collections enabling APL-like examples as

	#(1 2 3 4 5) * 2
and 
	2 * #(1 2 3 4 5).

However, we need to revisit this territory to bring it to completion, since neither

	#(1 2 3 4 5) negated
nor 
	#(1 2 3 4 5) raisedTo: 2

work in the analogously convenient way.  Interestingly, Marcel's trick offers a possible generic solution without having to write coercion code into all the numeric operations.  It's also a place where you would like to have numeric protocol be a first-class object.

You have to be careful rushing into this kind of thing, partly because of the subtlety aspect ("obfuscation", as Bob Jarvis puts it).  Also if you get an error in a trampoline, things may have gone far beyond what the user had in mind -- as, eg, if you misspell a collection selector, and it turns around and tries to send it to one of its elements.  I think there is a "sweet spot" here but, as with some of our other pending projects, I think we need some careful review and experimentation before we pop it into the mainline release.

	- Dan

	- Dan

	- Dan
	






More information about the Squeak-dev mailing list