Share your thoughts in the 2021 Clojure Community Survey!

Welcome! Please see the About page for a little more info on how this works.

+2 votes
in Clojure by

When writing code that needs to do predicate dispatch (such as code analysis libraries), it's quite common to need an invoke function for a pattern that looks like:

(defn classify [x]
(condp invoke x

string? (string-f x ..)
number? (number-f x ..)


This is definitely expressible without invoke, but the condp invoke pattern reads quite nicely.

Additionally invoke is also useful in the cases where one has a sliding window of functions and values, for patterns like:

(apply map invoke funs vals)

Patch: 0001-CLJ-2342-add-invoke.patch

2 Answers

+1 vote
Reference: (reported by bronsa)
0 votes

Comment made by: rickmoynihan

Some use cases for this are also when you have partially applied all the arguments to a function already and want to call it.

{{(map (comp :result invoke) fns)}}

{{apply}} can't be used for these cases as it requires arguments to be provided.

Welcome to Clojure Q&A, where you can ask questions and receive answers from members of the Clojure community.