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

tty gettimothy at zoho.com
Sun May 31 18:05:56 UTC 2020


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


More information about the Beginners mailing list