【发布时间】:2013-05-09 22:47:35
【问题描述】:
我试图弄清楚为什么这个特定的功能没有按预期工作。我从错误消息中怀疑它与我为累加器创建空向量的方式有关。
我有一个简单的函数,它返回一系列 2 元素向量:
(defn zip-with-index
"Returns a sequence in which each element is of the
form [i c] where i is the index of the element and c
is the element at that index."
[coll]
(map-indexed (fn [i c] [i c]) coll))
效果很好。当我尝试在另一个函数中使用它时,问题就来了
(defn indexes-satisfying
"Returns a vector containing all indexes of coll that satisfy
the predicate p."
[p coll]
(defn accum-if-satisfies [acc zipped]
(let [idx (first zipped)
elem (second zipped)]
(if (p elem)
(conj acc idx)
(acc))))
(reduce accum-if-satisfies (vector) (zip-with-index coll)))
它可以编译,但是当我尝试使用它时出现错误:
user=> (indexes-satisfying (partial > 3) [1 3 5 7])
ArityException Wrong number of args (0) passed to: PersistentVector
clojure.lang.AFn.throwArity (AFn.java:437)
我无法弄清楚这里出了什么问题。此外,如果有一种更“类似于 Clojure”的方式来做我想做的事情,我也有兴趣了解这一点。
【问题讨论】:
标签: exception vector clojure arity