This loop is a special case. size := 10. prevIndex := size. prevValue := seq at: prevIndex. thisIndex := 1. thisValue := seq at: thisIndex. nextIndex := 2. nextIndex := seq at: nextIndex. [thisIndex <= size] whileTrue: [ ... prevIndex := thisIndex. prevValue := thisValue. thisIndex := nextIndex. thisValue := nextValue. nextIndex := nextIndex = size ifTrue: [1] ifFalse: [nextIndex + 1]. nextValue := seq at: nextIndex]. Counting backwards is similar. On Fri, 29 Mar 2019 at 04:44, James Foster <Smalltalk@jgfoster.net> wrote:
One of my recent assignments to first-year programming students was to model Left-Center-Right (https://en.wikipedia.org/wiki/LCR_(dice_game)) where they needed to do just what you are trying to do.
| size stream | size := 10. stream := WriteStream on: String new. 1 to: size do: [:i | stream print: i; tab; print: i \\ size + 1; tab; print: i + size - 2 \\ size + 1; tab; cr. ]. stream contents
On Mar 28, 2019, at 8:12 AM, Tim Mackinnon <tim@testit.works> wrote:
Hey guys - Iâm wondering if someone might have a nice way of selecting the previous value in a list - but wrapping around to the last value if you are at the front.
Essentially I have an exercise with some values in a collection like:
list := #($a $e $o $u).
And I have an index into the list, and want to loop around from index 1, back to 4 when I hit the beginning of the list.
While moving forwards is pretty easy with mod - e.g.
^list at: (index \\ list size + 1)
Iâm struggling with the cleanest way to do this backwards - as the following is unreadable to me (assuming index = 1)
^list at: (index - 2 \\ list size + 1)
And then I tried (which isn't bad - but still not quite right).
^list before: (list at: index) ifAbsent: [ids last].
Iâm sure there is a better way to do this - but itâs alluding me?
Tim