I would do it as an iterator (a circular one). The structure of the underlying list is irrelevant here (as long as it is sequenceable), IMO. Regards, El vie., 29 de mar. de 2019 07:21, Peter Kenny <peter@pbkresearch.co.uk> escribió:
Tim
But the beauty of Smalltalk is that, if you need to use a particular structure frequently, you can make it an object just by subclassing something useful. In this case, you could create CircularList by subclassing SequenceableCollection and re-implementing #before and #after (and maybe tidying up #add if you don't want the circle to be extendable).
Pursuing this line of thought, we already have one specialized subclass in LinkedList. If the last element is linked to the first, this becomes circular. I glanced at some of the methods to see how well this would work, but because it regards links as separate from the linked objects, it got too complicated. But it could give a starting point if you have lots of such cases.
HTH
Peter Kenny
Tim Mackinnon wrote
Hey thanks guys - while it certainly makes sense when you think about it, I was kind of hoping we had something that was much more readable and obvious. It seems strange that when we have lots of esoteric things in collection, that something which is quite common to do isnât there. I was kind of hoping we might have some circular list or something.
As it stands, if you really want to communicate this clearly you are almost better off just doing, the more obvious:
(Index := index - 1) = 0 ifTrue: [index := items size].
Anyway, an interesting one.
Tim
On 28 Mar 2019, at 16:55, Peter Kenny <
peter@.co
> wrote:
Tim
I found myself puzzling as to *why* James's solution works. This longer explanation helped me to understand.
First, generalize moving forward to moving an arbitrary number of steps, still with wrapping.
^list at: (index + move - 1 \\ size + 1
Second, realize that moving backward one step with wrapping is exactly equivalent to moving forward (size - 1) steps - because of wrapping, a move of size steps is null.
Finally, substitute move = size - 1.
^list at: (index + size - 2 \\ size + 1.
This can of course easily generalize to backward moves of any size.
HTH
Peter Kenny
-- Sent from: http://forum.world.st/Pharo-Smalltalk-Users-f1310670.html
-- Sent from: http://forum.world.st/Pharo-Smalltalk-Users-f1310670.html