Clojure的map
是其他lisps可能会调用mapcar
,而car
大致相当于clojure的first
。这让我想知道是否有mapcdr
以及clojure是否具有cdr
大致相当于clojure' s rest
的函数。
我想这样的行为是这样的:
(mapcdr #(apply + %) [1 2 3 4 5])
=> (15 14 12 9 5)
扩张看起来像:
(list (apply + [1 2 3 4 5])
(apply + [2 3 4 5])
(apply + [3 4 5])
(apply + [4 5])
(apply + [5])
答案 0 :(得分:1)
写了一些快速的东西,虽然它仍然很好有一些更原生的东西。
(defn maplist
"Based on Common Lisp's maplist."
[fn coll]
(if (empty? coll) nil
(cons (fn coll)
(maplist fn (rest coll)))))
(maplist #(apply + %) [1 2 3 4 5])
=> (15 14 12 9 5)
如果没有,我会感到惊讶,因为似乎标准map
仅maplist
first
缠绕coll
。
答案 1 :(得分:0)
您可以使用fn reductions,它几乎可以满足您的需求:
(->> [1 2 3 4 5]
reverse
(reductions +)
reverse)