2/24/2012 - 3:01 AM

Vectors and Maps in Clojure

Vectors and Maps in Clojure

;; So I can create a map pretty easily
user=> (def a-map { :name "Nick" :age 1000 })

;; and get a value from it with the key
user=> (get a-map :name)

;; I also know how to create a vector (in this case of maps)
user=> (def a-vec [ { :name "Nick" :age 1000 } { :name "Bob" :age 45 } ])

;; retrieving from it seems simple enough
user=> (first a-vec)
{:age 1000, :name "Nick"}
user=> (get a-vec 0)
{:age 1000, :name "Nick"}

;; What I am confused about is why a map's key is a function to the map
user=> (:name a-map)
;; but the same isn't true for a vector
user=> (0 a-vec)
ClassCastException java.lang.Long cannot be cast to clojure.lang.IFn  user/eval1388 (NO_SOURCE_FILE:1)

;; yet the converse is fine with a vector
user=> (a-map :nick)
user=> (a-vec 0)
{:age 1000, :name "Nick"}