list - Clojure:移动列表中的项目

标签 list clojure position

我有这个列表:

("a" "b" "c" "d" "e")

我想将“d”移动到第一个位置:

("d" "a" "b" "c" "e")

有什么简单的方法可以做到这一点吗?

编辑

感谢您的回答。我研究了一下,然后这样做了:

(defn move-item [data item-to-move]
    (conj (remove #(= % item-to-move) data) item-to-move))
(move-item ["a" "b" "c" "d" "e"] ["d"])

我不确定这是否是好的设计,但它确实有效。

最佳答案

可能有用的功能:
1.旋转

user=> (defn rotate [xs] (cons (last xs) (drop-last xs)))
#'user/rotate
user=> (rotate '(1 2 3))
(3 1 2)

2. replace

user=> (replace {1 4} [1 2 3 4])
[4 2 3 4]

关于list - Clojure:移动列表中的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8193461/

相关文章:

c++ - 从 Sprite 的位置和旋转获取位置偏移

c++ - 类 "list"包含包含类的结构

python - 对具有多个条件的元组列表进行排序

clojure - 使用 ring/compojure 连接到 clojure nREPL 的问题

clojure gen 类可变参数构造函数

c++ - 设置开始和结束限制以从文件中读取

python - 从 Python 2.7 中的数字列表中删除一组索引的最有效方法是什么?

python - 如何从 dict 获取值列表?

Clojure : more than 1 variadic overload, 更惯用的做法?

html - 单个 HTML 元素在另一个元素开头的位移