Hi, From time to time, I need to perform some additional action (side effect) when a boolean is true, but preserve that boolean as return value. Here is a pseudo example: self findLast: [:each | | conforming |
conforming
:= self testSomeConditionFor: each.
conforming ifTrue: [
"found one, do some side effect before searching next" self performSomeAction: each ].
conforming
]. Or shorter, but I dislike the redundant true:
self findLast: [:each | (self testSomeConditionFor: each) and: [
"found one, do some side effect before searching next" self performSomeAction: each. true ]]. What i seek is even shorter:
self findLast: [:each | (self testSomeConditionFor: each) whenTrue: [
"found one, do some side effect before searching next" self performSomeAction: each ]]. Of course, we would have companions whenFalse:, whenTrue:whenFalse:... Would you buy it? |
Oups, my example was a bit stupid because findLast: loop stops at first true, but you get my intention... Replace with select:/reject: or provide a whenFalse: side effect... Le mar. 14 mai 2019 à 16:23, Nicolas Cellier <[hidden email]> a écrit :
|
In reply to this post by Nicolas Cellier
On 14/05/19 7:53 PM, Nicolas Cellier wrote:
> Hi, > From time to time, I need to perform some additional action (side > effect) when a boolean is true, but preserve that boolean as return value. Do you mean something like: Boolean>>whenTrue: aBlock self ifTrue: aBlock. ^self Yes, this would be useful in creating parsers. Regards .. Subbu |
In reply to this post by Nicolas Cellier
> Of course, we would have companions whenFalse:, whenTrue:whenFalse:...
+1 Stef |
Hi Nicolas, I understand your concern. However, (1) #whenTrue: suggests that there will be eventually a true case ("when" vs. "if") and (2) it's too close to #ifTrue: and could be used too often but maybe not optimized. :-) Also, I would need this for #detect: and #detect:ifNone:, too. :-D Best, Marcel
|
In reply to this post by Nicolas Cellier
Salut Nico, please excuse if my squeak knowlege is a bit rusty, but wouldn't this do the job: self findlast: [:each | [self testSomeConditionFor: each] value ifTrue:[self performSomeAction:each]; yourself. ]. Just an idea. And very sad that I have to wrap the condition in a
block, but I guess othewise the recipient for "yourself" would be
"self" On 14.05.19 17:02, Nicolas Cellier
wrote:
|
Hi, Ah yes, it works, no need for extra [] value (3 > 2) ifTrue: [Transcript cr; show: 'bingo!']; yourself The code is not optimized, but it works. Le jeu. 16 mai 2019 à 16:52, Torge Husfeldt <[hidden email]> a écrit :
|
Hi Nico, I'm convinced this only works without the extra [] value because you chose a special selector. For keyword-selector it should go to "self". Which part exactly is not optimized? The "ifTrue:" has a special handling in the bytecode complier, and iirc yourself is also not "actually sent". Cdlt Torge On 16.05.19 17:08, Nicolas Cellier
wrote:
|
This also works:
('isBoolean' beginsWith: 'is') ifTrue: [Display flash: World bounds]; yourself Regards .. Subbu On 16/05/19 8:43 PM, Torge Husfeldt wrote: > Hi Nico, > > I'm convinced this only works without the extra [] value because you > chose a special selector. For keyword-selector it should go to "self". > > Which part exactly is not optimized? The "ifTrue:" has a special > handling in the bytecode complier, and iirc yourself is also not > "actually sent". > > Cdlt > > Torge > > On 16.05.19 17:08, Nicolas Cellier wrote: >> Hi, >> Ah yes, it works, no need for extra [] value >> >> (3 > 2) ifTrue: [Transcript cr; show: 'bingo!']; yourself >> >> The code is not optimized, but it works. >> >> Le jeu. 16 mai 2019 à 16:52, Torge Husfeldt <[hidden email] >> <mailto:[hidden email]>> a écrit : >> >> Salut Nico, >> >> please excuse if my squeak knowlege is a bit rusty, but wouldn't >> this do the job: >> >> self findlast: [:each | >> >> [self testSomeConditionFor: each] value ifTrue:[self >> performSomeAction:each]; yourself. >> >> ]. >> >> Just an idea. And very sad that I have to wrap the condition in a >> block, but I guess othewise the recipient for "yourself" would be >> "self" >> >> On 14.05.19 17:02, Nicolas Cellier wrote: >>> Oups, my example was a bit stupid because findLast: loop stops at >>> first true, but you get my intention... >>> Replace with select:/reject: or provide a whenFalse: side effect... >>> >>> Le mar. 14 mai 2019 à 16:23, Nicolas Cellier >>> <[hidden email] >>> <mailto:[hidden email]>> a écrit : >>> >>> Hi, >>> From time to time, I need to perform some additional action >>> (side effect) when a boolean is true, but preserve that >>> boolean as return value. >>> >>> Here is a pseudo example: >>> >>> self findLast: [:each | >>> | conforming | >>> conforming := self testSomeConditionFor: each. >>> conforming >>> ifTrue: >>> [ "found one, do some side effect before >>> searching next" >>> self performSomeAction: each ]. >>> conforming ]. >>> >>> Or shorter, but I dislike the redundant true: >>> >>> self findLast: [:each | >>> (self testSomeConditionFor: each) >>> and: >>> [ "found one, do some side effect before >>> searching next" >>> self performSomeAction: each. >>> true ]]. >>> >>> What i seek is even shorter: >>> >>> self findLast: [:each | >>> (self testSomeConditionFor: each) >>> whenTrue: >>> [ "found one, do some side effect before >>> searching next" >>> self performSomeAction: each ]]. >>> >>> Of course, we would have companions whenFalse:, >>> whenTrue:whenFalse:... >>> Would you buy it? >>> >>> >>> >> >> > > |
Hi Subbu,
see, I forgot about the good old simple parantheses. As I said, rusty. I’d say, if we don’t reach unanimity regarding the new selectors, this shortcut should fit the bill for your initial request? Von meinem iPhone gesendet > Am 17.05.2019 um 13:02 schrieb K K Subbu <[hidden email]>: > > This also works: > > ('isBoolean' beginsWith: 'is') ifTrue: [Display flash: World bounds]; yourself > > Regards .. Subbu > >> On 16/05/19 8:43 PM, Torge Husfeldt wrote: >> Hi Nico, >> I'm convinced this only works without the extra [] value because you chose a special selector. For keyword-selector it should go to "self". >> Which part exactly is not optimized? The "ifTrue:" has a special handling in the bytecode complier, and iirc yourself is also not "actually sent". >> Cdlt >> Torge >>> On 16.05.19 17:08, Nicolas Cellier wrote: >>> Hi, >>> Ah yes, it works, no need for extra [] value >>> >>> (3 > 2) ifTrue: [Transcript cr; show: 'bingo!']; yourself >>> >>> The code is not optimized, but it works. >>> >>> Le jeu. 16 mai 2019 à 16:52, Torge Husfeldt <[hidden email] <mailto:[hidden email]>> a écrit : >>> >>> Salut Nico, >>> >>> please excuse if my squeak knowlege is a bit rusty, but wouldn't >>> this do the job: >>> >>> self findlast: [:each | >>> >>> [self testSomeConditionFor: each] value ifTrue:[self >>> performSomeAction:each]; yourself. >>> >>> ]. >>> >>> Just an idea. And very sad that I have to wrap the condition in a >>> block, but I guess othewise the recipient for "yourself" would be >>> "self" >>> >>>> On 14.05.19 17:02, Nicolas Cellier wrote: >>>> Oups, my example was a bit stupid because findLast: loop stops at >>>> first true, but you get my intention... >>>> Replace with select:/reject: or provide a whenFalse: side effect... >>>> >>>> Le mar. 14 mai 2019 à 16:23, Nicolas Cellier >>>> <[hidden email] >>>> <mailto:[hidden email]>> a écrit : >>>> >>>> Hi, >>>> From time to time, I need to perform some additional action >>>> (side effect) when a boolean is true, but preserve that >>>> boolean as return value. >>>> >>>> Here is a pseudo example: >>>> >>>> self findLast: [:each | >>>> | conforming | >>>> conforming := self testSomeConditionFor: each. >>>> conforming >>>> ifTrue: >>>> [ "found one, do some side effect before >>>> searching next" >>>> self performSomeAction: each ]. >>>> conforming ]. >>>> >>>> Or shorter, but I dislike the redundant true: >>>> >>>> self findLast: [:each | >>>> (self testSomeConditionFor: each) >>>> and: >>>> [ "found one, do some side effect before >>>> searching next" >>>> self performSomeAction: each. >>>> true ]]. >>>> >>>> What i seek is even shorter: >>>> >>>> self findLast: [:each | >>>> (self testSomeConditionFor: each) >>>> whenTrue: >>>> [ "found one, do some side effect before >>>> searching next" >>>> self performSomeAction: each ]]. >>>> >>>> Of course, we would have companions whenFalse:, >>>> whenTrue:whenFalse:... >>>> Would you buy it? >>>> >>>> >>>> >>> >>> > |
Hi Torge, considering that I'm probably the one who made cascading work for inlined messages, I'm happy enough with your suggestion ;) I thought I once proposed some Compiler changes to make the cacaded inlined message still optimized, but I can't find them anymore. It was rejected as overkill (two many changes for so few value) Le ven. 17 mai 2019 à 18:08, Torge Husfeldt <[hidden email]> a écrit : Hi Subbu, |
Le ven. 17 mai 2019 à 19:20, Nicolas Cellier <[hidden email]> a écrit :
if couse, it was not two, but many more, too many
|
http://source.squeak.org/trunk/Compiler-nice.189.diff with the help of Eliot :) Le ven. 17 mai 2019 à 19:32, Nicolas Cellier <[hidden email]> a écrit :
|
Héhé, I found older https://marc.info/?l=squeak-dev&m=123179608119480&w=2 Le ven. 17 mai 2019 à 19:47, Nicolas Cellier <[hidden email]> a écrit :
|
Free forum by Nabble | Edit this page |