There are places in code where I need functions that I define and use only once, in the same place I define them. There are 2 main ways to define such functions, one is anonymous fns:
(map #(blepify-with blep (:blop %)) xs)
Another is higher order functions:
(map (comp (partial blepify-with blep) :blop) xs)
I personally don't use partial
, and always prefer anonymous functions, because:
- anonymous functions go through var dereference on invokation, which means I can redefine
blepify-with
and new definition is used automatically. It helps greatly during development, and just as performant in production with direct linking;
partial
makes code less readable by hiding function arity. Explicit is better than implicit, and Cursive can show me immediately if I pass wrong amount of arguments to function
I also rarely use comp
. In addition to same argument about var dereferencing, it makes me read code from right to left. On small examples I provided it does not really matter, but as it grows bigger, threading macros greatly enhance readability.
They only time I find comp
useful is when inversed order of function application is negated by inverse order of data processing, such as transducers: (comp (filter even?) (map inc))
is intended to be read from left to right. Even though (map inc)
is invoked first, data processed by this transducer will first go through even?
predicate, and then through inc
transform.
My question is, is there something more to partial
and comp
that I'm missing? Maybe it's a matter of taste and I'm just not used to them, and they are just as readable, and not having to redefine usage places is overrated? When should I prefer comp
and partial
to anonymous functions?