Commit cbb90e6e authored by Nikodemus Siivola's avatar Nikodemus Siivola
Browse files

NTH-VALUE-OR

 * Thanks to Andreas Fuchs -- I only took the liberty of changing the name from
   MULTIPLE-VALUE-OR to NTH-VALUE-OR.
parent a12ca477
......@@ -65,4 +65,18 @@ value."
(return-from ,xor (values nil nil))
(setf ,true ,tmp))))
datums)
(return-from ,xor (values ,true t))))))
\ No newline at end of file
(return-from ,xor (values ,true t))))))
(defmacro nth-value-or (nth-value &body forms)
"Evaluates FORM arguments one at a time, until the NTH-VALUE returned by one
of the forms is non-NIL. It then returns all the values returned by evaluating
that form. If none of the forms return a non-nil nth value, this form returns
NIL."
(once-only (nth-value)
(with-gensyms (values)
`(let ((,values (multiple-value-list ,(first forms))))
(if (nth ,nth-value ,values)
(values-list ,values)
,(if (rest forms)
`(nth-value-or ,nth-value ,@(rest forms))
nil))))))
......@@ -12,6 +12,7 @@
;; Control flow
#:cswitch
#:eswitch
#:nth-value-or
#:switch
#:whichever
#:xor
......
......@@ -1391,3 +1391,11 @@
(type-error ()
:type-error))
:type-error)
(deftest nth-value-or.1
(multiple-value-bind (a b c)
(nth-value-or 1
(values 1 nil 1)
(values 2 2 2))
(= a b c 2))
t)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment