我觉得必须有一种更清洁的方法来做到这一点,但我没有看到。
(defn map-rest
"Passes through the first item in coll intact. Maps the rest with f."
[f coll]
(concat (list (first coll)) (map f (rest coll))))
最佳答案
解构和使用 cons
而不是 concat
:
(defn map-rest [f [fst & rst]] (cons fst (map f rst)))
REPL 输出:
user=> (map-rest inc [1 2 3 4 5])
(1 3 4 5 6)
关于clojure - 有没有更好的方法来映射除第一项之外的所有项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13016020/