defn basics in Clojure
Defines a baker's hydration percentage with a docstring and a private helper.
;; the string after the name is a docstring, stored on the var
(defn hydration
"Water as a percent of flour weight, rounded to a whole number."
[flour-g water-g]
(Math/round (* 100.0 (/ water-g flour-g))))
;; defn- keeps a helper private to this namespace
(defn- describe [pct]
(cond
(< pct 65) "stiff"
(< pct 80) "standard"
:else "slack"))
(def dough (hydration 500 375))
(println "hydration" dough "-" (describe dough))
(println (:doc (meta #'hydration)))
How it works
hydrationtakes[flour-g water-g]after its docstring and rounds the percentage.defn-makesdescribeprivate to the namespace, wherecondnames three ranges.(:doc (meta #'hydration))reads the docstring back off the var at the end.
Keywords and builtins used here
conddefdefndescribehydrationmetaprintln
The run, in numbers
- Lines
- 16
- Characters to type
- 459
- Tokens
- 77
- Three-star pace
- 80 tpm
At the three-star pace of 80 tokens a minute, this run takes about 58 seconds.
Step 1 of 3 in Functions, step 7 of 27 in Language basics.