Collection>>select: aBlock ifNoneTakeAsDefault: aDefaultValue

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

Collection>>select: aBlock ifNoneTakeAsDefault: aDefaultValue

Bart Gauquie
Dear all,

While developing some code, I made following extension of the Collection protocol: 

select: aBlock ifNoneTakeAsDefault: aDefaultValue
   |result|
   result := self select: aBlock.
   (result isEmpty)
      ifTrue: [^self class with: aDefaultValue]
      ifFalse: [^result].

Maybe this could be added to the Pharo 1.1 Collection clazz? Or do we want to keep the interface of Collection as small as possible (which is also a good idea!).

Kind Regards,

Bart

--
imagination is more important than knowledge - Albert Einstein
Logic will get you from A to B. Imagination will take you everywhere - Albert Einstein
Learn from yesterday, live for today, hope for tomorrow. The important thing is not to stop questioning. - Albert Einstein
The true sign of intelligence is not knowledge but imagination. - Albert Einstein
Gravitation is not responsible for people falling in love. - Albert Einstein

_______________________________________________
Pharo-project mailing list
[hidden email]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
Reply | Threaded
Open this post in threaded view
|

Re: Collection>>select: aBlock ifNoneTakeAsDefault: aDefaultValue

Julian Fitzell-2
Personally, I would rather see #ifEmpty: modified to return self in
the false case like #ifNil: does. The only in-image sender I can find
that even uses the return value seems to actually expect this
behaviour anyway. :)

Then you could simply write:

(aCollection select: [:ea | ...]) ifEmpty: [ ... ]

Julian

2009/12/26 Bart Gauquie <[hidden email]>:

> Dear all,
>
> While developing some code, I made following extension of the Collection
> protocol:
> select: aBlock ifNoneTakeAsDefault: aDefaultValue
>    |result|
>    result := self select: aBlock.
>    (result isEmpty)
>       ifTrue: [^self class with: aDefaultValue]
>       ifFalse: [^result].
>
> Maybe this could be added to the Pharo 1.1 Collection clazz? Or do we want
> to keep the interface of Collection as small as possible (which is also a
> good idea!).
> Kind Regards,
> Bart
> --
> imagination is more important than knowledge - Albert Einstein
> Logic will get you from A to B. Imagination will take you everywhere -
> Albert Einstein
> Learn from yesterday, live for today, hope for tomorrow. The important thing
> is not to stop questioning. - Albert Einstein
> The true sign of intelligence is not knowledge but imagination. - Albert
> Einstein
> Gravitation is not responsible for people falling in love. - Albert Einstein
>
> _______________________________________________
> Pharo-project mailing list
> [hidden email]
> http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
>

_______________________________________________
Pharo-project mailing list
[hidden email]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
Reply | Threaded
Open this post in threaded view
|

Re: Collection>>select: aBlock ifNoneTakeAsDefault: aDefaultValue

Bart Gauquie
Hi,

that seems like a very good suggestion. Is more general usable also.

Regards,

Bart

On Sat, Dec 26, 2009 at 11:44 PM, Julian Fitzell <[hidden email]> wrote:
Personally, I would rather see #ifEmpty: modified to return self in
the false case like #ifNil: does. The only in-image sender I can find
that even uses the return value seems to actually expect this
behaviour anyway. :)

Then you could simply write:

(aCollection select: [:ea | ...]) ifEmpty: [ ... ]

Julian

2009/12/26 Bart Gauquie <[hidden email]>:
> Dear all,
>
> While developing some code, I made following extension of the Collection
> protocol:
> select: aBlock ifNoneTakeAsDefault: aDefaultValue
>    |result|
>    result := self select: aBlock.
>    (result isEmpty)
>       ifTrue: [^self class with: aDefaultValue]
>       ifFalse: [^result].
>
> Maybe this could be added to the Pharo 1.1 Collection clazz? Or do we want
> to keep the interface of Collection as small as possible (which is also a
> good idea!).
> Kind Regards,
> Bart
> --
> imagination is more important than knowledge - Albert Einstein
> Logic will get you from A to B. Imagination will take you everywhere -
> Albert Einstein
> Learn from yesterday, live for today, hope for tomorrow. The important thing
> is not to stop questioning. - Albert Einstein
> The true sign of intelligence is not knowledge but imagination. - Albert
> Einstein
> Gravitation is not responsible for people falling in love. - Albert Einstein
>
> _______________________________________________
> Pharo-project mailing list
> [hidden email]
> http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
>

_______________________________________________
Pharo-project mailing list
[hidden email]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project



--
imagination is more important than knowledge - Albert Einstein
Logic will get you from A to B. Imagination will take you everywhere - Albert Einstein
Learn from yesterday, live for today, hope for tomorrow. The important thing is not to stop questioning. - Albert Einstein
The true sign of intelligence is not knowledge but imagination. - Albert Einstein
Gravitation is not responsible for people falling in love. - Albert Einstein

_______________________________________________
Pharo-project mailing list
[hidden email]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
Reply | Threaded
Open this post in threaded view
|

Re: Collection>>select: aBlock ifNoneTakeAsDefault: aDefaultValue

