-2

In Clojure, it is not possible to use Case forms with Vars:

(case (resolve 'inc) #'inc 1)
Execution error (IllegalArgumentException) at user/eval2061 (REPL:1).
No matching clause: #'clojure.core/inc

But I could use a hash-map or a (condp = ...) form instead for a readable solution.

What are some good approaches to dispatch on values of Var objects in Clojure when considering only raw performance?

erdos
  • 3,135
  • 2
  • 16
  • 27

2 Answers2

4

You could pull the symbol from the var and match on symbols instead with case.

(case (symbol a-var) clojure.core/inc 1 #_other_cases )
Alex Miller
  • 69,183
  • 25
  • 122
  • 167
1

You've listed the obvious candidates. To see which is faster for your use case, write them both up and do some benchmarking. Strangers who don't know what kinds of lookups you need to do will not have more accurate answers.

Aside: it seems kinda weird to be searching through a bunch of vars for an equality match. Are you sure you want to do that?

amalloy
  • 89,153
  • 8
  • 140
  • 205