Sequence abstractions

There are functions that work on all the built in datastructures in Clojure.

first second rest cons

Practising with lists

Note Create a simple collection of developer events. First use a list of strings, then try a map with keywords. For each data structure, pull out some of the event details

  1. (def developer-events-strings '("Devoxx UK" "Devoxx France" "Devoxx" "Hack the Tower"))
  2. (def developer-events-strings2 (list "Devoxx UK" "Devoxx France" "Devoxx" "Hack the Tower"))
  3. developer-events-strings
  4. (first developer-events-strings)
  5. (def developer-events-vector
  6. [:devoxxuk :devoxxfr :devoxx :hackthetower] )

Using a Clojure Vector data structure seems a little more Clojurey, especially when the vector contains keywords. Think of a Vector as an Array, although in Clojure it is again immutable in the same way a list is.

Note Create a slightly more involved data structure, holding more data around each developer events. Suggest using 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.

  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 true}
  6. :hackthetower {:URL "http://hackthetower.co.uk"
  7. :event-type "hackday"
  8. :number-of-attendees 60
  9. :call-for-papers false}})

Lets 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))

Note Lets define a simple data structure for stocks data using a vector of maps, as there will be one or more company stocks to track. Each map represents the stock information for a company. Get the value of the whole data structure by refering to it by name, ask for a specific element by its position in the array using the nth function. Then try some of the common functions that work on collections.

  1. (def portfolio [ { :ticker "CRM" :lastTrade 233.12 :open 230.66}
  2. { :ticker "AAPL" :lastTrade 203.25 :open 204.50}
  3. { :ticker "MSFT" :lastTrade 29.12 :open 29.08 }
  4. { :ticker "ORCL" :lastTrade 21.90 :open 21.83 }])
  5. portfolio
  6. (nth portfolio 0)
  7. (nth portfolio 3)
  8. (first portfolio)
  9. (rest portfolio)
  10. (last portfolio)

First and next are termed as sequence functions in Clojure, unlike other lisps, you can use first and next on other data structures too