Stéphane Ducasse
In reply to this post by Julian Fitzell-2

On Dec 26, 2009, at 11:44 PM, Julian Fitzell wrote:

> Personally, I would rather see #ifEmpty: modified to return self in
> the false case like #ifNil: does. The only in-image sender I can find
> that even uses the return value seems to actually expect this
> behaviour anyway. :)

what is the behavior in other dialects ? :)
Do you have some nice tests to cover the semantics?
Did you do the change and see what break?

Stef

>
> Then you could simply write:
>
> (aCollection select: [:ea | ...]) ifEmpty: [ ... ]
>
> Julian
>
> 2009/12/26 Bart Gauquie <[hidden email]>:
>> Dear all,
>>
>> While developing some code, I made following extension of the Collection
>> protocol:
>> select: aBlock ifNoneTakeAsDefault: aDefaultValue
>>    |result|
>>    result := self select: aBlock.
>>    (result isEmpty)
>>       ifTrue: [^self class with: aDefaultValue]
>>       ifFalse: [^result].
>>
>> Maybe this could be added to the Pharo 1.1 Collection clazz? Or do we want
>> to keep the interface of Collection as small as possible (which is also a
>> good idea!).
>> Kind Regards,
>> Bart
>> --
>> imagination is more important than knowledge - Albert Einstein
>> Logic will get you from A to B. Imagination will take you everywhere -
>> Albert Einstein
>> Learn from yesterday, live for today, hope for tomorrow. The important thing
>> is not to stop questioning. - Albert Einstein
>> The true sign of intelligence is not knowledge but imagination. - Albert
>> Einstein
>> Gravitation is not responsible for people falling in love. - Albert Einstein
>>
>> _______________________________________________
>> Pharo-project mailing list
>> [hidden email]
>> http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
>>
>
> _______________________________________________
> Pharo-project mailing list
> [hidden email]
> http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project


_______________________________________________
Pharo-project mailing list
[hidden email]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
Reply | Threaded
Open this post in threaded view
|

Re: Collection>>select: aBlock ifNoneTakeAsDefault: aDefaultValue

Stéphane Ducasse
In reply to this post by Julian Fitzell-2
I forgot open a bug tracker entry because we should not lose the suggestion.

On Dec 26, 2009, at 11:44 PM, Julian Fitzell wrote:

> Personally, I would rather see #ifEmpty: modified to return self in
> the false case like #ifNil: does. The only in-image sender I can find
> that even uses the return value seems to actually expect this
> behaviour anyway. :)
>
> Then you could simply write:
>
> (aCollection select: [:ea | ...]) ifEmpty: [ ... ]
>
> Julian
>
> 2009/12/26 Bart Gauquie <[hidden email]>:
>> Dear all,
>>
>> While developing some code, I made following extension of the Collection
>> protocol:
>> select: aBlock ifNoneTakeAsDefault: aDefaultValue
>>    |result|
>>    result := self select: aBlock.
>>    (result isEmpty)
>>       ifTrue: [^self class with: aDefaultValue]
>>       ifFalse: [^result].
>>
>> Maybe this could be added to the Pharo 1.1 Collection clazz? Or do we want
>> to keep the interface of Collection as small as possible (which is also a
>> good idea!).
>> Kind Regards,
>> Bart
>> --
>> imagination is more important than knowledge - Albert Einstein
>> Logic will get you from A to B. Imagination will take you everywhere -
>> Albert Einstein
>> Learn from yesterday, live for today, hope for tomorrow. The important thing
>> is not to stop questioning. - Albert Einstein
>> The true sign of intelligence is not knowledge but imagination. - Albert
>> Einstein
>> Gravitation is not responsible for people falling in love. - Albert Einstein
>>
>> _______________________________________________
>> Pharo-project mailing list
>> [hidden email]
>> http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
>>
>
> _______________________________________________
> Pharo-project mailing list
> [hidden email]
> http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project


_______________________________________________
Pharo-project mailing list
[hidden email]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
Reply | Threaded
Open this post in threaded view
|

Re: Collection>>select: aBlock ifNoneTakeAsDefault: aDefaultValue

Lukas Renggli
In reply to this post by Stéphane Ducasse
>> Personally, I would rather see #ifEmpty: modified to return self in
>> the false case like #ifNil: does. The only in-image sender I can find
>> that even uses the return value seems to actually expect this
>> behaviour anyway. :)

+1

> what is the behavior in other dialects ? :)
> Do you have some nice tests to cover the semantics?
> Did you do the change and see what break?

I don't think anybody else has this method.

Lukas

--
Lukas Renggli
http://www.lukas-renggli.ch

_______________________________________________
Pharo-project mailing list
[hidden email]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
Reply | Threaded
Open this post in threaded view
|

Re: Collection>>select: aBlock ifNoneTakeAsDefault: aDefaultValue

Mariano Martinez Peck
In reply to this post by Julian Fitzell-2


On Sat, Dec 26, 2009 at 11:44 PM, Julian Fitzell <[hidden email]> wrote:
Personally, I would rather see #ifEmpty: modified to return self in
the false case like #ifNil: does. The only in-image sender I can find
that even uses the return value seems to actually expect this
behaviour anyway. :)


