Here's a list of things I collected a conversation on partial
. Personally I think using partial
almost never has a benefit, while there are some downsides to using it. There are exceptional cases where partial
has a benefit, e.g. if you want to "wrap" a multi-arity function or a function with an unknown amount of arguments.
Some reasons not to use partial:
- Some people find it harder to find out what's happening*
- https://ask.clojure.org/index.php/8373/when-should-prefer-comp-and-partial-to-anonymous-functions?show=8374#a8374
- https://www.tiagoespinha.net/2016/12/clojure-beware-of-the-partial/
- https://twitter.com/borkdude/status/796018276044247041
- Better stack traces from SCI (if your code runs through SCI)
- Better performance in some cases (inlining / intrinsics)
- Arity warnings by analysis tools (CLJS compiler, kondo)
- https://tonsky.me/blog/readable-clojure/#avoid-higher-order-functions
*) For example, a function I recently encountered in a codebase. Personally I find comp
and partial
overused here and the usage of anonymous functions would improve understandability.
(defn process-blocks [viewers {:as doc :keys [ns]}]
(-> doc
(update :blocks (partial into [] (comp (mapcat (partial with-block-viewer doc))
(map (comp process-wrapped-value
apply-viewers*
(partial ensure-wrapped-with-viewers viewers))))))
(select-keys [:blocks :toc :title])
(cond-> ns (assoc :scope (datafy-scope ns)))))