Declare dummy (unused) parameters for Clojure anonymous function

て烟熏妆下的殇ゞ 提交于 2020-01-07 02:37:05

问题


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:

  1. It is almost as much to type as the fn-variant.
  2. 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


回答1:


No. Just use fn. That's what it's for.




回答2:


If it's a constant return you are after you can use constantly:

(map (constantly 10) '(1 2 3 4 5)) 
; ==> (10 10 10 10 10)

If it's not you can use fn with a rest argument:

(fn [& _] 
 some-side-effect-expression)
; ==> non functional value

In fact this can be done on higher order functions where you don't need all the passes arguments:

(some-fun (fn [a b & _] (+ a b)) '(1 2 3 4 5))
; ==> (3 5 7 9)



回答3:


If you insist on the #( ... ) syntax, you can write

#(do % true)

... incurring no run-time penalty, whatever you want to return.

But (constantly true), as @Sylvester suggests, is idiomatic, and accepts any argument list:

((constantly true) 1 2 "Buckle my shoe");true


来源:https://stackoverflow.com/questions/41909132/declare-dummy-unused-parameters-for-clojure-anonymous-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!