Managing program complexity with STM
The basis of Clojure's concurrency is its STM system. Basically, this extends the semantics of database transactions to the computer's memory.
For this recipe, we'll use the STM to calculate the families per housing unit from a piece of U.S. census data. We'll use future-call
to perform the calculations in the thread pool and spread the execution over multiple cores. Afterwards, we'll go into more detail about how the STM works in general, and how it's applied in this particular recipe.
Getting ready
To prepare for this recipe, we first need to list our dependencies in the Leiningen project.clj
file:
(defproject concurrent-data "0.1.0-SNAPSHOT" :dependencies [[org.clojure/clojure "1.6.0"] [org.clojure/data.csv "0.1.2"]])
We also need to import these dependencies to our script or REPL:
(require '[clojure.java.io :as io] '[clojure.data.csv :as csv])
Finally, we need to have our data file. I downloaded one of the bulk data files from...