chapter seventeen
The functions illustrated in this section are at the heart of the Read, Eval, Print, Loop (REPL for short). When you open the Clojure REPL, you’re welcomed by a prompt waiting for a form to evaluate. That’s a call to read using the standard input as argument. After hitting the return key, eval analyzes the forms and emits bytecode that is put into execution. The result is printed on the screen and the loop start again. But even when you are not using the REPL, Clojure uses the same functions extensively to run a program. There are three main families of functions dedicated to evaluation (with some overlapping):
- read, read-string and eval work on a single form and produce an in-memory evaluation.
- compile, load, load-file, load-reader and load-string operate on a library as a group of forms [226]. compile also produces a file on disk.
- clojure.edn/read and clojure.edn/read-string are the equivalent read and read-string operations for EDN, the Extensible Data Notation. EDN is a subset of the Clojure syntax designed specifically for data transport.
read transforms a string (in this case a character stream) into the corresponding data structures, following the rules of the Clojure syntax. The character stream should be an instance of java.io.PushbackReader or subclasses. The easiest way to test the function is without arguments, forcing it to use the dynamic var in as input: