Map

A map is a key / value pair data structure. Keys are usually defined using a keyword, although they can be strings or anything else.

Keywords point to themselves, so using them for the keys makes it very easy to get values out of the map, or for updating existing values in the map.

Note Explore creating maps

  1. {:key "value"}
  2. (:key 42)
  3. {:key :value}
  4. {"key" "value"}
  5. ("key" :value)
  6. {:a 1 :b 2 :c 3}
  7. {:monday 1 :tuesday 2 :wednesday 3 :thursday 4 :friday 5 :saturday 6 :sunday 7}

Maps of other collections

Its also quite common to have maps made up of other maps, maps of vectors or vectors of maps.

Note Create a map to represent the world of Starwars, including characters & ships. Indicate the factions that characters and ships belong to.

  1. {:starwars {
  2. :characters {
  3. :jedi ["Luke Skywalker"
  4. "Obiwan Kenobi"]
  5. :sith ["Darth Vader"
  6. "Darth Sideous"]
  7. :droids ["C3P0"
  8. "R2D2"]}
  9. :ships {
  10. :rebel-alliance ["Millenium Falcon"
  11. "X-wing figher"]
  12. :imperial-empire ["Intergalactic Cruser"
  13. "Destroyer"
  14. "Im just making these up now"]}}}

Individual starwars characters can be defined using a map of maps

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

To make the starwars character information easier to use, lets give the collection of characters a name using the def function

  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"}})

Now we can refer to the characters using keywords. Using the get function we return all the information about Luke

  1. (get starwars-characters :luke)
  2. (get (get starwars-characters :luke) :fullname)

By wrapping the get function around our first, we can get a specific piece of information about Luke. There is also the get-in function that makes the syntax a little easier to read

  1. (get-in starwars-characters [:luke :fullname])
  2. (get-in starwars-characters [:vader :fullname])

Or if you want the data driven approach, just talk to the map directly

  1. (starwars-characters :luke)
  2. (:fullname (:luke starwars-characters))
  3. (:skill (:luke starwars-characters))
  4. (starwars-characters :vader)
  5. (:skill (:vader starwars-characters))
  6. (:fullname (:vader starwars-characters))

And finally we can also use the threading macro to minimise our code further

  1. (-> starwars-characters
  2. :luke)
  3. (-> starwars-characters
  4. :luke
  5. :fullname)
  6. (-> starwars-characters
  7. :luke
  8. :skill)

This technique is called descructuring. Find out more on Destructuring

Duplicate keys in a map are not allowed, so the following maps…

  1. {"fish" "battered" "chips" "fried" "fish" "battered and fried"}
  2. {:fish "battered" :chips "fried" :fish "battered & fried"}
  3. ;; ...throw dupicate key errors
  4. ;; Duplicate values are okay though
  5. {"fish" "battered" "chips" "fried" "cod" "fried"}

Updating maps

Note Get the current project information and create a map to hold that information

  1. (->> "project.clj"
  2. slurp
  3. read-string
  4. (drop 2)
  5. (cons :version)
  6. (apply hash-map)
  7. (def project-configs))
  8. ;; Evalute the new map defined as project
  9. project

We pull out the map of project information using slurp, tidy the text up using read-string and drop the first two elements (defproject playground). This returns a list that we want to turn into a map, but first we need to add a key to the version number. Using the cons function we can add an element to the start of the list, in this case the :version keyword

Now we can successfully convert the list that is returned into a map, with balanced key-value pairs. Then we simply create a name for this new map, project-configs, so we can refer to it elsewhere in the code.