With Maps of Maps

Note Define a collection of starwars characters using a map of maps. Each character should have an name that they are typically refered to, along with their fullname and skill

  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 informatoin about Luke

  1. (get starwars-characters :luke)

By wrapping the get function around our first, we can get a specific piece of information about Luke

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

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 you can get really Clojurey by just talking 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)

Note* Create a slightly data structure holding data around several developer events. Each event should have a website address, event type, number of attendees, call for papers.

  1. (def dev-event-details
  2. {:devoxxuk {:URL "http://jaxlondon.co.uk"
  3. :event-type "Conference"
  4. :number-of-attendees 700
  5. :call-for-papers "open"}
  6. :hackthetower {:URL "http://hackthetower.co.uk"
  7. :event-type "hackday"
  8. :number-of-attendees 99
  9. :call-for-papers "closed"}})

This data structure is just a map, with each key being the unique name of the developer event.

The details of each event (the value to go with the event name key) is itself a map as there are several pieces of data associated with each event name. So we have a map where each value is itself a map.

Call the data structre and see what it evaluates too, it should not be a surprise

  1. dev-event-details

We can ask for the value of a specific key, and just that value is returned

  1. (dev-event-details :devoxxuk)

In our example, the value returned from the :devoxxuk key is also a map, so we can ask for a specific part of that map value by again using its key

  1. (:URL (dev-event-details :devoxxuk))