Is the -> operator in Clojure (and what is this operator called in Clojure-speak?) equivalent to the pipeline operator |> in F#? If so, why does it need such a complex macro definition, when (|>) is just defined as
let inline (|>) x f = f x
Or if not, does F#’s pipeline operator exist in Clojure, or how would you define such an operator in Clojure?
No, they are not the same. Clojure doesn’t really have a need for
|>because all function calls are enclosed in lists, like(+ 1 2): there’s no magic you could do to make1 + 2work in isolation.1->is for reducing nesting and simplifying common patterns. For example:Expands to
The former is often easier to read, because conceptually you’re performing a series of operations on
x; the former code is “shaped” that way, while the latter needs some mental unraveling to work out.1 You can write a macro that sorta makes this work. The idea is to wrap your macro around the entire source tree that you want to transform, and let it look for
|>symbols; it can then transform the source into the shape you want. Hiredman has made it possible to write code in a very Haskell-looking way, with his functional package.