pr and prn

NOTE: All the code in this tutorial can be quickly tested at the REPL using the command lein repl  to start a REPL to test with.

One of the first things you learn when learning clojure is print  and println . For example,

That’s probably the first Clojure program you ever wrote. Maybe you wrote this variation.

Because println  or print are the first ways learned of displaying output in Clojure, most Clojure developers continue using them when debugging code. Nothing wrong with that. But sometimes it is better to use a more specialized tool.

In steps the prn  and pr  commands. These two commands are tailored for printing objects. At first glance, they appear to work the same as print  and println . However, they actually show more information in some instances.

If using print  or println , the \n  in the following code quietly gets replaced by the action of a new line being printed.

When pr  or prn  are used, the two characters \  and n  are displayed instead of an actual new line. This is very handy when hidden characters are fowling up your layout, or needed to be visible when debugging code.

Final note: The only difference between pr  and prn , is that prn  prints a new line after the objects it is printing out.

 

Clojure REPL for Development

A common way to develop code (don’t blame the messenger) is to start the application watch for the logged errors, make changes and then restart the application again, looking for more logged errors.

That’s not how it’s done in the world of Clojure, not just because it’s bad, but because the process of starting any Clojure program is horribly slow. Instead, in Clojure normally you start the application and then connect with a terminal window running a Clojure REPL. For example, if you are running a Luminus server, when you start it, it will tell you what port the server is listening for REPL connections on.

Continue reading “Clojure REPL for Development”