Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Map mixer
(def aap {"Toby" {"Batman begins" 4.5
"TDKR" 5.0}
"Lisa" {"Batman begins" 5.0
"Spiderman" 4.5}})
(defn order-seq [order values]
(let [val-vec (vec values)]
(reduce (fn [v idx]
(conj v (get val-vec idx)))
[] order)))
(defmacro map-mixer [order]
(let [n (count order)
map-sym (gensym)
values-sym (gensym)
key-syms (take (dec n) (repeatedly gensym))
val-syms (cons values-sym (take (dec n) (repeatedly gensym)))]
(letfn [(outside-in [[k & ks] [v1 v2 :as vs] m]
(if k
`(reduce (fn [~map-sym [~k ~v2]]
~(outside-in ks (rest vs) map-sym))
~m ~v1)
`(assoc-in ~map-sym
~(order-seq (drop-last order) key-syms)
~(last val-syms))))]
`(fn [~values-sym]
~(outside-in key-syms val-syms {})))))
(defn transform-perfs [perfs]
(reduce (fn [m [person scores]]
(reduce (fn [m [movie score]]
(assoc m movie (assoc (get m movie {}) person score)))
m scores)) {} perfs))
(defn transform-perfs2 [perfs]
(reduce (fn [m [person scores]]
(reduce (fn [m [movie score]]
(assoc-in m [movie person] score))
m scores)) {} perfs))
(def transform-perfs3 (map-mixer [1 0 2]))
(defn -main
[& args]
(println (transform-perfs aap))
(println (transform-perfs2 aap))
(println (transform-perfs3 aap)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment