Consider this method in an example class with an instance variable threshold:
Example >>
Now, if you are reading testArraySum code, if the threshold does not change, it should always repeat the same thing, right? Try that you start setting a fixed value of a, and then subtract (or not, depending on the threshold, but we said that it is fixed) a fixed amount, so it should be ... 20.
Ok if you rate
Example new testArraySum
several times, you get 20,18, 16 ... because array # (4 8 10) is changing. On the other hand,
Example >> testConstantArraySum | a | a := Array new: 3. a at: 1 put: 4; at: 2 put: 8; at: 3 put: 10. a sum > threshold ifTrue: [ a at: 1 put: a first - 2 ]. ^a sum
really constant.
source share