Skip to content

Instantly share code, notes, and snippets.

@scotthaleen
Last active August 29, 2015 13:56
Show Gist options
  • Select an option

  • Save scotthaleen/9276698 to your computer and use it in GitHub Desktop.

Select an option

Save scotthaleen/9276698 to your computer and use it in GitHub Desktop.
Given a list and a vector of indexes -- return a reorganized list from the indexes supplied, e.g. [A B C] [2 3 1] => [B C A]
(defn reorganize-list
([lst idxs] (reorganize-list lst idxs nil))
([lst idxs out-of-range]
(let [n (fn [i] (fn [l] (nth l i out-of-range)))
f (apply juxt (map n idxs))]
(f lst))))
;;=> (reorganize-list (range 10 20) [1 -1 5] 99)
;;[11 99 15]
;;=>(reorganize-list (range 10 20) [1 -1 5])
;;[11 nil 15]
;;=> (reorganize-list [\a \b \c \d \e] [4 2 1 99] \z)
;;[\e \c \b \z]
;;=> (reorganize-list [\a \b \c \d \e] [4 2 1 99])
;;[\e \c \b nil]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment