As also explained in the answers of this question, Clojure determines the number of parameters of an anonymous function (defined through #()
), by the maximal parameter index referenced in the body (e.g. if the maximal referenced parameter is %4
, then that anonymous function has 4 parameters).
Question: is there some syntax to tell the Clojure compiler that an anonymous function expects one parameter, even not referencing that parameter? Or, in this case, the only "clean way"* is to use the fn syntax? (* "Clean way": I'm aware that in most cases, you could reference the dummy parameter without affecting the function -- see below, but I would like to avoid such workarounds.)
My use case: I have a function (defn foo [predicate])
, where predicate
expects exactly one argument, and I would like to test foo
with a predicate that always returns true
. (I.e., something like #(true)
.) This causes an ArityException
since the interpreter thinks that #(true)
expects zero arguments.
In this particular case, I could use some clever hack like #(or true %)
, but:
- It is almost as much to type as the
fn
-variant. - I'm interested if there is a generic solution.
Edit: so to be clear, I'm looking for something like this:
#[1](true) ;; the anonymous function takes one parameter