我試圖找到某個特定函數計算結果爲true的向量中的所有元素。我的失敗嘗試是在下面。想法?Clojure:查找所有功能評估爲真的向量
(defn find-matches [match-fn elements]
(map-indexed
(fn [idx elem]
(if (= true (apply match-fn elem))
[idx elem])
elements))
(find-matches even? [1 2 3 4]) ; -> Arity Exception Wrong number of args (1) passed to: core$map-indexed clojure.lang.AFn.throwArity
請回想一下'map'和'map-indexed'產生一個新的序列one-for-一個與他們的序列參數的元素。你需要''for''中的'filter'或'keep-indexed'或者':when'子句(就像這些答案一樣)來保留一些元素並丟棄其他元素。 – Thumbnail 2014-09-01 08:40:30