Odd behavior in inject:into: on simple change in Boolean test sequence

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
tty
Reply | Threaded
Open this post in threaded view
|

Odd behavior in inject:into: on simple change in Boolean test sequence

tty
Hi folks.

I have a bit of code I am attempting to expand and I am getting strange
behavior on a very simple change in structure.

I am parsing '2 * 3 / 2"  and returning the product of the terms. Works
fine.

Here is the original code. it works as intended.


* result := pairs inject: first
                        into: [ :total :pair |  |p|
                                        p := pair reject:[:a | a class = OrderedCollection].
               
                                         (p first = '*')
                                                ifTrue: [total :=  (total * p last )]
                                                ifFalse: [
                                                        (p last = 0)
                                                                ifTrue:[^NaNError]. "prevent divide by 0"
                                                        total :=  ( total / p last )]
                                                 ] .*






However, If I make a very simple change to the internal logic by changing
the ifTrue: ifFalse: to two separate tests 'total' goes nil on the second
pass.


* result := pairs inject: first
                        into: [ :total :pair |  |p|
                                        p := pair reject:[:a | a class = OrderedCollection].
               
                                         (p first = '*')
                                                ifTrue: [total :=  (total * p last )] .
                                         (p first = '/')
                                                ifTrue: [
                                                        (p last = 0)
                                                                ifTrue:[^NaNError]. "prevent divide by 0"
                                                        total :=  ( total / p last )]
                                                 ] .*

it blows up on the second pass as 'total' is Nil and the division throughs a
DNU. yet, |p| is populated with #('/' 2)

Thanks in advance





--
Sent from: http://forum.world.st/Squeak-Beginners-f107673.html
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners