Follow @nonrecursive to hear about new content or subscribe:

Buy the (beta) ebook!?

Hello, brave and true reader!

Someone recently informed me that it's not completely crazypants to charge a sustainable amount for high quality programming content. This was news to me, but I thought, hey, why not give it a shot?

The First Part's Free

For this book on parallel programming, I'm releasing the Introduction and Part 1 for free (I guess I haven't really absorbed the lesson?). Part 1 is a practical tutorial that will teach you how to use reducers; I want every Clojurist to be able to use this useful tool. If you want Parts 2 and 3, you'll need to buy the (beta) ebook.

The ebook is where the awesome is

One of the reasons you love Clojure is that it makes advanced (but relevant) programming concepts and techniques accessible. It is mentally stimulating and fun and actually useful. In Parts 2 and 3, you will explore exciting new programming vistas. Recall how you felt learning about Clojure's state model, or learning about programming with pure functions. You'll get that sense of wow! and holy schnitzel, that's amazing! in the paid parts of the book.

Parallel programming has become more and more relevant because of the inexorable lurch toward multi-core processors. You'll learn about parallel programming concepts and techniques in Part 2, adding an invaluable tool to your mental toolkit. These ideas are universal; you can apply them outside Clojure.

In Part 3 (unreleased; still in progress), you'll look at how the reducers library is implemented, and in the process explore some grade-A functional programming voodoo. You'll also see how protocols and reification in Clojure can be put to heart-breakingly elegant use. Once I release Part 3, I'll send you an email and you'll be able to download an update. I plan on finishing the book by October 2017. The book is currently about 68 pages, and I plan on adding 50-70 more.

Writing this stuff is a ton of work, and if you like what you read, want to learn more, and want to help me finish this book, then please purchase an ebook. When Gumroad sends me the email notifying me of your purchase, I'll show my thanks by printing out your email address and drawing a heart around it. Thank you for your support!

— Daniel Higginbotham, programmer, author, and heart drawer

Intro

Welcome, new employee, to most innovative startup on the planet! I’m so glad to have bestowed upon you the greatest achievement of your life: being chosen to work here.

Now that you’re an official team/family member, you get to enter our Startup Stealth Tent and learn what you’ll actually work on. Our plan is to disrupt the ancient field of palm reading with a mobile app. This engineering marvel will let a user position their phone so that the camera faces their palm and takes a picture, then predicts the future. I call it: iFacePalm.

The real value of our paradigm-annihilating app will lie as much in its accuracy as its ease of use, so we’ll need to develop an accurate palm-based human life prediction model. And to do that, we’re going to need to process a poop-ton of data, comparing people’s palm stats with their entire life history to reveal the connections that without a doubt exist.

Processing poop-tons of data is exactly what parallel programming is good for, and to get you onboarded I’ve written this ebook to explain it. In truth, though, it’s not just important for our app; parallel programming has become an essential tool for modern hackers thanks to fundamental changes in computer hardware. (Not that you’ll ever need to work on anything else once the money deluge sweeps us away.)

In Clojure for the Brave and True I wrote a bit about concurrent and parallel programming, or “the sacred art of structuring your application to safely manage multiple, simultaneously executing tasks.” That book focused on state-management hassles and how Clojure can help you deal with them, and it took only brief forays into the world of parallelism.

This book focuses on performance and parallelization, and it will show you how to structure your application so that it will get better performance by automatically dividing work among all CPU cores. In the pages (screens? not-yet-scrolled-to portion?) ahead, you’ll learn about Clojure’s core.reducers library, a great option for doing parallel computation. Whereas clojure.core provides only pmap to parallelize mapping, I’ll show you how to use reducers to parallelize take, filter, and more.

My larger goal is for you to understand the reducers library completely; if I only show you how to use reducers, I’ll have failed you as an author and as an iconoclastic visionary. The world of parallel programming is fascinating, and this article will take you on a thorough tour of it so you’ll understand how the reducers library fits into the broader computing landscape. To that end, this book is divided into three parts: Practice, Theory, and Implementation.

In part one, Practice, I’ll start you off with a tutorial that will give you a good understanding of how to use the library. You’ll learn about what the functions do, when to use them, and unexpected behavior to watch out for. I’ll also compare reducer performance to serial counterparts.

This will give you enough to hang your hat on; it will give you a good concrete reference point to make sense of the more abstract discussion that will follow. Plus, it will inspire the kind of pulse-pounding edge-of-your-seat suspense that you haven’t felt since Lost went off the air as your brain scrambles to answer the question But how do reducers do that? (Probably.) And if you somehow manage to stop reading after the tutorial, you’ll still have learned enough to improve your code.

In part two, Theory, you’ll jump into the more conceptual portion by learning all about parallel performance. You’ll learn more about why it matters, and some general performance strategies that can be applied to any language, not just Clojure. You’ll dig deep into data parallism, where you’ll learn about the work-span model, one of the theoretical models used to reason about parallel performance. It’s not all theory, though; you’ll also learn about the practical approaches to writing parallel programs. I’ll discuss how you can achieve the balance between minimizing overhead and load balancing using thread management, granularity, parallel slack, tiling, and fusion. You’ll learn about the executors in Java’s java.util.concurrent package, which Clojure uses extensively.

All of this fun prep work will have you primed to understand the fork/join framework. You’ll learn how fork/join implements many of the techniques mentioned above and adds a couple more to the mix, including recursive decomposition and work stealing.

In Part three, Implementation, I’ll reach out my hand to you like Aladdin from Disney’s Aladdin and ask, “Do you trust me?”, then take you on a magic carpet ride through the surprisingly succinct reducers codebase. It’s always worthwhile to read Clojure’s source, and the reducers library is particularly delightful because it uses functional programming and Clojure’s protocols in ways that, personally, I find exhilirating. (Actually, the first time I worked my way through the code it my head hurt, but that’s pretty much the same as exhilirating, right? Hopefully, with me as your guide you’ll get all of the joy and none of the headache.)

As a foretaste: the reducers library relies on functions that return functions that return functions. It’s like Leonardo DiCaprio’s character in Inception had a baby with that Yo Dawg meme guy, and it was raised by John McCarthy using a parenting manual written by Alonzo Church. In short, I give you my word as your new boss and as a purveyor of fine ideas that part three will help you understand how to use Clojure and functional programming to write elegant code.

After going through the implementation code, your brain, which is a parallel processing machine, will fully understand Clojure’s parallel processing library. You will then enjoy a moment of smug assurance that your brain is able to appreciate this fact, while Clojure’s reducers library cannot. And then you will experience the onset of existential angst as, unbidden by quote-unqoute “you”, your brain spontaneously excretes the thought, But for how long?

Luckily, you’ll have the tools to start building our fortune-telling app, so you’ll actually be able to know how much time you’ll have to enjoy the bajillionteen bucks we’ll make selling this Nobel-worthy consumer application before our silicon successors take the helm of starship Earth and we go the way of Australopithecus.

Sounds super fun! Let’s get started!

Chapters
  1. Intro
  2. Chapter 1: Know Your Reducers
  3. Chapter 2: Performance and Parallelism (ebook-only)
  4. Chapter 3: Fork/Join and Implementation (in progess; ebook-only)