# HG changeset patch # User Steve Losh # Date 1470937459 0 # Node ID e14d7729f02a38e07bb624f8a4b39828504d7d70 # Parent 2991413e9fa6131f2e390d84e11cd52b2d55222d Add `nullary` diff -r 2991413e9fa6 -r e14d7729f02a losh.lisp --- 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)