[ANSI-Smalltalk] Re: A list of interesting methods, ifNotNil:
Richard Sargent
rsargent at 5x5.on.ca
Sat Sep 6 13:53:03 BST 2008
Paolo Bonzini bonzini at gnu.org wrote:
>> x ifEmpty: block1 ifNotEmptyDo: block2
>>
>> And I say *could*, I'm not endorsing this particular addition -- I'm
>> just using to show why IMO using the "do" suffix to choose between zero-
>> and one-arity blocks is bad.
A while ago, I had occasion to implement something like this. I guess you
could call it a reification of the "0, 1, infinity" rule that P. J. Plauger
described in Software Development or some other magazine nearly 20 years
ago.
zeroDo: emptyHandler oneDo: singularHandler multipleDo: multipleHandler
"Answer the result of evaluating the appropriate handler for the
cardinality of the receiver.
Each handler is a Block.
The emptyHandler takes no arguments.
The singularHandler takes an optional argument, the single element
of the collection.
The multipleHandler takes an optional argument, the collection
itself."
self isEmpty ifTrue: [^emptyHandler value].
self isMultiple ifTrue: [^multipleHandler valueWithOptionalArgument:
self].
^singularHandler valueWithOptionalArgument: self asArray first
Richard Sargent
rsargent at 5x5.on.ca
http://www.pendragonfarm.com/
More information about the ANSI-Smalltalk
mailing list