List Clojure:移动列表中的项目
我有以下清单:List Clojure:移动列表中的项目,list,clojure,position,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"]) 我不
("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"])
我不确定这是否是一个好的设计,但它确实起到了作用。如果您不需要太多的灵活性,那么我会选择一个分解解决方案:
user=> (letfn [(des [{:strs [a b c d]}] [d a b c])]
(des (set ["a" "b" "c" "d"])))
["d" "a" "b" "c"]
user=>
使用set
允许将元素用作键,可以使用:strs
指令将其拆分和命名。然后,您只需按照所需的顺序返回元素
我想宏应该允许您概括这种方法。可能有用的函数:
1. <代码>旋转
user=> (defn rotate [xs] (cons (last xs) (drop-last xs)))
#'user/rotate
user=> (rotate '(1 2 3))
(3 1 2)
二,
在这种情况下,您应该发布自己的答案并接受它。(移动项目[“a”“b”“c”“d”“e”][“d”];=>([“d”]“a”“b”“c”“d”“e”)
user=> (replace {1 4} [1 2 3 4])
[4 2 3 4]
(defn move-last [coll]
(cons (last coll) (drop-last coll)))
user=>(move-last '("a" "b" "c" "d"))
("d" "a" "b" "c")