Case statement and lazy comparison in Pharo

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

Case statement and lazy comparison in Pharo

gerard alis
Well, perhaps is a theme worked in another times but... is possible for Pharo have a basic Case or elseIf statement? I know is easy create you own structure control, but not is more useful have a "standard" for everybody?  I´m tired of write code like that...

                        (self currentRow == sortedRows last and: [ self currentCell isNil ]) ifTrue:
                        [
                                self navigationKey: event
                        ]
                        ifFalse:
                        [
                                (self currentRow notNil and: [ self currentCell isNil ]) ifTrue:
                                [
                                        self setCurrentRowToNext.
                                ]
                                ifFalse:
                                [
                                        (self currentRow notNil and: [ self currentCell notNil ]) ifTrue:
                                        [
                                                self setCurrentCellToNext.
                                               
                                                self currentCell notNil ifTrue:
                                                [
                                                        self currentCell performKeyFocus: event inCellBounds: (self pvtGetCellBounds: self currentCell).
                                                ].
                                        ].
                                ].
                        ].


Write code with that format is pathetical :(

Is valid too have a "and" and "or" lazy? Exists a not lazy with #& and #|  , but could exists an #&& and #||  . Is more easy...

      value1 == value2 and:[ <condition> ] and: [<condition>] ......

or

     value1 == value2 && <condition> && <condition> ......... ???

Well, perhaps is a stupid question but I miss a more complete way for write code. If in Smalltalk is possible do easy that and include it in "core" why not do it?

Is a reasonable desire :)

Regards
     




       
Reply | Threaded
Open this post in threaded view
|

Re: Case statement and lazy comparison in Pharo

Levente Uzonyi-2
On Mon, 15 Mar 2010, nullPointer wrote:

>
Well, perhaps is a theme worked in another times but... is possible for Pharo
have a basic Case or elseIf statement? I know is easy create you own
structure control, but not is more useful have a "standard" for everybody?
I´m tired of write code like that...

                         (self currentRow == sortedRows last and: [ self
currentCell isNil ]) ifTrue:
  [
  self navigationKey: event
  ]
  ifFalse:
  [
  (self currentRow notNil and: [ self currentCell isNil ]) ifTrue:
  [
  self setCurrentRowToNext.
  ]
  ifFalse:
  [
  (self currentRow notNil and: [ self currentCell notNil ]) ifTrue:
  [
  self setCurrentCellToNext.

  self currentCell notNil ifTrue:
  [
  self currentCell performKeyFocus: event inCellBounds: (self
pvtGetCellBounds: self currentCell).
  ].
  ].
  ].
  ].


You can save 2-3 lines/branch if you format your code with a better style:

foo
  ifTrue: [ ... ]
  ifFalse: [
  bar
  ifTrue: [ ... ]
  ifFalse: [
  baz
  ... ] ]

If you make this branch a separate method you can use returns making it
even simpler:

foo ifTrue: [ ^... ]
bar ifTrue: [ ^... ]
baz ifTrue: [ ^... ]
...

And there's #caseOf:otherwise: for a case-like pseudostatemet. Note that
this is inlined by the compiler.

foobar
  caseOf: {
  [ ... ] -> [ ... ].
  [ ... ] -> [ ... ].
  ... }
  otherwise: [ ... ]


> Write code with that format is pathetical :(

Is valid too have a "and" and "or" lazy? Exists a not lazy with #& and #|  ,
but could exists an #&& and #||  . Is more easy...

       value1 == value2 and:[ <condition> ] and: [<condition>] ......

or

      value1 == value2 && <condition> && <condition> ......... ???


#and: and #or: is short-circuit (lazy) by default.


> Well, perhaps is a stupid question but I miss a more complete way for
write
code. If in Smalltalk is possible do easy that and include it in "core" why
not do it?


Soon I'll start collecting credits for promoting this book:
http://stephane.ducasse.free.fr/FreeBooks/WithStyle/SmalltalkWithStyle.pdf 
:)


Levente


Is a reasonable desire :)

Regards






--
View this message in context: http://n4.nabble.com/Case-statement-and-lazy-comparison-in-Pharo-tp1594080p1594080.html
Sent from the Pharo Smalltalk mailing list archive at Nabble.com.

_______________________________________________
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: Case statement and lazy comparison in Pharo

Michael Roberts-2
In reply to this post by gerard alis
I would try and reduce the number of branches first. you can use guard
conditions to exit early.

This is not necessarily exact below, but you hopefully get the idea...

(self currentRow == sortedRows last and: [self currentCell isNil])
        ifTrue: [^self navigationKey: event]

self currentRow ifNil: [^self].

self currentCell ifNil:
        [^self setCurrentRowToNext]

self setCurrentCellToNext.

self currentCell ifNil: [^self].
       
self currentCell performKeyFocus: event inCellBounds:
        (self pvtGetCellBounds: self currentCell)


A different approach entirely is to have an object(s) that represents
the nil state for the row and cell. That way you are not checking
constantly for it being nil, and perhaps you can dispatch some
behaviour to it.

cheers,
Mike

On Mon, Mar 15, 2010 at 9:49 PM, nullPointer <[hidden email]> wrote:

>
> Well, perhaps is a theme worked in another times but... is possible for Pharo
> have a basic Case or elseIf statement? I know is easy create you own
> structure control, but not is more useful have a "standard" for everybody?
> I´m tired of write code like that...
>
>                        (self currentRow == sortedRows last and: [ self
> currentCell isNil ]) ifTrue:
>                        [
>                                self navigationKey: event
>                        ]
>                        ifFalse:
>                        [
>                                (self currentRow notNil and: [ self currentCell isNil ]) ifTrue:
>                                [
>                                        self setCurrentRowToNext.
>                                ]
>                                ifFalse:
>                                [
>                                        (self currentRow notNil and: [ self currentCell notNil ]) ifTrue:
>                                        [
>                                                self setCurrentCellToNext.
>
>                                                self currentCell notNil ifTrue:
>                                                [
>                                                        self currentCell performKeyFocus: event inCellBounds: (self
> pvtGetCellBounds: self currentCell).
>                                                ].
>                                        ].
>                                ].
>                        ].
>
>
> Write code with that format is pathetical :(
>
> Is valid too have a "and" and "or" lazy? Exists a not lazy with #& and #|  ,
> but could exists an #&& and #||  . Is more easy...
>
>      value1 == value2 and:[ <condition> ] and: [<condition>] ......
>
> or
>
>     value1 == value2 && <condition> && <condition> ......... ???
>
> Well, perhaps is a stupid question but I miss a more complete way for write
> code. If in Smalltalk is possible do easy that and include it in "core" why
> not do it?
>
> Is a reasonable desire :)
>
> Regards
>
>
>
>
>
>
> --
> View this message in context: http://n4.nabble.com/Case-statement-and-lazy-comparison-in-Pharo-tp1594080p1594080.html
> Sent from the Pharo Smalltalk mailing list archive at Nabble.com.
>
> _______________________________________________
> 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: Case statement and lazy comparison in Pharo

Henrik Sperre Johansen
In reply to this post by gerard alis
  On 15.03.2010 22:49, nullPointer wrote:

> Well, perhaps is a theme worked in another times but... is possible for Pharo
> have a basic Case or elseIf statement? I know is easy create you own
> structure control, but not is more useful have a "standard" for everybody?
> I´m tired of write code like that...
>
>                          (self currentRow == sortedRows last and: [ self
> currentCell isNil ]) ifTrue:
> [
> self navigationKey: event
> ]
> ifFalse:
> [
> (self currentRow notNil and: [ self currentCell isNil ]) ifTrue:
> [
> self setCurrentRowToNext.
> ]
> ifFalse:
> [
> (self currentRow notNil and: [ self currentCell notNil ]) ifTrue:
> [
> self setCurrentCellToNext.
>
> self currentCell notNil ifTrue:
> [
> self currentCell performKeyFocus: event inCellBounds: (self
> pvtGetCellBounds: self currentCell).
> ].
> ].
> ].
> ].
>
>
> Write code with that format is pathetical :(
>
> Is valid too have a "and" and "or" lazy? Exists a not lazy with #&  and #|  ,
> but could exists an #&&  and #||  . Is more easy...
>
>        value1 == value2 and:[<condition>  ] and: [<condition>] ......
>
> or
>
>       value1 == value2&&  <condition>  &&  <condition>  ......... ???
>
> Well, perhaps is a stupid question but I miss a more complete way for write
> code. If in Smalltalk is possible do easy that and include it in "core" why
> not do it?
>
> Is a reasonable desire :)
>
> Regards
>

