A Clojure keyword is a named immutable value commonly used as a map key. It is also callable: given associative data, it looks up the value stored under itself. section 15.1 section 8.2.2
Use a keyword directly as a function when its lookup role makes the code clearer. It accepts a map or another associative structure and can receive a fallback value when the key is absent. section 8.2.2
Lookup with a keyword
(:b {:a 1 :b 2}) ;; 2 (:c {:a 1 :b 2} "Not found") ;; "Not found"
Keywords fit several related operations:
- Use them as map keys and invoke them for concise associative lookup. section 8.2.2
- Use find when the result must preserve both the key and its value as a map entry. section 8.2.2
Qualified keywords
(def bx (keyword "b" "x")) [(namespace bx) (name bx)] ;; ["b" "x"] section 15.1
(symbol "s") ; #1 ;; s (keyword "k") ; #2 ;; :k
Compare constructing a symbol and a keyword, including the keyword's printed form. section 15.1
