Add two utility functions to safely get a list or a single element out of a variable.

This commit is contained in:
Renaud Casenave-Péré 2014-09-19 17:17:58 +09:00
parent 4e544af6fc
commit d5d0aefbbd

View file

@ -7,9 +7,8 @@
(defpackage stoe.utils (defpackage stoe.utils
(:use :cl) (:use :cl)
(:nicknames :utils) (:nicknames :utils)
(:export :it (:export :it :aif :awhen
:aif :safe-first :safe-list
:awhen
:group :once-only :group :once-only
:restartable :restartable
:add-hook :remove-hook :run-hook :add-hook :remove-hook :run-hook
@ -33,6 +32,14 @@
(when it (when it
,@body))) ,@body)))
(defun safe-first (x)
"Return the first element of `x' if it is a list, return `x' otherwise."
(if (listp x) (first x) x))
(defun safe-list (x)
"Return `x' if it is a list, return '(x) otherwise."
(if (listp x) x (list x)))
(defun group (source n) (defun group (source n)
"Regroup the list `source' elements by n." "Regroup the list `source' elements by n."
(when (zerop n) (when (zerop n)