从两个或更多seqs创建seq的地图

时间:2012-06-10 08:00:28

标签: map clojure hashmap seq

我是Clojure的新手,我想知道是否有办法从两个或更多序列创建一系列地图。

假设你有:

(def numbers '(1 2 3))
(def letters '("a" "b" "c"))
(def shapes '("circle" "square" "triangle")) 

如果你合并了那些我想在Ruby中得到哈希数组的那些,或者在Clojure中得到一系列地图?

({:number 1, :letter "a", :shape "circle"} {:number 2, :letter "b", :shape "square"} {:number 3, :letter "c", :shape "triangle"})

谢谢!

2 个答案:

答案 0 :(得分:4)

 (map (fn [n l s] {:number n, :letter l, :shape s})  numbers letters shapes) ;;=> 
 ({:number 1, :letter "a", :shape "circle"} {:number 2, :letter "b", :shape "square"} {:number 3, :letter "c", :shape "triangle"})

答案 1 :(得分:4)

我确信有一种更惯用的方法可以做到这一点,但是:

(map (fn [n l s] {:number n, :letter l, :shape s}) numbers letters shapes)

甚至

(map #(do {:number %1, :letter %2, :shape %3}) numbers letters shapes)