Create a map when you have a vector of keys and values

I am sure this is easy, but I suspect that I will have many small questions on the way to idomatic clojure. Maybe I missed something, but looking at the clojure map page, I did not find a solution.

Given two vectors (one of the keys of other values), how do you efficiently (keyword!) Create a map from key to value?

Keys and meanings below:

(:year :month :day) (core/split "2013-02-18" #"-") 
+6
source share
2 answers

A natural solution is to use zipmap :

 (zipmap [:year :month :day] (clojure.string/split "2013-02-18" #"-")) ;= {:day "18", :month "02", :year "2013"} 

For a small such card it is quite effective. For a larger map, you need zipmap use transients that are not currently available. There is a ticket for this in JIRA, with my patch attached: CLJ-1005 .

Of course, it’s simple enough to include the zipmap included with the transients in one own code base and use it in preference to the one located in clojure.core . This is a pretty important thing if you are holding large cards.

Code can be copied from a patch or from the main ClojureScript library, which uses transients in its zipmap ; here is a link to the source of ClojureScript from version 1844 (this particular function can be used in Clojure unchanged).

+16
source

What you are looking for, zipmap

+2
source

Source: https://habr.com/ru/post/949888/


All Articles