Last active
August 29, 2015 13:56
-
-
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]
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 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