[Pharo-project] Collection>>select: aBlock ifNoneTakeAsDefault: aDefaultValue
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
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 <bart.gauquie@gmail.com>:
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 Pharo-project@lists.gforge.inria.fr http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
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 <jfitzell@gmail.com> 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 <bart.gauquie@gmail.com>:
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 Pharo-project@lists.gforge.inria.fr http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
_______________________________________________ Pharo-project mailing list Pharo-project@lists.gforge.inria.fr 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
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 <bart.gauquie@gmail.com>:
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 Pharo-project@lists.gforge.inria.fr http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
_______________________________________________ Pharo-project mailing list Pharo-project@lists.gforge.inria.fr http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
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
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 <bart.gauquie@gmail.com>:
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 Pharo-project@lists.gforge.inria.fr http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
_______________________________________________ Pharo-project mailing list Pharo-project@lists.gforge.inria.fr http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
On Sat, Dec 26, 2009 at 11:44 PM, Julian Fitzell <jfitzell@gmail.com> 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 <bart.gauquie@gmail.com>:
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 Pharo-project@lists.gforge.inria.fr http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
_______________________________________________ Pharo-project mailing list Pharo-project@lists.gforge.inria.fr http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
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
2009/12/27 Lukas Renggli <renggli@gmail.com>:
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 Pharo-project@lists.gforge.inria.fr http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
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 <renggli@gmail.com>:
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 Pharo-project@lists.gforge.inria.fr http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
_______________________________________________ Pharo-project mailing list Pharo-project@lists.gforge.inria.fr http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
participants (6)
-
Bart Gauquie -
Julian Fitzell -
Lukas Renggli -
Mariano Martinez Peck -
Nicolas Cellier -
Stéphane Ducasse