问题描述
我想要在Clojure中使用等同于Python的itertools.accumulate()。
如果您不熟悉,基本上是reduce()
,但是它将每个调用的输出存储到reduce函数。
我似乎在内置clojure函数中找不到1:1的等效项。我最接近的工作近似值是
(defn accumulate
"Like `reduce` but stores result of every step."
([f coll]
(accumulate f (first coll) (rest coll)))
([f val coll]
(loop [result [val]
current-val val
next-val (first coll)
coll (rest coll)]
(if (empty? coll)
(conj result (f current-val next-val))
(let [new-val (f current-val next-val)]
(recur (conj result new-val)
new-val
(first coll)
(rest coll)))))))
是否已经存在执行此功能的功能?
如果没有,还有更好的方法吗?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)