clojure的map可能是其他lisp所说的mapcar,而car大致相当于clojure的first。这让我想知道是否有mapcdr,以及clojure是否有这样一个函数,其中cdr大致相当于clojure的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])发布于 2015-03-09 01:04:53
您可以使用fn reductions,它几乎可以执行您想要的操作:
(->> [1 2 3 4 5]
reverse
(reductions +)
reverse)发布于 2015-03-09 22:10:11
写的东西很快,但如果能有更本地化的东西还是很好的。
(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。
https://stackoverflow.com/questions/28928620
复制相似问题