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!
(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"})
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With