Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert an array of tuples into a hash-map in Clojure

I have an array of tuples, where each tuple is a 2 tuple with a key and a value. What would be the cleanest way to convert this array of tuples into a hash-map?

like image 724
Mantas Vidutis Avatar asked Dec 01 '10 20:12

Mantas Vidutis


2 Answers

user=> (into {} [[:a 1] [:b 2]]) {:a 1, :b 2} 
like image 169
PheliX Avatar answered Oct 21 '22 07:10

PheliX


A map is a sequence of MapEntry elements. Each MapEntry is a vector of a key and value. The tuples in the question are already in the form of a MapEntry, which makes things convenient. (That's also why the into solution is a good one.)

user=> (reduce conj {} [[:a 1] [:b 2]]) {:b 2, :a 1} 
like image 41
miner49r Avatar answered Oct 21 '22 05:10

miner49r