clojure - 有没有更好的方法来映射除第一项之外的所有项?

标签 clojure

我觉得必须有一种更清洁的方法来做到这一点,但我没有看到。

(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/

相关文章:

clojure - "SSL doesn' t have a valid keystore"尝试连接到 Datomic Cloud 时出错

clojure - 有没有办法在 Clojure 中获取 watch 的所有按键

clojure - 在 Clojure 中构建 CLI 脚本

clojure - 如何在用户定义的命名空间中启动 REPL?

java - Twitter Storm 的测试 API 不会初始化

algorithm - 在 Clojure 中实现 Minimax 算法 - 具有多个递归调用的条件函数

clojure - 一个参数,多种功能

clojure - 我可以在 Clojure 的类型和属性上分派(dispatch)一个多方法吗?

mongodb - Clojure 和 NoSQL 数据库

java - Clojure Intellij Cursive插件JDK错误