Skip to Main Content
Programming Clojure, 3rd Edition
book

Programming Clojure, 3rd Edition

by Alex Miller, Stuart Halloway, Aaron Bedra
February 2018
Intermediate to advanced content levelIntermediate to advanced
304 pages
7h 11m
English
Pragmatic Bookshelf
Content preview from Programming Clojure, 3rd Edition

Writing a Control Flow Macro

Clojure provides the if special form as part of the language:

 (​if​ (= 1 1) (println ​"yep, math still works today"​))
 | yep, math still works today

Some languages have an unless, which is (almost) the opposite of ifunless performs a test and then executes its body only if the test is logically false.

Clojure doesn’t have unless, but it does have an equivalent macro called when-not. For the sake of having a simple example to start with, let’s pretend that when-not doesn’t exist and create an implementation of unless. To follow the rules of Macro Club, begin by trying to write unless as a function:

 ; This is doomed to fail...
 (​defn​ unless [expr form]
  (​if​ expr nil form))

Check ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Programming Kotlin

Programming Kotlin

Venkat Subramaniam
Getting Clojure

Getting Clojure

Russ Olsen

Publisher Resources

ISBN: 9781680505719Errata Page