Varying Your Vars
If vars are all about providing the global, stable environment for your code, you might wonder why vars are mutable. After all, Clojure loves immutability. But we can def and re-def our vars with wild abandon. The answer is as simple as it is pragmatic: mutable vars make for more productive Clojure programmers. Most Clojure programming is done in some form of REPL or other. So while developing, we might start out by creating a couple of vars:
| user=> (def PI 3.14) |
| #'user/PI |
| |
| user=> (defn compute-area [diameter] |
| #_=> (* PI diameter diameter)) |
| #'user/compute-area |
and then realize that we need more precision:
| user=> (def PI 3.14159) |
| #'user/PI |
and we also got the calculation wrong:
| user=> (defn |
Get Getting Clojure now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.