It’s Made of Atoms

Let’s start with that simple hit-counter application. Here is a basic Ring web application that counts the number of visitors to the site and congratulates every 100th visitor:

 (​def​ counter 0)
 (​defn​ greeting-message [req]
  (​if​ (zero? (mod counter 100))
  (str ​"Congrats! You are the "​ counter ​" visitor!"​)
  (str ​"Welcome to Blotts Books!"​)))
 ;; ect

Except that it doesn’t. The trouble is that counter stays firmly bound to zero and it’s not clear how we can fix that. And as we’ve discussed any number of times, we cannot yield to the temptation of slipping (def counter (inc counter)) in greeting-message: vars are there to hold relatively stable values and a hit counter is anything but stable. ...

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.