Skip to content

Instantly share code, notes, and snippets.

@stoeckley
Created July 30, 2017 17:37
Show Gist options
  • Save stoeckley/1d26aa13eb05c90be57966c49e097c00 to your computer and use it in GitHub Desktop.
Save stoeckley/1d26aa13eb05c90be57966c49e097c00 to your computer and use it in GitHub Desktop.
Drag index through a vector
(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