Just Juxt It!
juxt is one remarkably useful core Clojure function, that doesn’t
seem to be widely used (or understood for that matter), but is part of
the arsenal of every experienced Clojure hacker.
Looking at the official docs you’ll see that
juxt takes a set of
functions and returns a function that is the juxtaposition of those
functions. The returned function takes a variable number of
arguments, and returns a vector containing the result of applying each
function to the arguments (from left to right). Basically:
At first glace that probably doesn’t seem particularly useful. Let’s
see some practical applications of
juxt. What if we wanted to split
a sequence into two sequences - one with the values that satisfy some
predicate and one with the values that don’t. While there are many
ways to do so,
juxt offers one particularly elegant:
1 2 3 4 5 6
juxt is also quite helpful when dealing with multiple map keys:
1 2 3 4 5 6 7 8 9 10 11 12
Hopefully this short article has whetted your appetite and you’ll find
even more elegant uses for
juxt in your code.