def & names in Clojure
Names a market stall's inventory with def, deriving one value from the others.
;; def interns a name in the namespace; names use hyphens, not camelCase
(def stall-name "Ridgeline Orchard")
(def crates-on-hand 12)
(def apples-per-crate 40)
;; a def can be computed from names defined above it
(def apples-on-hand (* crates-on-hand apples-per-crate))
;; a trailing ? marks a name that answers yes or no
(def restock-needed? (< crates-on-hand 15))
;; a trailing ! marks a name tied to something that changes
(def sold-today! (atom 0))
(swap! sold-today! + 96)
(println stall-name "has" apples-on-hand "apples")
(println "restock?" restock-needed? "- sold" @sold-today!)
How it works
(def stall-name "Ridgeline Orchard")interns a name in the namespace, hyphenated rather than camelCase.apples-on-handis computed as(* crates-on-hand apples-per-crate)from the two defs above it.restock-needed?ends in a question mark for a yes-or-no value, andsold-today!in a bang for the atom thatswap!updates.
Keywords and builtins used here
atomdefprintln
The run, in numbers
- Lines
- 17
- Characters to type
- 591
- Tokens
- 66
- Three-star pace
- 75 tpm
At the three-star pace of 75 tokens a minute, this run takes about 53 seconds.
Step 1 of 3 in Values & bindings, step 1 of 27 in Language basics.