Support cycle/empty?

Add predicate for determining if a cycle contains items.

Updated cycle/{new,from-list} to support setting current-index to nil when a
consumer calls it with an empty list.
This commit is contained in:
William Carroll 2020-02-08 15:53:28 +00:00
parent 5ade510598
commit 3f54dd8601

View file

@ -34,17 +34,19 @@
(defconst cycle/enable-tests? t
"When t, run the tests defined herein.")
(defun cycle/new (&rest xs)
"Create an empty cycle."
(make-cycle :current-index 0
:previous-index nil
:xs xs))
(defun cycle/from-list (xs)
"Create a cycle from a list of `XS'."
(make-cycle :current-index 0
:previous-index nil
:xs xs))
(if (= 0 (length xs))
(make-cycle :current-index nil
:previous-index nil
:xs xs)
(make-cycle :current-index 0
:previous-index nil
:xs xs)))
(defun cycle/new (&rest xs)
"Create a cycle with XS as the values."
(cycle/from-list xs))
(defun cycle/to-list (xs)
"Return the list representation of a cycle, XS."
@ -135,6 +137,14 @@ underlying struct."
cycle-xs
(list/contains? x)))
(defun cycle/empty? (xs)
"Return t if cycle XS has no elements."
(= 0 (length (cycle-xs xs))))
(defun cycle/focused? (xs)
"Return t if cycle XS has a non-nil value for current-index."
(maybe/some? (cycle-current-index xs)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Tests
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;