Background Motivation
First things first, I love to use the -> (thread-first) and -» (thread-last) macros. Recently, while coding a simple Clojure app, I encountered a very common problem. One function I required in my chain did not match positions with the others.
And so, I reached out to twitter and to the blogosphere.
Standard Wisdom
Gonzih let me know that the rule of thumb was to use nested calls of the opposite macro.
Jay Field’s blog reinforced the same.
In his example, the given data looked like this:
and the desired output, like this:
Though not his final solution, the one I suspect most would use looks like this:
Enter the as-> Macro
A bit of further searching led me to the new-as-of-Clojure 1.5 as-> macro.
At the cost of some more explicitness, the as-> macro allows us to specify the ‘stitch point’ for our argument insertion.
As a reformed Pythonista, I am not against being explicit. In my coding since, I have begun to use the as-> macro even when -> or -» would suffice.
Alternatives
While searching, I also stumbled upon Robert Levy’s swiss-arrows library. Though unnecessary for my simple use, swiss-arrows provides some excellent macros for threading a var through a series of expressions.