Created
July 30, 2017 17:37
-
-
Save stoeckley/1d26aa13eb05c90be57966c49e097c00 to your computer and use it in GitHub Desktop.
Drag index through a vector
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn slide | |
"Returns the final vector after dragging an indexed item to another index, which means it swaps incrementally with each index it passes through. v should be a vector but if it isn't it will be converted to one." | |
[v o n] | |
(let [v (vec v) ;; ensures v is a vector | |
item (v o) | |
removed (if (= o (count v)) | |
(pop v) | |
`[~@(concat | |
(subvec v 0 o) | |
(subvec v (inc o)))])] | |
`[~@(concat (conj | |
(subvec removed 0 n) | |
item) | |
(subvec removed n))])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment