Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create seq of maps from two or more seqs

I'm new to Clojure and I was wondering if there's a way to create a sequence of maps from two or more sequences.

Let's say you have:

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

If you merged those I'd like to get what would be an array of hashes in Ruby, or in Clojure a sequence of maps?

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

thanks!

like image 462
kreek Avatar asked Jun 10 '12 08:06

kreek


2 Answers

 (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"})
like image 186
Michiel Borkent Avatar answered Nov 05 '22 01:11

Michiel Borkent


I'm sure there is a more idiomatic way to do this, but:

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

Or even

(map #(do {:number %1, :letter %2, :shape %3}) numbers letters shapes)
like image 36
huon Avatar answered Nov 05 '22 03:11

huon