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:
parent
5ade510598
commit
3f54dd8601
1 changed files with 19 additions and 9 deletions
|
@ -34,17 +34,19 @@
|
||||||
(defconst cycle/enable-tests? t
|
(defconst cycle/enable-tests? t
|
||||||
"When t, run the tests defined herein.")
|
"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)
|
(defun cycle/from-list (xs)
|
||||||
"Create a cycle from a list of `XS'."
|
"Create a cycle from a list of `XS'."
|
||||||
|
(if (= 0 (length xs))
|
||||||
|
(make-cycle :current-index nil
|
||||||
|
:previous-index nil
|
||||||
|
:xs xs)
|
||||||
(make-cycle :current-index 0
|
(make-cycle :current-index 0
|
||||||
:previous-index nil
|
:previous-index nil
|
||||||
:xs xs))
|
:xs xs)))
|
||||||
|
|
||||||
|
(defun cycle/new (&rest xs)
|
||||||
|
"Create a cycle with XS as the values."
|
||||||
|
(cycle/from-list xs))
|
||||||
|
|
||||||
(defun cycle/to-list (xs)
|
(defun cycle/to-list (xs)
|
||||||
"Return the list representation of a cycle, XS."
|
"Return the list representation of a cycle, XS."
|
||||||
|
@ -135,6 +137,14 @@ underlying struct."
|
||||||
cycle-xs
|
cycle-xs
|
||||||
(list/contains? x)))
|
(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
|
;; Tests
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
|
|
Loading…
Reference in a new issue