Clojure:对映射集合中的值求和,直到达到一个值。

Clojure: summing values in a collection of maps until a value is reached.

我的目标是将一组地图中的值相加,直到达到其中一个地图值中的值。我尝试使用 this example 来解决这个问题,但它没有涉及我如何只能获取列表的一部分。然后 return 没有值的集合。像这样

(def foo '({:key 1, :value 2} {:key 1, :value 2} {:key 1, :value 2})
(defn addValuesUp [foo]
  (take-while (< ((apply merge-with + foo) :value) 4) foo))
 and have it return something like this
'({:key 1, :value 2} {:key 1, :value 2})   

相反,我收到错误布尔值无法转换为 clojure.lang.IFn

查看 take-while 的文档:

clojure.core/take-while
([pred coll])
  Returns a lazy sequence of successive items from coll while
  (pred item) returns true. pred must be free of side-effects.

pred 在这种情况下是一个 returns 布尔值的函数。在您的代码中, take-while 的第一个参数不是函数而是表达式。

这就是您收到错误 ClassCastException java.lang.Boolean cannot be cast to clojure.lang.IFn 的原因。这个错误告诉你 Clojure 需要一个函数 (IFn) 但它找到了一个布尔值(你的表达式的结果)。

一旦你把它变成一个函数,你应该会有所进步。然而,在函数实现中可能需要做更多的工作。

我解决这个问题的方法是在映射中添加一个新键,其中包含所有以前的值,这样你就可以做一个简单的 take-while:

(defn sums-of-values [c]
  (reductions + c))

(defn with-accum [c]
  (map #(assoc %1 :accum %2)
       c
       (sums-of-values (map :value c))))

现在地图有一个 :accum 槽,您可以使用 take-while:

(take-while (comp (partial >= 4)
                  :accum)
            (with-accum foo))