e14d7729f02a
Add `nullary`
author | Steve Losh <steve@stevelosh.com> |
---|---|
date | Thu, 11 Aug 2016 17:44:19 +0000 |
parents | 2991413e9fa6 |
children | 1a685df116ab |
branches/tags | (none) |
files | losh.lisp |
Changes
--- a/losh.lisp Thu Aug 11 00:08:21 2016 +0000 +++ b/losh.lisp Thu Aug 11 17:44:19 2016 +0000 @@ -192,6 +192,30 @@ (lambda (&rest args) (mapcar (rcurry #'apply args) fns))) +(defun nullary (function &optional result) + "Return a new function that acts as a nullary-patched version of `function`. + + The new function will return `result` when called with zero arguments, and + delegate to `function` otherwise. + + Examples: + + (max 1 10 2) => 10 + (max) => invalid number of arguments + + (funcall (nullary #'max)) => nil + (funcall (nullary #'max 0)) => 0 + (funcall (nullary #'max) 1 10 2) => 10 + + (reduce #'max nil) => invalid number of arguments + (reduce (nullary #'max) nil) => nil + (reduce (nullary #'max :empty) nil) => :empty + (reduce (nullary #'max) '(1 10 2)) => 10 + + " + (lambda (&rest args) + (if (null args) result (apply function args)))) + ;;;; Control Flow (defmacro recursively (bindings &body body)