Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clojure: move item in a list

I have this list:

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

I want to move "d" in the first position:

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

Is there any straightforward way to do this?

EDIT

Thanks for the answers. I had a look into it and I did this:

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

I am not sure if this is good design, but it does the trick.

like image 424
kfk Avatar asked Jun 27 '26 07:06

kfk


1 Answers

functions that can be helpful:
1. rotate

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]
like image 108
4e6 Avatar answered Jun 29 '26 23:06

4e6



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!