philipbohun.com

Glojure by Example: Maps

Maps associate keys with values. Keywords are the most common keys. A keyword looks itself up. get takes a fallback for a missing key.

(def person {:name "Ada" :born 1815})
(println (:name person))
(println (get person :city "unknown"))

assoc, dissoc, and update return a new map. keys returns the keys. Sorting them makes the print order stable.

(println (assoc person :city "London"))
(println (dissoc person :born))
(println (update {:n 1} :n inc))
(doseq [k (sort (keys person))]
  (println k (get person k)))

Map literals print between braces.

$ glj maps.glj
Ada
unknown
{:name Ada, :born 1815, :city London}
{:name Ada}
{:n 2}
:born 1815
:name Ada

Next example: Sets.