2020-09-01 14:44:18 +02:00
|
|
|
;;; cycle.el --- Simple module for working with cycles -*- lexical-binding: t -*-
|
2020-09-01 11:17:43 +02:00
|
|
|
|
2019-10-09 13:13:56 +02:00
|
|
|
;; Author: William Carroll <wpcarro@gmail.com>
|
2020-09-01 11:17:43 +02:00
|
|
|
;; Version: 0.0.1
|
2020-09-01 14:44:18 +02:00
|
|
|
;; Package-Requires: ((emacs "24.3"))
|
2019-10-09 13:13:56 +02:00
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
;; Something like this may already exist, but I'm having trouble finding it, and
|
|
|
|
;; I think writing my own is a nice exercise for learning more Elisp.
|
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
2020-01-09 14:41:44 +01:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; Dependencies
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
|
|
|
(require 'prelude)
|
2019-10-09 13:13:56 +02:00
|
|
|
(require 'math)
|
2020-01-09 14:41:44 +01:00
|
|
|
(require 'maybe)
|
2020-09-02 15:39:41 +02:00
|
|
|
(require 'struct)
|
2020-09-02 16:23:46 +02:00
|
|
|
(require 'cl-lib)
|
2019-10-09 13:13:56 +02:00
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; Wish list
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
|
|
|
;; - TODO: Provide immutable variant.
|
|
|
|
;; - TODO: Replace mutable consumption with immutable variant.
|
2020-09-01 11:17:43 +02:00
|
|
|
;; - TODO: Replace indexing with (math-mod current cycle).
|
2019-10-09 13:13:56 +02:00
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; Library
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
|
|
|
;; `current-index' tracks the current index
|
|
|
|
;; `xs' is the original list
|
2020-01-09 14:41:44 +01:00
|
|
|
(cl-defstruct cycle current-index previous-index xs)
|
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(defconst cycle-enable-tests? t
|
2020-01-09 14:41:44 +01:00
|
|
|
"When t, run the tests defined herein.")
|
2019-10-09 13:13:56 +02:00
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(defun cycle-from-list (xs)
|
2019-10-09 13:13:56 +02:00
|
|
|
"Create a cycle from a list of `XS'."
|
2020-02-08 16:53:28 +01:00
|
|
|
(if (= 0 (length xs))
|
|
|
|
(make-cycle :current-index nil
|
|
|
|
:previous-index nil
|
|
|
|
:xs xs)
|
|
|
|
(make-cycle :current-index 0
|
|
|
|
:previous-index nil
|
|
|
|
:xs xs)))
|
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(defun cycle-new (&rest xs)
|
2020-02-08 16:53:28 +01:00
|
|
|
"Create a cycle with XS as the values."
|
2020-09-01 11:17:43 +02:00
|
|
|
(cycle-from-list xs))
|
2019-10-09 13:13:56 +02:00
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(defun cycle-to-list (xs)
|
2019-10-09 13:13:56 +02:00
|
|
|
"Return the list representation of a cycle, XS."
|
|
|
|
(cycle-xs xs))
|
|
|
|
|
2020-09-01 14:44:18 +02:00
|
|
|
(defun cycle--next-index<- (lo hi x)
|
2019-10-09 13:13:56 +02:00
|
|
|
"Return the next index in a cycle when moving downwards.
|
|
|
|
- `LO' is the lower bound.
|
|
|
|
- `HI' is the upper bound.
|
|
|
|
- `X' is the current index."
|
|
|
|
(if (< (- x 1) lo)
|
|
|
|
(- hi 1)
|
|
|
|
(- x 1)))
|
|
|
|
|
2020-09-01 14:44:18 +02:00
|
|
|
(defun cycle--next-index-> (lo hi x)
|
2019-10-09 13:13:56 +02:00
|
|
|
"Return the next index in a cycle when moving upwards.
|
|
|
|
- `LO' is the lower bound.
|
|
|
|
- `HI' is the upper bound.
|
|
|
|
- `X' is the current index."
|
|
|
|
(if (>= (+ 1 x) hi)
|
|
|
|
lo
|
|
|
|
(+ 1 x)))
|
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(defun cycle-previous-focus (cycle)
|
2020-01-09 14:41:44 +01:00
|
|
|
"Return the previously focused entry in CYCLE."
|
|
|
|
(let ((i (cycle-previous-index cycle)))
|
2020-08-31 15:59:48 +02:00
|
|
|
(if (maybe-some? i)
|
2020-01-09 14:41:44 +01:00
|
|
|
(nth i (cycle-xs cycle))
|
|
|
|
nil)))
|
|
|
|
|
2020-01-09 15:01:49 +01:00
|
|
|
;; TODO: Consider adding "!" to the function name herein since many of them
|
|
|
|
;; mutate the collection, and the APIs are beginning to confuse me.
|
2020-09-01 11:17:43 +02:00
|
|
|
(defun cycle-focus-previous! (xs)
|
2020-01-09 15:01:49 +01:00
|
|
|
"Jump to the item in XS that was most recently focused; return the cycle.
|
|
|
|
This will error when previous-index is nil. This function mutates the
|
|
|
|
underlying struct."
|
|
|
|
(let ((i (cycle-previous-index xs)))
|
2020-08-31 15:59:48 +02:00
|
|
|
(if (maybe-some? i)
|
2020-01-09 15:01:49 +01:00
|
|
|
(progn
|
2020-09-01 11:17:43 +02:00
|
|
|
(cycle-jump i xs)
|
|
|
|
(cycle-current xs))
|
2020-01-09 15:01:49 +01:00
|
|
|
(error "Cannot focus the previous element since cycle-previous-index is nil"))))
|
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(defun cycle-next (xs)
|
2020-01-09 14:41:44 +01:00
|
|
|
"Return the next value in `XS' and update `current-index'."
|
|
|
|
(let* ((current-index (cycle-current-index xs))
|
2020-09-01 14:44:18 +02:00
|
|
|
(next-index (cycle--next-index-> 0 (cycle-count xs) current-index)))
|
2020-09-01 00:28:47 +02:00
|
|
|
(struct-set! cycle previous-index current-index xs)
|
|
|
|
(struct-set! cycle current-index next-index xs)
|
2020-01-09 14:41:44 +01:00
|
|
|
(nth next-index (cycle-xs xs))))
|
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(defun cycle-prev (xs)
|
2020-01-09 14:41:44 +01:00
|
|
|
"Return the previous value in `XS' and update `current-index'."
|
|
|
|
(let* ((current-index (cycle-current-index xs))
|
2020-09-01 14:44:18 +02:00
|
|
|
(next-index (cycle--next-index<- 0 (cycle-count xs) current-index)))
|
2020-09-01 00:28:47 +02:00
|
|
|
(struct-set! cycle previous-index current-index xs)
|
|
|
|
(struct-set! cycle current-index next-index xs)
|
2020-01-09 14:41:44 +01:00
|
|
|
(nth next-index (cycle-xs xs))))
|
2019-10-09 13:13:56 +02:00
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(defun cycle-current (cycle)
|
2019-10-09 13:13:56 +02:00
|
|
|
"Return the current value in `CYCLE'."
|
|
|
|
(nth (cycle-current-index cycle) (cycle-xs cycle)))
|
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(defun cycle-count (cycle)
|
2019-10-09 13:13:56 +02:00
|
|
|
"Return the length of `xs' in `CYCLE'."
|
|
|
|
(length (cycle-xs cycle)))
|
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(defun cycle-jump (i xs)
|
2020-01-09 14:41:44 +01:00
|
|
|
"Jump to the I index of XS."
|
|
|
|
(let ((current-index (cycle-current-index xs))
|
2020-09-01 11:17:43 +02:00
|
|
|
(next-index (math-mod i (cycle-count xs))))
|
2020-09-01 00:28:47 +02:00
|
|
|
(struct-set! cycle previous-index current-index xs)
|
|
|
|
(struct-set! cycle current-index next-index xs))
|
2020-01-09 14:41:44 +01:00
|
|
|
xs)
|
2019-10-09 13:13:56 +02:00
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(defun cycle-focus (p cycle)
|
2019-10-09 13:13:56 +02:00
|
|
|
"Focus the element in CYCLE for which predicate, P, is t."
|
|
|
|
(let ((i (->> cycle
|
|
|
|
cycle-xs
|
|
|
|
(-find-index p))))
|
|
|
|
(if i
|
2020-09-01 11:17:43 +02:00
|
|
|
(cycle-jump i cycle)
|
2019-10-09 13:13:56 +02:00
|
|
|
(error "No element in cycle matches predicate"))))
|
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(defun cycle-focus-item (x xs)
|
2020-09-01 14:44:18 +02:00
|
|
|
"Focus item, X, in cycle XS.
|
2020-02-08 16:55:59 +01:00
|
|
|
ITEM is the first item in XS that t for `equal'."
|
2020-09-01 11:17:43 +02:00
|
|
|
(cycle-focus (lambda (y) (equal x y)) xs))
|
2020-02-08 16:55:59 +01:00
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(defun cycle-contains? (x xs)
|
2019-10-09 13:13:56 +02:00
|
|
|
"Return t if cycle, XS, has member X."
|
|
|
|
(->> xs
|
|
|
|
cycle-xs
|
2020-09-01 11:17:43 +02:00
|
|
|
(list-contains? x)))
|
2019-10-09 13:13:56 +02:00
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(defun cycle-empty? (xs)
|
2020-02-08 16:53:28 +01:00
|
|
|
"Return t if cycle XS has no elements."
|
|
|
|
(= 0 (length (cycle-xs xs))))
|
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(defun cycle-focused? (xs)
|
2020-02-08 16:53:28 +01:00
|
|
|
"Return t if cycle XS has a non-nil value for current-index."
|
2020-08-31 15:59:48 +02:00
|
|
|
(maybe-some? (cycle-current-index xs)))
|
2020-02-08 16:53:28 +01:00
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(defun cycle-append (x xs)
|
2020-02-08 16:57:08 +01:00
|
|
|
"Add X to the left of the focused element in XS.
|
|
|
|
If there is no currently focused item, add X to the beginning of XS."
|
2020-09-01 11:17:43 +02:00
|
|
|
(if (cycle-empty? xs)
|
2020-02-08 16:57:08 +01:00
|
|
|
(progn
|
2020-09-01 00:28:47 +02:00
|
|
|
(struct-set! cycle xs (list x) xs)
|
|
|
|
(struct-set! cycle current-index 0 xs)
|
|
|
|
(struct-set! cycle previous-index nil xs))
|
2020-02-08 16:57:08 +01:00
|
|
|
(let ((curr-i (cycle-current-index xs))
|
|
|
|
(prev-i (cycle-previous-index xs)))
|
|
|
|
(if curr-i
|
|
|
|
(progn
|
2020-09-01 00:28:47 +02:00
|
|
|
(struct-set! cycle xs (-insert-at curr-i x (cycle-xs xs)) xs)
|
2021-12-24 17:47:16 +01:00
|
|
|
(when (and prev-i (>= prev-i curr-i))
|
|
|
|
(struct-set! cycle previous-index (1+ prev-i) xs))
|
2020-09-01 00:28:47 +02:00
|
|
|
(when curr-i (struct-set! cycle current-index (1+ curr-i) xs)))
|
2020-02-08 16:57:08 +01:00
|
|
|
(progn
|
2020-09-01 00:28:47 +02:00
|
|
|
(struct-set! cycle xs (cons x (cycle-xs xs)) xs)
|
|
|
|
(when prev-i (struct-set! cycle previous-index (1+ prev-i) xs))))
|
2020-02-08 16:57:08 +01:00
|
|
|
xs)))
|
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(defun cycle-remove (x xs)
|
2020-02-08 16:57:08 +01:00
|
|
|
"Attempt to remove X from XS.
|
|
|
|
|
|
|
|
X is found using `equal'.
|
|
|
|
|
|
|
|
If X is the currently focused value, after it's deleted, current-index will be
|
|
|
|
nil. If X is the previously value, after it's deleted, previous-index will be
|
|
|
|
nil."
|
|
|
|
(let ((curr-i (cycle-current-index xs))
|
|
|
|
(prev-i (cycle-previous-index xs))
|
|
|
|
(rm-i (-elem-index x (cycle-xs xs))))
|
2020-09-01 00:28:47 +02:00
|
|
|
(struct-set! cycle xs (-remove-at rm-i (cycle-xs xs)) xs)
|
2020-02-08 16:57:08 +01:00
|
|
|
(when prev-i
|
2020-09-01 00:28:47 +02:00
|
|
|
(when (> prev-i rm-i) (struct-set! cycle previous-index (1- prev-i) xs))
|
|
|
|
(when (= prev-i rm-i) (struct-set! cycle previous-index nil xs)))
|
2020-02-08 16:57:08 +01:00
|
|
|
(when curr-i
|
2020-09-01 00:28:47 +02:00
|
|
|
(when (> curr-i rm-i) (struct-set! cycle current-index (1- curr-i) xs))
|
|
|
|
(when (= curr-i rm-i) (struct-set! cycle current-index nil xs)))
|
2020-02-08 16:57:08 +01:00
|
|
|
xs))
|
|
|
|
|
2020-01-09 14:41:44 +01:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; Tests
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(when cycle-enable-tests?
|
|
|
|
(let ((xs (cycle-new 1 2 3)))
|
|
|
|
(prelude-assert (maybe-nil? (cycle-previous-focus xs)))
|
|
|
|
(prelude-assert (= 1 (cycle-current xs)))
|
|
|
|
(prelude-assert (= 2 (cycle-next xs)))
|
|
|
|
(prelude-assert (= 1 (cycle-previous-focus xs)))
|
|
|
|
(prelude-assert (= 1 (->> xs (cycle-jump 0) cycle-current)))
|
|
|
|
(prelude-assert (= 2 (->> xs (cycle-jump 1) cycle-current)))
|
|
|
|
(prelude-assert (= 3 (->> xs (cycle-jump 2) cycle-current)))
|
|
|
|
(prelude-assert (= 2 (cycle-previous-focus xs)))
|
|
|
|
(prelude-assert (= 2 (cycle-focus-previous! xs)))
|
|
|
|
(prelude-assert (equal '(1 4 2 3) (cycle-xs (cycle-append 4 xs))))
|
|
|
|
(prelude-assert (equal '(1 2 3) (cycle-xs (cycle-remove 4 xs))))
|
2020-02-08 16:57:08 +01:00
|
|
|
(progn
|
2020-09-01 11:17:43 +02:00
|
|
|
(cycle-focus-item 3 xs)
|
|
|
|
(cycle-focus-item 2 xs)
|
|
|
|
(cycle-remove 1 xs)
|
|
|
|
(prelude-assert (= 2 (cycle-current xs)))
|
|
|
|
(prelude-assert (= 3 (cycle-previous-focus xs))))))
|
2020-01-09 14:41:44 +01:00
|
|
|
|
2019-10-09 13:13:56 +02:00
|
|
|
(provide 'cycle)
|
|
|
|
;;; cycle.el ends here
|