orMoreDo:

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

orMoreDo:

Stéphane Rollandin
Hello,


I just wanted to share the following method, which comes in handy when
one wants a receiver to be considered always as a collection, possibly
by lifting it as a singleton (reminds you of something, Chris ?)

Object>>orMoreDo: aBlock
        ^ aBlock value: self

Collection>>orMoreDo: aBlock
        ^ self do: aBlock


example:

4 orMoreDo: [:x | Transcript show: x + 1]
(4 to: 7) orMoreDo: [:x | Transcript show: x + 1]


I don't know if this is generally useful; it has proven useful to me at
least...


Stef

Reply | Threaded
Open this post in threaded view
|

Re: orMoreDo:

Stéphane Rollandin
I forgot this part of my implementation:

Symbol>>orMoreDo: aBlock
        ^ aBlock value: self

(else a Symbol is considered as a collection of characters...)

Stef

Reply | Threaded
Open this post in threaded view
|

Re: orMoreDo:

Chris Muller-3
In reply to this post by Stéphane Rollandin
How dare you!?   ;-)

2010/12/9 Stéphane Rollandin <[hidden email]>:

> Hello,
>
>
> I just wanted to share the following method, which comes in handy when one
> wants a receiver to be considered always as a collection, possibly by
> lifting it as a singleton (reminds you of something, Chris ?)
>
> Object>>orMoreDo: aBlock
>        ^ aBlock value: self
>
> Collection>>orMoreDo: aBlock
>        ^ self do: aBlock
>
>
> example:
>
> 4 orMoreDo: [:x | Transcript show: x + 1]
> (4 to: 7) orMoreDo: [:x | Transcript show: x + 1]
>
>
> I don't know if this is generally useful; it has proven useful to me at
> least...
>
>
> Stef
>
>