Book Image

Clojure Data Analysis Cookbook - Second Edition

By : Eric Richard Rochester
Book Image

Clojure Data Analysis Cookbook - Second Edition

By: Eric Richard Rochester

Overview of this book

Table of Contents (19 chapters)
Clojure Data Analysis Cookbook Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Debugging concurrent programs with watchers


Watchers are not only good for logging and counting, but they are also useful for debugging. If an agent or reference is not getting updated the way we expect, we can temporarily add a watcher to track what's happening so we can see what's going on.

For this recipe, we'll continue the theme we've been working with for the last few recipes. This time, instead of counting the data, the watch function will print the change of state to the console.

Getting ready

We'll use the dependencies and requirements that we did in the Monitoring processing with watchers recipe. For this recipe, we just need to add a two more functions to them.

How to do it…

  1. The main difference from the last recipe is the watch function. Here's the new one:

    (defn debug-watch [watch-key watch-agent old-state new-state]
      (let [output (str watch-key
                        ": "
                        (pr-str old-state)
                        " => "
                        (pr-str new-state)
       ...