Would you consider something like this a bad practice?
(defun cycle-list (ls &optional backward?)
"Given any list, returns the next element each time it is called."
(let* ((last-pos (- (length ls) 1))
(maybe-counter (gethash ls cycle-list-counter))
(cal-counter (if maybe-counter
(if backward?
(- maybe-counter 1)
(+ maybe-counter 1))
0))
(counter (cond ((> cal-counter last-pos) 0)
((< cal-counter 0) last-pos)
(t cal-counter))))
(puthash ls counter cycle-list-counter)
(nth counter ls)))
I wrote it because it is pretty handy, e.g. I used it to write another function which cycle between themes.
Surely, it shouldn't be used with simple lists, such as '(1 2 3)
, or in place of (do-list ...)
.
But also, say we add one more theme to (custom-available-themes), now the previous hash-key is left unused.
I suppose in this particular case of theme cycling, there is probably a way to deal with this but: I want to know if the function (cycle-list ...)
itself to you seems bizarre.