Squeak
  links to this page:    
View this PageEdit this PageUploads to this PageHistory of this PageTop of the SwikiRecent ChangesSearch the SwikiHelp Guide
DRINK ME
Last updated at 12:31 pm UTC on 17 January 2006
There seemed to be no use in waiting by the little door, so she went back to the table, half hoping she might find another key on it, or at any rate a book of rules for shutting people up like telescopes: this time she found a little bottle on it ("which certainly was not here before," said Alice), and tied round the neck of the bottle was a paper label, with the words "DRINK ME" beautifully printed on it in large letters.


DRINK ME is an exercise in capturing common but verbose idioms in Smalltalk in a more concise form.

(See Sorrow, OOPAL and Lispy Forth for some of the mail list discussion on this subject.)


"What a curious feeling!" said Alice. "I must be shutting up like a telescope."

And so it was indeed: she was now only ten inches high, and her face brightened up at the thought that she was now the right size for going through the little door into that lovely garden.


The current (and soon to be posted) code lets you use the 'each' keyword instead of 'collect: [:each| ]'.

For example:
'hello' each asUppercase -> 'HELLO'
#(3 2 1) each asString -> #('3' '2' '1')


"What do you mean by that?" said the Caterpillar, sternly. "Explain yourself!"

"I can't explain myself, I'm afraid, Sir", said Alice, "because I'm not myself, you see."

"I don't see," said the Caterpillar.


DRINK ME patches the Parser to translate uses of 'each' into 'each: aMessage' and encodes everything to right of 'each' as a literal Message object.



Comments


Hans-Martin Mosner thinks that hacking the Parser is not really necessary. Just create a class Each:

nil subclass: #Each
instanceVariableNames: 'collection'
...

Then add two methods to Each:
doesNotUnderstand: aMessage
^collection collect: [:each | each perform: aMessage selector withArguments: aMessage arguments]

collection: aCollection
collection := aCollection

And one method to Collection:
each
^Each new collection: self

There you are - works as intended :-)

I've just written it down in VA Smalltalk, as I don't have Squeak here at work, and when I inspect
'abc' each
, I get three inspectors. Not exactly what I expected, but funny nevertheless...

David Farber responds:

Hans, I want to be able to do more that just
'abc' each
. I also want to be able to do
0 @ #(1 2 3) each
which should give you three points, 0@1, 0@2, and 0@3. You can't do this just by defining a method on Collection.

Marcel Weiher notes:

Actually, that is exactly one of the things HOM brings to the table: it can iterate over any combination of parameters to a message.

Your example looks like this:
 0 collect @ #(1 2 3) each 
.