Skip to content

Instantly share code, notes, and snippets.

@qbg
Created July 9, 2010 18:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qbg/469794 to your computer and use it in GitHub Desktop.
Save qbg/469794 to your computer and use it in GitHub Desktop.
(defn convert-alist
[alist]
(let [size (apply max (map first alist))]
(reduce (fn [v [i o]] (assoc v (dec i) o))
(vec (repeat size nil))
alist)))
@slyrus
Copy link

slyrus commented Jul 9, 2010

(defn convert-alist
  [alist & [key first]]
  (reduce (fn [v x] (assoc v (key x) x))
          [nil]
          alist))

@slyrus
Copy link

slyrus commented Jul 9, 2010

Ok then, this should do it:

(defn convert-alist
  [alist & [key first]]
  (reduce (fn [v x] (assoc v (key x) x))
          (vec (repeat (apply max (map key alist)) nil))
          alist))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment