Skip to content

Instantly share code, notes, and snippets.

@jkk
Created December 8, 2010 18:38
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 jkk/733691 to your computer and use it in GitHub Desktop.
Save jkk/733691 to your computer and use it in GitHub Desktop.
;; in response to https://gist.github.com/733674
(def m
{"Lisa Rose" {"Lady in the Water" 2.5 "Snakes on a Plane" 3.5}
"Gene Seymour" {"Lady in the Water" 3.0 "Snakes on a Plane" 3.5}})
;; one way
(reduce
(fn [m [critic movie rating]]
(assoc-in m [movie critic] rating))
{}
(for [[critic ratings] m
[movie rating] ratings]
[critic movie rating]))
;; another way
(reduce
(fn [m [critic ratings]]
(reduce
(fn [m [movie rating]]
(assoc-in m [movie critic] rating))
m ratings))
{} m)
;; yet another way
(apply
merge-with merge
(for [[critic ratings] m
[movie rating] ratings]
{movie {critic rating}}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment