获取错误的args数量传递给Clojure函数

Dav*_*aid 10 clojure

更多Clojure怪异.我有这个功能,我正在尝试定义和调用.它有3个参数,但当我用3个参数调用它时,我得到了

Wrong number of args (1) passed to: solr-query$correct-doc-in-results-QMARK-$fn
 [Thrown class clojure.lang.ArityException]
Run Code Online (Sandbox Code Playgroud)

当我用2个参数调用它时,我得到了

Wrong number of args (2) passed to: solr-query$correct-doc-in-results-QMARK-
  [Thrown class clojure.lang.ArityException]
Run Code Online (Sandbox Code Playgroud)

当我用4个参数调用它时,我得到了

Wrong number of args (4) passed to: solr-query$correct-doc-in-results-QMARK-
  [Thrown class clojure.lang.ArityException]
Run Code Online (Sandbox Code Playgroud)

这是函数的定义:

(defn correct-doc-in-results? [query results docid]
  "Check if the docid we expected is returned in the results"
  (some #(.equals docid) (map :id (get results query))))
Run Code Online (Sandbox Code Playgroud)

这就是我试图调用它的方式(来自REPL,使用emacs中的swank):

(correct-doc-in-results? "FLASHLIGHT" all-queries "60184")
Run Code Online (Sandbox Code Playgroud)

有谁知道发生了什么事?为什么我认为我在传递3时只传递1个参数,但在2或4时正确?我不是一个非常流利的clojure程序员,但定义一个函数是非常基本的.

Bri*_*per 15

注意区别

solr-query$correct-doc-in-results-QMARK-

solr-query$correct-doc-in-results-QMARK-$fn

第一个是指你的功能correct-doc-in-results?.后者指的是在该函数内定义的一些匿名函数.

如果你传递2或4个参数,你的顶层函数会出现错误,正如预期的那样.当你传递3个参数时,你会得到一个错误#(.equals docid),因为#(.equals docid)想要零参数但是得到一个.尝试将其更改为#(.equals % docid).

  • @Brian实际上错误是因为匿名函数想要零args(没有%)并且在被`some`调用时得到一个.当然,如果函数实际上*没有参数被调用,那么通过尝试仅使用一个参数调用`.equals`会导致另一个错误. (2认同)