Have you seen that Quino’s drawing in which a housemaid is so good that she even puts Picasso’s Guernica in order when asked to tidy a messy room?
Clojure is as cool as that housemaid in that it even cares about style, allowing you to choose whichever fits you better. Thus, if like me, you’re more used to an imperative-ordered writing/reading style, Clojure has something called arrow macros to make things easier for us.
Let’s pick a simple example to ilustrate this. Let’s define an odd-adder function which, as its name suggests, given a collection of numbers, it will only add the odd elements; as an extra feature, the result will be the quantity expressed in dollars:
(defn odd-adder [numbers]
(str "$" (reduce + (filter odd? numbers))))
//(odd-adder [1 3 4 8 5]) will be evaluated to $9
For someone used to functional nesting in Clojure, the fact that you have to read from right to left to understand the order in which the operations will be evaluated, is likely no big deal. But let’s give that piece of code to our orderly housemaid:
(defn odd-adder [numbers]
(->> (filter odd? numbers)
(reduce +)
(str "$")
))
Clearer, isn’t it?
Arrow functions (or Thread macros, as they’re also known as) come in several flavours: check them out here! My preferred one -the one that has been used in the example above- is called Thread-last (->>). However, you are allowed to call it the Quino housemaid if it suits you better…
And that is one more reason why I love Clojure…