It's a lot clearer what the code does when you avoid repeating checks in deeper nested levels.

Why not rewrite to the equivalent expression:

self currentCell
    ifNil: [
       self currentRow == sortedRows last
          ifTrue: [self navigationKey: event ]
          ifFalse: [self currentRow ifNotNil: [self setCurrentRowToNext]]
    ifNotNil: [
       self currentRow ifNotNil: [
          self setCurrentCellToNext.
          self currentCell performKeyFocus: event
                           inCellBounds: (self pvtGetCellBounds: self currentCell)]]

if sortedRows last cannot be nil, you could even write:

self currentRow ifNil: [^self].
self currentCell
    ifNotNil: [
       self setCurrentCellToNext.
       self currentCell performKeyFocus: event
                        inCellBounds: (self pvtGetCellBounds: self currentCell)]
    ifNil: [
       self currentRow == sortedRows last
          ifTrue: [self navigationKey: event]
          ifFalse: [self setCurrentRowToNext]]

Cheers,
Henry







_______________________________________________
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: Case statement and lazy comparison in Pharo

Stéphane Ducasse
In reply to this post by Michael Roberts-2
+ 1

On Mar 15, 2010, at 11:26 PM, Michael Roberts wrote:

> I would try and reduce the number of branches first. you can use guard
> conditions to exit early.
>
> This is not necessarily exact below, but you hopefully get the idea...
>
> (self currentRow == sortedRows last and: [self currentCell isNil])
> ifTrue: [^self navigationKey: event]
>
> self currentRow ifNil: [^self].
>
> self currentCell ifNil:
> [^self setCurrentRowToNext]
>
> self setCurrentCellToNext.
>
> self currentCell ifNil: [^self].
>
> self currentCell performKeyFocus: event inCellBounds:
> (self pvtGetCellBounds: self currentCell)
>
>
> A different approach entirely is to have an object(s) that represents
> the nil state for the row and cell. That way you are not checking
> constantly for it being nil, and perhaps you can dispatch some
> behaviour to it.
>
> cheers,
> Mike
>
> On Mon, Mar 15, 2010 at 9:49 PM, nullPointer <[hidden email]> wrote:
>>
>> Well, perhaps is a theme worked in another times but... is possible for Pharo
>> have a basic Case or elseIf statement? I know is easy create you own
>> structure control, but not is more useful have a "standard" for everybody?
>> I´m tired of write code like that...
>>
>>                        (self currentRow == sortedRows last and: [ self
>> currentCell isNil ]) ifTrue:
>>                        [
>>                                self navigationKey: event
>>                        ]
>>                        ifFalse:
>>                        [
>>                                (self currentRow notNil and: [ self currentCell isNil ]) ifTrue:
>>                                [
>>                                        self setCurrentRowToNext.
>>                                ]
>>                                ifFalse:
>>                                [
>>                                        (self currentRow notNil and: [ self currentCell notNil ]) ifTrue:
>>                                        [
>>                                                self setCurrentCellToNext.
>>
>>                                                self currentCell notNil ifTrue:
>>                                                [
>>                                                        self currentCell performKeyFocus: event inCellBounds: (self
>> pvtGetCellBounds: self currentCell).
>>                                                ].
>>                                        ].
>>                                ].
>>                        ].
>>
>>
>> Write code with that format is pathetical :(
>>
>> Is valid too have a "and" and "or" lazy? Exists a not lazy with #& and #|  ,
>> but could exists an #&& and #||  . Is more easy...
>>
>>      value1 == value2 and:[ <condition> ] and: [<condition>] ......
>>
>> or
>>
>>     value1 == value2 && <condition> && <condition> ......... ???
>>
>> Well, perhaps is a stupid question but I miss a more complete way for write
>> code. If in Smalltalk is possible do easy that and include it in "core" why
>> not do it?
>>
>> Is a reasonable desire :)
>>
>> Regards
>>
>>
>>
>>
>>
>>
>> --
>> View this message in context: http://n4.nabble.com/Case-statement-and-lazy-comparison-in-Pharo-tp1594080p1594080.html
>> Sent from the Pharo Smalltalk mailing list archive at Nabble.com.
>>
>> _______________________________________________
>> 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: Case statement and lazy comparison in Pharo

Richard Durr-2
cool :D

On Tue, Mar 16, 2010 at 3:58 PM, Stéphane Ducasse
<[hidden email]> wrote:

> + 1
>
> On Mar 15, 2010, at 11:26 PM, Michael Roberts wrote:
>
>> I would try and reduce the number of branches first. you can use guard
>> conditions to exit early.
>>
>> This is not necessarily exact below, but you hopefully get the idea...
>>
>> (self currentRow == sortedRows last and: [self currentCell isNil])
>>       ifTrue: [^self navigationKey: event]
>>
>> self currentRow ifNil: [^self].
>>
>> self currentCell ifNil:
>>       [^self setCurrentRowToNext]
>>
>> self setCurrentCellToNext.
>>
>> self currentCell ifNil: [^self].
>>
>> self currentCell performKeyFocus: event inCellBounds:
>>       (self pvtGetCellBounds: self currentCell)
>>
>>
>> A different approach entirely is to have an object(s) that represents
>> the nil state for the row and cell. That way you are not checking
>> constantly for it being nil, and perhaps you can dispatch some
>> behaviour to it.
>>
>> cheers,
>> Mike
>>
>> On Mon, Mar 15, 2010 at 9:49 PM, nullPointer <[hidden email]> wrote:
>>>
>>> Well, perhaps is a theme worked in another times but... is possible for Pharo
>>> have a basic Case or elseIf statement? I know is easy create you own
>>> structure control, but not is more useful have a "standard" for everybody?
>>> I´m tired of write code like that...
>>>
>>>                        (self currentRow == sortedRows last and: [ self
>>> currentCell isNil ]) ifTrue:
>>>                        [
>>>                                self navigationKey: event
>>>                        ]
>>>                        ifFalse:
>>>                        [
>>>                                (self currentRow notNil and: [ self currentCell isNil ]) ifTrue:
>>>                                [
>>>                                        self setCurrentRowToNext.
>>>                                ]
>>>                                ifFalse:
>>>                                [
>>>                                        (self currentRow notNil and: [ self currentCell notNil ]) ifTrue:
>>>                                        [
>>>                                                self setCurrentCellToNext.
>>>
>>>                                                self currentCell notNil ifTrue:
>>>                                                [
>>>                                                        self currentCell performKeyFocus: event inCellBounds: (self
>>> pvtGetCellBounds: self currentCell).
>>>                                                ].
>>>                                        ].
>>>                                ].
>>>                        ].
>>>
>>>
>>> Write code with that format is pathetical :(
>>>
>>> Is valid too have a "and" and "or" lazy? Exists a not lazy with #& and #|  ,
>>> but could exists an #&& and #||  . Is more easy...
>>>
>>>      value1 == value2 and:[ <condition> ] and: [<condition>] ......
>>>
>>> or
>>>
>>>     value1 == value2 && <condition> && <condition> ......... ???
>>>
>>> Well, perhaps is a stupid question but I miss a more complete way for write
>>> code. If in Smalltalk is possible do easy that and include it in "core" why
>>> not do it?
>>>
>>> Is a reasonable desire :)
>>>
>>> Regards
>>>
>>>
>>>
>>>
>>>
>>>
>>> --
>>> View this message in context: http://n4.nabble.com/Case-statement-and-lazy-comparison-in-Pharo-tp1594080p1594080.html
>>> Sent from the Pharo Smalltalk mailing list archive at Nabble.com.
>>>
>>> _______________________________________________
>>> 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
>

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