Variations on the Theme

In addition to the plain vanilla version of let that we’ve looked at so far, Clojure comes packaged with a couple of handy variations. The most commonly used of these is probably if-let. As you might guess from the name, if-let is an if and a let rolled into one. To see if-let in action, imagine that we decide to represent anonymous books with our now-familiar book map, sans the :author key:

 (​def​ anonymous-book
  {:title ​"Sir Gawain and the Green Knight"​})
 
 (​def​ with-author
  {:title ​"Once and Future King"​ :author ​"White"​})

Now imagine we needed to write a function that will return the uppercase version of the author’s name, or nil if there is no author. The twist is that we need to avoid computing the uppercase ...

Get Getting Clojure now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.