Converting a vector of maps to map of maps in Clojure
I've a vector of maps like this:
[{:categoryid 1, :categoryname "foo" }
{:categoryid 2, :categoryname "bar" }
{:categoryid 3, :categoryname "baz" }]
and would like to generate a map of maps like this 开发者_如何学运维for searching by categoryname
{"foo" {:categoryid 1, :categoryname "foo" },
"bar" {:categoryid 2, :categoryname "bar" },
"baz" {:categoryid 3, :categoryname "baz" }}
How can I do this?
Another way: (into {} (map (juxt :categoryname identity) [...]))
(reduce (fn [m {catname :categoryname :as input}]
(assoc m catname input))
{}
[{:categoryid 1, :categoryname "foo" }
{:categoryid 2, :categoryname "bar" }
{:categoryid 3, :categoryname "baz" }])
Better yet,
(#(zipmap (map :categoryname %) %)
[{:categoryid 1, :categoryname "foo" }
{:categoryid 2, :categoryname "bar" }
{:categoryid 3, :categoryname "baz" }])
(ns code.groupby
(:use clojure.contrib.seq-utils))
(def vector-of-maps [ {:categoryid 1, :categoryname "foo" }
{:categoryid 2, :categoryname "bar" }
{:categoryid 3, :categoryname "baz" } ])
(group-by :categoryname vector-of-maps)
Gives you a map of Vectors of maps
{"bar" [{:categoryid 2, :categoryname "bar"}],
"baz" [{:categoryid 3, :categoryname "baz"}],
"foo" [{:categoryid 1, :categoryname "foo"}]}
(which I now realise isn't what you wanted...sorry)
I haven't tested it in Clojure, but in ClojureScript this variant appears to be faster than others:
(reduce #(assoc %1 (:categoryname %2) %2) {} [...])
精彩评论