Master Concurrent Processes with core.async
From the post:
One day, while you are walking down the street, you will be surprised, intrigued, and a little disgusted to discover a hot dog vending machine. Your scalp tingling with guilty curiosity, you won’t be able to help yourself from pulling out three Sacagawea dollars and seeing if this contraption actually works. After accepting your money, it will emit a click and a whirr, and out will pop a fresh hot dog, bun and all.
The vending machine exhibits simple behavior: when it receives money, it releases a hot dog, then readies itself for the next purchase. When it’s out of hot dogs, it stops. All around us are hot dog vending machines in different guises, independent entities concurrently responding to events in the world according to their nature. The espresso machine at your favorite coffee shop, the pet hamster you loved as a child – everything can be modeled in terms of behavior using the general form, “when x happens, do y.” Even the programs we write are just glorified hot dog vending machines, each one an independent process waiting for the next event, whether it’s a keystroke, a timeout, or the arrival of data on a socket.
Clojure’s core.async library allows you to create independent processes within a single program. This chapter describes a useful model for thinking about this style of programming as well as the practical details you need to actually write stuff. You’ll learn how to use channels, alts, and go blocks to create independent processes and communicate between them, and you’ll learn a bit about how Clojure uses threads and something called “parking” to allow this all to happen efficiently.
One of the things I like the best about CLOJURE for the BRAVE and TRUE by Daniel Higginbotham is the writing style.
Yes, most processes are more complicated than a hot dog vending machine but then the inside of a hot dog vending machine if probably more complicated than you would think as well. But the example captures enough of the essence of the vending machine to make it work.
That’s a rare authoring talent and you should take advantage of it whenever you see it. (As in Daniel’s Clojure site.)