I need an interactive function that works like a generator, whenever it is called it should return "next" or "previous" value from a list. Whenever it reaches the end of the list, it should start over from the beginning (and vice versa when going backwards).
It looks like elisp offers (at least) a couple of ways to iterate forward (generators, cl-loop), but how do I cycle back?
I need something like:
(defvar my-list '(:a :b :c))
(defun cycle-through (previous)
(interactive)
(if previous
;; get previous --
;; get next))
(cycle-through) => :a
(cycle-through) => :b
(cycle-through t) => :a
(cycle-through) => :b
(cycle-through) => :c
(cycle-through) => :a
What's the best and elegant way of achieving something like that?
Mind that the list may have duplicate values, trying to store the current value and determine the current position based on that not desirable.