April 2012
Intermediate to advanced
296 pages
7h 3m
English
The best way to appreciate multimethods is to spend a few minutes living without them, so let’s do that. Clojure can already print anything with print/println. But pretend for a moment that these functions do not exist and that you need to build a generic print mechanism. To get started, create a my-print function that can print a string to the standard output stream *out*:
| src/examples/life_without_multi.clj | |
(defn my-print [ob] | |
(.write *out* ob)) | |
Next, create a my-println that simply calls my-print and then adds a line feed:
| src/examples/life_without_multi.clj | |
(defn my-println [ob] | |
(my-print ob) | |
(.write *out* "\n")) | |
The line feed makes my-println’s output easier to read when testing at the REPL. ...
Read now
Unlock full access