The Observer pattern revisited
In Chapter 1, What is Reactive Programming?, we saw a brief overview of the Observer design pattern and a simple implementation of it in Clojure using watches. Here's how we did it:
(def numbers (atom [])) (defn adder [key ref old-state new-state] (print "Current sum is " (reduce + new-state))) (add-watch numbers :adder adder)
In the preceding example, our observable subject is the var, numbers
. The observer is the adder
watch. When the observable changes, it pushes its changes to the observer synchronously.
Now, contrast this to working with sequences:
(->> [1 2 3 4 5 6] (map inc) (filter even?) (reduce +))
This time around, the vector is the subject being observed and the functions processing it can be thought of as the observers. However, this works in a pull-based model. The vector doesn't push any elements down the sequence. Instead, map
and friends ask the sequence for more elements. This is a synchronous operation...