The joyful Clojure example code convention

I started reading The Joy of Clojure, seems like a great book and I like its example code convention. I’m documenting it here because I’ll adopt it on my blog and I want to be able to link to it. I’ll call it the joyful Clojure example code convention.

A simple piece of Clojure code looks like this:

(* 2 10)

If you run it on the REPL it looks like this:

user> (* 2 10)
20

The first example, it’s just the code, with no return value, the second one, shows the return value, but when you have several lines it becomes cumbersome to copy and paste:

user> (* 2 10)
20
user> (* 2 11)
22
user> (* 2 12)
24

The Joy of Clojure code convention solves both problems by removing the prompt and leaving return values in comments:

(* 2 10)
;=> 20
(* 2 11)
;=> 22
(* 2 12)
;=> 24

Now you can copy and paste and still have the results.

If the snippet prints something, it will also be displayed but without the arrow, like this:

(println "Hello world")
; Hello world
;=> nil
Advertisements

8 Replies to “The joyful Clojure example code convention”

    1. I learned Common Lisp and Scheme, I even wrote my own little Lisp interpreter, in Objective-C. But that was years ago. Nevertheless I needed a book that would go faster and I wanted to learn the proper way to do things in Clojure, not just the way it works (for example, using (seq a) instead of (not (empty? a))) so your book is exactly what I needed. I’m liking it a lot.

    2. Oh, one more thing… I totally understood when you say, in the book, “if you don’t know Lisp, we envy you.”. I also envy people who don’t know Lisp and get to live the thrill and exciting of learning. I wish I could temporarily remove Lisp from my head just to live that again.

  1. “I started reading The Joy of Clojure, seems like a great book and I like it’s example code convention.”

    Um, it’s = it is.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s