O'Reilly logo

Clojure: High Performance JVM Programming by Akhil Wali, Shantanu Kumar, Eduardo Díaz

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Reading and evaluating code

Let's have a look at how code can be parsed and evaluated in Clojure. The most elementary way to convert text into an expression is by using the read function. This function accepts a java.io.PushbackReader instance as its first argument, as shown here:

user> (read (-> "(list 1 2 3)"
                .toCharArray
                java.io.CharArrayReader.
                java.io.PushbackReader.))
(list 1 2 3)

Note

These examples can be found in src/m_clj/c4/read_and_eval.clj of the book's source code.

In this example, a string containing a valid expression is first converted into an instance of java.io.PushbackReader and then passed to the read function. It seems like a lot of unnecessary work to read a string, but it is due to the fact that the read function deals ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required