As you noted, wrapping going forward: seq at: index \\ seq size + 1 Wrapping going backward is nearly as easy: seq at: (index - 2) \\ seq size + 1 This would not work with #rem:
On Fri, 29 Mar 2019 at 04:13, 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 > > > >