Added the 'chain' convenience macro for method call chaining (ex:
authorVladimir Sedach <vsedach@gmail.com>
Thu, 18 Jun 2009 23:11:56 +0000 (17:11 -0600)
committerVladimir Sedach <vsedach@gmail.com>
Thu, 18 Jun 2009 23:11:56 +0000 (17:11 -0600)
(chain ($ "foo") (bar x z) (baz 5)) => $('foo').bar(x, z).baz(5);)

src/lib/ps-macro-lib.lisp

index 5c65ad6..1013939 100644 (file)
       `(@ (slot-value ,obj ,(if (symbolp (car props)) `',(car props) (car props))) ,@(cdr props))
       obj))
 
+(defpsmacro chain (&rest method-calls)
+  (labels ((do-chain (method-calls)
+             (if (cdr method-calls)
+                 `((@ ,(do-chain (cdr method-calls)) ,(caar method-calls)) ,@(cdar method-calls))
+                 (car method-calls))))
+    (do-chain (reverse method-calls))))
+
+
 (defpsmacro concatenate (result-type &rest sequences)
   (assert (equal result-type ''string) () "Right now Parenscript 'concatenate' only support strings.")
   (cons '+ sequences))