These macros are definitely useful, but being macros, they deal with syntactic forms instead of functions, and thus make a bunch of things harder than they should be. Some such cases:
- Running a computation conditionally.
- Nil-shortcutting. This is a special case of 1, where the condition is that the element isn’t nil.
- There are two macros for dealing with two common argument positions - first and last. What happens when the argument has to go anywhere else? Or what happens when in a single pipeline, you need to invoke two computations with two different argument positions?
- Dropping in a real function value in pipeline.
- Threading a value through a bunch of iteratively obtained computations.
- Tap an immediate value, and pass it forward.
- Handle to intermediate results.
These, and many such other problems, have spawned to a cottage industry of people developing macro extensions or macro alternatives to the two standard threading macros. Let’s see how some of those solutions try to address the problems mentioned above:
- Prismatic developed penguin operators,
?>>. Pellet came up with
when->etc. Chris Houser’s Synthread has
when. Clojure core recently added
cond->>to address the same problem. All of these are special purpose macros which transform the given expression in a form acceptable to
- Swiss Arrows has
some-<>>. A variation of the same has made it to the standard library, under name
some->. (And its cousin
- Swiss Arrows’
-<>>(diamond wand and diamond spear respectively) allow you to explicitly specify the argument position. (I find this better.) Synthread’s
arg->, and now core’s
as->allow you to name intermediate results, which can also help in marking the argument position.
- In the current implementation, people typically just put another set of parens around a function value. e.g.
(-> 5 ((fn [x] (- x 7)))). (Yeah, hacky.) Prismatic’s plumbing library has a
fn->macro which makes this syntactically a bit nicer.
- See #3.
There is a common theme in all of the above solutions:
- They are all macros. These are typically developed in one of two ways: Either a whole new set of macros with additional semantics, like Swiss Arrows. Or, macros that expand their input forms in a way acceptable to
->>, and thus can be used with them. This doesn’t strike me as very elegant.
- Many of them come in two flavors - one that works with
->, another that works with
- As evidenced by #1, macros are the primary way used for extension. It’s hard to extend with functions in this framework.
Here I am proposing a dead simple threading solution, purely based on functions/combinators.
Pretty simple, isn’t it? Here is some example use (from REPL):
Some benefits of this approach:
- It deals entirely with function values. The pipe combinator only cares that it’s given a bunch of functions that can be called one after another. That’s the only contract it has. It doesn’t care how these function values came to life.
- …which means it’s quite easy to extend this system with new combinators. For an exercise, try writing a combinator that executes the given function and on exception defaults to a given fallback value.
- You do not need to invent a new convention or new syntax for the argument positioning. The user can simply use
#(reader macro, or
partial, or whatever strikes their fancy, and benefit from the already present syntax/functions.
- Handle to an intermediate value can be done in a straightforward way with nested pipes.
It’s not all good and shiny though. There are some major downsides too:
- The most common use cases of
->>when translated to our approach would perhaps lead to noisier code, using either
- The macro based approaches we talked about move around forms, insert arguments here and there, create intermediate let-bindings, and avoid generating function values as much as possible. This leads to better performance.
This perhaps indicates that the function based approach may have occurred before to people, but they would have abandoned it for aforementioned reasons.
Nevertheless I may try using this on some project, just to see how much it scales, it terms of readability and performance.
This blog post was originally posted at my Blogspot blog at this URL.