Julian: we (stef and I) have something we don't understand. The implementation of ifEmpty: is like this in Pharo:

Collection >> ifEmpty: aBlock
    "Evaluate the block if I'm empty"

    self isEmpty ifTrue: [ ^aBlock value ]


In this case, in the false case, nothing is expressively returned, so self is returned.

Did I miss something ?

cheers

mariano
 
Then you could simply write:

(aCollection select: [:ea | ...]) ifEmpty: [ ... ]

Julian

2009/12/26 Bart Gauquie <[hidden email]>:
> Dear all,
>
> While developing some code, I made following extension of the Collection
> protocol:
> select: aBlock ifNoneTakeAsDefault: aDefaultValue
>    |result|
>    result := self select: aBlock.
>    (result isEmpty)
>       ifTrue: [^self class with: aDefaultValue]
>       ifFalse: [^result].
>
> Maybe this could be added to the Pharo 1.1 Collection clazz? Or do we want
> to keep the interface of Collection as small as possible (which is also a
> good idea!).
> Kind Regards,
> Bart
> --
> imagination is more important than knowledge - Albert Einstein
> Logic will get you from A to B. Imagination will take you everywhere -
> Albert Einstein
> Learn from yesterday, live for today, hope for tomorrow. The important thing
> is not to stop questioning. - Albert Einstein
> The true sign of intelligence is not knowledge but imagination. - Albert
> Einstein
> Gravitation is not responsible for people falling in love. - Albert Einstein
>
> _______________________________________________
> Pharo-project mailing list
> [hidden email]
> http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
>

_______________________________________________
Pharo-project mailing list
[hidden email]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project


_______________________________________________
Pharo-project mailing list
[hidden email]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
Reply | Threaded
Open this post in threaded view
|

Re: Collection>>select: aBlock ifNoneTakeAsDefault: aDefaultValue

Lukas Renggli
> Julian: we (stef and I) have something we don't understand. The
> implementation of ifEmpty: is like this in Pharo:
>
> Collection >> ifEmpty: aBlock
>     "Evaluate the block if I'm empty"
>
>     self isEmpty ifTrue: [ ^aBlock value ]

In Pharo 1.0rc1 it looks like this:

Collection>>ifEmpty: aBlock
        "Evaluate the block if I'm empty"

        ^ self isEmpty ifTrue: aBlock

Lukas

--
Lukas Renggli
http://www.lukas-renggli.ch

_______________________________________________
Pharo-project mailing list
[hidden email]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
Reply | Threaded
Open this post in threaded view
|

Re: Collection>>select: aBlock ifNoneTakeAsDefault: aDefaultValue

Nicolas Cellier
2009/12/27 Lukas Renggli <[hidden email]>:

>> Julian: we (stef and I) have something we don't understand. The
>> implementation of ifEmpty: is like this in Pharo:
>>
>> Collection >> ifEmpty: aBlock
>>     "Evaluate the block if I'm empty"
>>
>>     self isEmpty ifTrue: [ ^aBlock value ]
>
> In Pharo 1.0rc1 it looks like this:
>
> Collection>>ifEmpty: aBlock
>        "Evaluate the block if I'm empty"
>
>        ^ self isEmpty ifTrue: aBlock
>
> Lukas
>

It sounds stupid to return nil if not empty.
Should the change in 1.1 be backported ?

Nicolas

> --
> Lukas Renggli
> http://www.lukas-renggli.ch
>
> _______________________________________________
> Pharo-project mailing list
> [hidden email]
> http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
>

_______________________________________________
Pharo-project mailing list
[hidden email]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
Reply | Threaded
Open this post in threaded view
|

Re: Collection>>select: aBlock ifNoneTakeAsDefault: aDefaultValue

Stéphane Ducasse
The question is how much will we have to backport.
Because we do not have that horse power to do that for each little details.
I would only backport really important fixes.

Stef


On Dec 27, 2009, at 7:58 PM, Nicolas Cellier wrote:

> 2009/12/27 Lukas Renggli <[hidden email]>:
>>> Julian: we (stef and I) have something we don't understand. The
>>> implementation of ifEmpty: is like this in Pharo:
>>>
>>> Collection >> ifEmpty: aBlock
>>>     "Evaluate the block if I'm empty"
>>>
>>>     self isEmpty ifTrue: [ ^aBlock value ]
>>
>> In Pharo 1.0rc1 it looks like this:
>>
>> Collection>>ifEmpty: aBlock
>>        "Evaluate the block if I'm empty"
>>
>>        ^ self isEmpty ifTrue: aBlock
>>
>> Lukas
>>
>
> It sounds stupid to return nil if not empty.
> Should the change in 1.1 be backported ?
>
> Nicolas
>
>> --
>> Lukas Renggli
>> http://www.lukas-renggli.ch
>>
>> _______________________________________________
>> Pharo-project mailing list
>> [hidden email]
>> http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
>>
>
> _______________________________________________
> Pharo-project mailing list
> [hidden email]
> http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project


_______________________________________________
Pharo-project mailing list
[hidden email]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project