From d5d0aefbbd2d2d06ee94721df5d7a7787e8ea6c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Renaud=20Casenave-P=C3=A9r=C3=A9?= Date: Fri, 19 Sep 2014 17:17:58 +0900 Subject: [PATCH] Add two utility functions to safely get a list or a single element out of a variable. --- src/utils.lisp | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/utils.lisp b/src/utils.lisp index b099352..9658bbd 100644 --- a/src/utils.lisp +++ b/src/utils.lisp @@ -7,9 +7,8 @@ (defpackage stoe.utils (:use :cl) (:nicknames :utils) - (:export :it - :aif - :awhen + (:export :it :aif :awhen + :safe-first :safe-list :group :once-only :restartable :add-hook :remove-hook :run-hook @@ -33,6 +32,14 @@ (when it ,@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) "Regroup the list `source' elements by n." (when (zerop n)