Thanks for a great idea Andrew!
I was slightly annoyed by the lack of consistency in parameter
ordering of Clojure collections API (cons vs conj, get/assoc vs filter/
map/take).
IMHO the input collection should ALWAYS be the last param. Then simple
->> macro would be sufficient in most cases.
Out of curiosity (and because apparently your code is not open-
source :-) I've re-written it as:
(defmacro -$> [ & forms ] (reduce #(replace {'$ %1} %2) forms))
It does not support your non-seq entries in the pipeline, but I
personally don't want them.
I'm guessing they are to be able to write something like
(-$> 1 (* 3 $) inc)
instead of
(-$> 1 (* 3 $) (inc $))
or do you have a better use for it?
Thanks again,
- Dmitry
On Oct 29, 12:35 pm, "AndrewC." <[email protected]> wrote:
> Here's a macro that generalizes the two 'threading' macros -> and ->>.
>
> It works by using the idea of 'nesting marker' (from Oz) to specify
> where the result of the previous form should be nested in the
> subsequent form.
>
> (defmacro -$>
> "Threads the expr through the forms. Inserts x into
> the first form at the position marked by the $ symbol.
> If the second form is not a list then it behaves as ->.
> If there are more forms, inserts the first form into the second
> form at the position marked by the $ symbol, etc."
> ([x form] (if (seq? form)
> (let [split (split-with (partial (complement =) '$)
> form)]
> `(~@(first split) ~x ~@(rest (second split))))
> (list form x)))
> ([x form & more] `(-$> (-$> ~x ~form) ~...@more)))
>
> If this is any use to anyone (esp. anyone who has a CA) then consider
> it public domain.
>
> Any improvements appreciated!
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en