126034bff1da

Add Fare's `nest` macro
[view raw] [browse files]
author Steve Losh <steve@stevelosh.com>
date Mon, 25 Sep 2017 20:47:10 -0400
parents 7528d18f2430
children 0cf1ef232b1c
branches/tags (none)
files losh.lisp package.lisp

Changes

--- a/losh.lisp	Wed Aug 09 14:55:32 2017 -0400
+++ b/losh.lisp	Mon Sep 25 20:47:10 2017 -0400
@@ -14,6 +14,30 @@
                     forms))
      <>))
 
+(defmacro nest (&rest forms)
+  "Thread the given forms, putting each as the body of the previous.
+
+  Example:
+
+    (nest (multiple-value-bind (a b c) (foo))
+          (when (and a b c))
+          (multiple-value-bind (d e f) (bar))
+          (when (and d e f))
+          (do-something))
+
+  macroexpands to:
+
+    (multiple-value-bind (a b c) (foo)
+      (when (and a b c)
+        (multiple-value-bind (d e f) (bar)
+          (when (and d e f)
+            (do-something)))))
+
+  "
+  ;; thanks, Fare
+  (reduce (lambda (prefix body) `(,@prefix ,body))
+          forms :from-end t))
+
 
 ;;;; Types --------------------------------------------------------------------
 (deftype array-index (&optional (length (1- array-dimension-limit)))
--- a/package.lisp	Wed Aug 09 14:55:32 2017 -0400
+++ b/package.lisp	Mon Sep 25 20:47:10 2017 -0400
@@ -62,6 +62,7 @@
   (:export
     :-<>
     :<>
+    :nest
     :recursively
     :recur
     :when-found