philipbohun.com

Glojure by Example: Keywords and Symbols

Keywords are names that evaluate to themselves. They are the usual keys in maps, and they stand in for the enumerations a Go program would build with iota. A keyword is also a function: it looks itself up in a map.

(println :status)
(println (:status {:status "ok"}))
(println (:missing {:status "ok"} "gone"))

A set of keywords models a closed group of choices. conj of a member that is already present does not grow the set.

(def colors #{:red :green :blue})
(println (contains? colors :red))
(println (contains? colors :purple))
(println (count (conj colors :red)))
(println (count (conj colors :purple)))

A symbol is a name for something in a program. Quoting it with ' stops evaluation, so you get the symbol itself. keyword and name convert between strings and keywords.

(println 'map)
(println (keyword "status"))
(println (name :status))

Keywords print with a leading colon.

$ glj keywords.glj
:status
ok
gone
true
false
3
4
map
:status
status

Next example: Conditionals.