Maps

The conj function works on all of the Clojure collections. The map collection also has functions that affect the evaluation of a map and the value of map returned.

Adding new values with conj

If you have a collection of maps, you can add another map to that collection with the conj function.

  1. (conj [{:map1 1}] {:map2 2})
  2. ;; => [{:map1 1} {:map2 2}]

Changing values with assoc

The assoc function is used to update a value in a map without neccessary being concerned about the current value. assoc returns a complete new map with the specified value.

  1. (assoc {:food "Fish"} :food "Fish&Chips")
  2. ;; => {:food "Fish&Chips"}

It does not matter how many keys are in the map, as keys are unique, then assoc will look up the specific key and change its value to that specified in the third argument.

If a key is not in the map, assoc will add both the key and the value.

  1. (def alphabet-soup {:a 1 :b 2 :c 3})
  2. (assoc alphabet-soup :d 4)
  3. ;; => {:a 1, :b 2, :c 3, :d 4}

If there are multiple levels to the structure of your map, ie. the value of a key in the map is also a map

For example, the value of :luke in the starwars-characters map is represented as a map too {:fullname "Luke Skywarker" :skill "Targeting Swamp Rats"}

  1. (def starwars-characters
  2. {:luke {:fullname "Luke Skywarker" :skill "Targeting Swamp Rats"}
  3. :vader {:fullname "Darth Vader" :skill "Crank phone calls"}
  4. :jarjar {:fullname "JarJar Binks" :skill "Upsetting a generation of fans"}})

To update the skill of one of the characters we can use assoc-in to update the correct value by traversing the map via the given keys.

  1. (assoc-in starwars-characters [:vader :skill] "The Dark Side of the Force")
  2. ;; => {:luke {:fullname "Luke Skywarker", :skill "Targeting Swamp Rats"},
  3. :vader {:fullname "Darth Vader", :skill "The Dark Side of the Force"},
  4. :jarjar {:fullname "JarJar Binks", :skill "Upsetting a generation of fans"}}

Update values in a map with update

Rather than replace the current value with one specified, update applys a function to the existing value in order to update that value.

  1. (def alphabet-soup {:a 1 :b 2 :c 3})
  2. (update alphabet-soup :a inc)
  3. ;; => {:a 2, :b 2, :c 3}

Hint As with assoc you can also use update on nested maps using the update-in function.