Clojure Maps

; Hash maps and array maps share an interface. Hash maps have faster

; lookups but don’t retain key order.

(class {:a 1 :b 2 :c 3}) ; => clojure.lang.PersistentArrayMap
(class (hash-map :a 1 :b 2 :c 3)) ; => clojure.lang.PersistentHashMap

; Arraymaps will automatically become hashmaps through most operations

; if they get big enough, so yu don’t need to worry.

;Maps can use any hashable type as a key, but usually keywords are best

; Keywords are like strings with some efficiency bounces

(class :a) ; => clojure.lang.Keyword
(def stringmap {“a” 1, “b” 2, “c” 3})
stringmap ; => {“a” 1, “b” 2, “c” 3}
(def keymap {:a 1, :b 2, :c 3})
keymap ; => {:a 1, :c 3, :b 2}

; By the way, commas are always treated as whitespace and do nothing

; Retrieve a value from a map by calling it as a function

(stringmap “a”) ; => 1
(keymap :a) ; => 1

; Keywords can be used to retrieve their value from a map too

(:b keymap) ; => 2
; Don't try this with strings.
;("a" stringmap)
; => Exception: java.lang.String cannot be cast to clojure.lang.IFn

;Retrieving a non-present key returns nil

(stringmap “d”) ; => nil

; Use assoc to add new keys to hash-maps

(def newkeymap (assoc keymap :d 4))
newkeymap ; => {:a 1, :b 2, :c 3}

; But remember, clojure types are immutable!!!!

keymap : => {:a 1, :b 2, :c 3}

; Use dissoc to remove keys

(dissoc keymap :a :b) ; => {:c 3}
One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.