Okay, it looks like you did the work. So I'll offer an alternative. Move the 'a' assignment to the top of the loop, immediately preceded by the bounds check, with the beneficial side-effect that the loop exits earlier.
myArray := #(2 32 44 67 89 111 123). i := 1. n := 1. nMax := myArray size. [ (i <=130) and: (n<=nMax) ] whileTrue: [ a := myArray at: n. (i = a) ifTrue: [ Transcript show: i; cr. n := n +1. ]. ].
I think "array includes: i" is nice. But I guess my way is faster.
Correct
yes. But is #includes: fast enough? You are trading *one* line of code for *ten*. That makes #includes: faster write, so you stay more in-the-zone of your application domain, and forever-after makes it faster to understand every time your read your code.
You should avoid premature optimisation, and only use your implementation once benchmarks show its a problem.
cheers -ben
On 17 November 2017 at 10:57, RedTigerFish chihuyu@gmail.com wrote:
*Update:*
I found another way to solve this issue:
|myArray|
myArray := #(2 32 44 67 89 111 123).
n := 1. a := myArray at: n.
1 to: 130 do: [:i| i = a ifTrue: [ Transcript show: i; cr. n := n +1. a := myArray at: n. n = 7 ifTrue: [n := n - 1]. ]. ].
*Looks ugly though*
Dig, dig where you are, Down below's well. Let those that walk in darkness shout, Down below's hell. -- Sent from: http://forum.world.st/Squeak-Beginners-f107673.html _______________________________________________ Beginners mailing list Beginners@lists.squeakfoundation.org http://lists.squeakfoundation.org/mailman/listinfo/beginners