Destructuring and Working with Keywords
Destructuring and keywords are everyday tools in idiomatic Clojure. This lesson teaches how to pull values out of maps and vectors cleanly and use keywords as fast, self-documenting keys.
Keywords as Keys
A keyword starts with a colon, like :name. Keywords are interned, compare fast, and evaluate to themselves.
(def user {:name "Ada" :role :admin})
(println user)Keywords Are Functions
A keyword is also a function: (:name user) looks itself up in a map — the most common way to read a value.
(def user {:name "Ada" :role :admin})
(println (:name user))
(println (:missing user :default))All lessons in this course
- Introduction to Clojure & JVM
- Core Data Structures & Syntax
- REPL-Driven Development Workflow
- Destructuring and Working with Keywords