From d3d24db8bd03f5182afa8979c77b501f92fcc3f4 Mon Sep 17 00:00:00 2001 From: nicolas-p Date: Fri, 3 Apr 2015 23:02:39 +0200 Subject: [PATCH] Added ap-pipe and ap-compose macros `ap-pipe` is an anaphoric version of `->` and `->>`. It is useful for specifying where the argument should go in each expression. `ap-compose` returns a function which is the composition of several anaphoric forms. --- hy/contrib/anaphoric.hy | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/hy/contrib/anaphoric.hy b/hy/contrib/anaphoric.hy index 5000cbf..628ae9d 100644 --- a/hy/contrib/anaphoric.hy +++ b/hy/contrib/anaphoric.hy @@ -109,3 +109,15 @@ `(let [[acc ~initial-value]] (ap-each ~lst (setv acc ~form)) acc))) + + +(defmacro ap-pipe [var &rest forms] + "Pushes a value through several forms. + (Anaphoric version of -> and ->>)" + (if (empty? forms) var + `(ap-pipe (let [[it ~var]] ~(first forms)) ~@(rest forms)))) + + +(defmacro ap-compose [&rest forms] + "Returns a function which is the composition of several forms." + `(fn [var] (ap-pipe var ~@forms)))