Search code examples
clojurearray-map

Array-map example in clojure


I am learning clojure and trying to implement a problem. I am storing maps in a vector. Each map contains an id. For example [{:id 1 :name "abc"} {:id 2 :name "xyz"}]. The map also contains some more fields.

I read somewhere that, instead of using a vector to store the maps, I could use an array-map and do away with my id and store it something like {1 {:name "abc"}, 2 {:name "xyz"}}.

I tried going through the clojure docs but didn't find a good example to achieve this. Can some please help me out and give me a good example?


Solution

  • You can use assoc to add values to a map. assoc takes 3 args. The first arg is the map that you want to add to, 2nd arg is a key, and the third is a value. The function returns the old map with the key-value pair added.

    Example:

    (assoc {} 1 {:name "abc"})
    

    returns

    {1 {:name "abc"}}