Hitchhiker's Guide to Clojure

Don't Worry About the Parens

Hitchhiker's Guide to Clojure - Part 2

Amy and Frank were hurtled quite rapidly through time and space after attaching themselves to a transaction headed through the Datomic Transactor. From there things slowed down a bit, then took a sharp left and ricocheted off again with incredible speed until they landed in another Datomic Peer, and finally appeared in the same room. Amy was quite startled by the anti-climatic nature of the whole dematerializing and rematerializing in the same exact spot, and didn’t really know what to do next. She surveyed her office and found it exactly the same, except for two distinct details. For one, the pistachio shells had disappeared, and for another, the date on the computer showed yesterday at 8:00 am. She tried to connect these facts rationally with the pistachios in her pocket and finally said,

“I am about to come into work.”

Frank, who was busily hunting through his blue zippered pack around his waist, looked up briefly.

“Well, we better get out of here then, I only have a blue fanny pack.”

The Hitchhiker’s Guide to Clojure explains that the “fanny pack”, or “bum bag”, is the symbol of a licensed Chrono-agent. The rank of the Chrono-agent can be clearly determined by its color on the ROYGBIV scale.

The origins of this licensing method can be traced to an embarrassing incident in human history known as “The Great Flood”. A junior Chrono-agent was trying to increase the yield of a tomato crop during a dry spell and was trying to do the following recursive function in his evaluator:

1
2
3
4
5
6
7
8
9
10
11
12
(defn rain [days]
  (when (pos? days)
    (println (str "Rain: " days))
    (rain (dec days))))

(rain 5)
;;Output
;;  Rain: 5
;;  Rain: 4
;;  Rain: 3
;;  Rain: 2
;;  Rain: 1

Unfortunately, he made the rookie mistake of forgetting to decrement the days before passing it to the recursive function.

1
(dec 5) ;=> 4

The result of which was severely overwatered tomatoes.

1
2
3
4
5
6
7
8
9
10
11
12
(defn rain [days]
  (when (pos? days)
    (println (str "Rain: " days))
    (rain days)))

(rain 5)
;;  Rain: 5
;;  Rain: 5
;;  Rain: 5
;;  Rain: 5
;;  Rain: 5
;;  ...(you get the idea)

It is interesting to note that he could he written the same function with a recur instead.

1
2
3
4
5
6
7
8
9
10
11
12
(defn rain [days]
  (when (pos? days)
    (println (str "Rain: " days))
    (recur days)))

(rain 5)
;;Output
;;  Rain: 5
;;  Rain: 5
;;  Rain: 5
;;  Rain: 5
;;  Rain: 5

That would have had the nice effect of not consuming the stack, (which is fabulous for constructing those lovely fibonacci sea shells for beach vacations), but without decrementing the parameter in the recursive call, it wouldn’t have really helped.

A senior Chrono-agent was dispatched to sort out the mess. By the time he got there and stopped the rain, there was not much left to work with. Thankfully, he was quite versed in lazy and infinite aspects of Clojure. For instance, take this vector of 2 chickens:

1
[:hen :rooster]

It can be transformed into an infinite lazy list of chickens by using cycle.

1
(cycle [:hen :rooster])

What really set the senior Chrono-agent apart from his junior colleague, was that he did not put the infinite sequence in the evaluator. If he had, there would have been another embarrassing incident in human history, this time involving an over-abundance of poultry. Instead, he used take to get the first n infinite chickens.

1
2
3
4
5
(take 5 (cycle [:hen :rooster]))
;;=> (:hen :rooster :hen :rooster :hen)
(take 10 (cycle [:hen :rooster]))
;;=> (:hen :rooster :hen :rooster :hen :rooster :hen :rooster :hen
:rooster)

After that, the council of Chrono-agents, decided to license evaluator use. Low-level recursion requires the 2nd highest indigo level rank. The highest violet rank, of course, belonging only to the Macro Masters. All lower levels are required to stick to the safer, higher level abstractions like for, map, or reduce.

Amy was still watching Frank busily rumaging through his pack in the office . Finally he found what he was looking for, his hand emerging triumphantly with a fistful of mini-marshmallows.

“Got it. Come on, let’s go! Someone is trying to destroy the world and we need to see the otters.”