我想知道为什么这个特定函数不能按预期工作。我怀疑从错误消息中可以看出,它与我为累加器创建空向量的方式有关。如何通过调用来构建矢量以减少
我有一个简单函数,返回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的方式来做我想做的事情,我也有兴趣听到这一点。
这正是问题。这么小的事情。我也喜欢你的版本。 – 2013-05-09 23:10:19