Collection ifEmpty:

Damien Cassou damien.cassou at gmail.com
Wed Mar 21 08:41:45 UTC 2007


Please fill in a bug report in bugs.squeak.org

2007/3/21, brad fowlow <fowlow at pacbell.net>:
>
> Howdy folks.
>
> A bit of collection trivia...
>
> Collection ifEmpty: [ foo ]
> returns nil if the collection is not empty,
> owing to the placement of the return in
>
>         ifEmpty: aBlock
>                 "Evaluate the block if I'm empty"
>                 ^ self isEmpty ifTrue: aBlock
>
>
> It would be a tad less efficient,
> but (I think) more useful to say
>         ifEmpty: aBlock
>                 self isEmpty ifTrue: [ ^ aBlock value ]
>
> returning the original collection if non-empty,
> ... since this allows expressions such as
>
>         foo := (collection allAfterLast:'@') ifEmpty: [collection].
>
>         This will only work if you add a cumbersome ifNotEmpty:[:c|c]
>          to the end, to force the result value to be the non-empty
> allAfterLast:
>
> My Squeak (3.8.something, a Croquet image)
> has 18 uses of isEmpty:,  none of them using the result.
>
> Of course it's very unlikely anyone would arrange
> a platform change of this sort for such a trivial thing.
> But the ifEmpty: methods appear to be of later vintage
> than a lot of the core of Collection.
>
> And usually, I find that the methods in the basic system
> are well thought out to suit a wide range of use patterns;
> this seemed like an odd exception.
>
> So... a question for the tidy expression fans....
> what's a nice tidy ordered-collection expression for
>         everything after x, unless there is no x, in which case all
> since I'm probably just looking into my blind spot,
> overlooking something obvious.
>
> Regards,
> brad
>
>


-- 
Damien Cassou



More information about the Squeak-dev mailing list