A good intro on how to build parallelizable clojure code

Lau from bestinclass.dk has written another excellent article. I would highly recommend going Lau’s blog and reading every single one the articles in there, for their high educational value, especially if you are new –or even not so new– to Clojure in particular and Functional Programming in general.

The article referred to walks the reader through the construction of a Brian’s Brain graphical animation in 2D with only 68 lines of Clojure code. Yes, 68 lines.

The take-away of this article, in my opinion, is how Lau breaks down what would be an iterative problem into a functional one, only to then show how this functional problem can be easily parallelized by replacing a sequential map with a parallel map (pmap). Once your data is known to be immutable and you have broken the problem down to a functional one, the parallelization of your code execution is that easy. Lau later shows how this parallelized implementation lets clojure fully utilize all the cores available in your machine.

In the future, when asked about the suitability of functional programming and persistent data structures in today’s age, I will refer people to this article since it exposes the magic of the functional paradigm and how such paradigm can be leveraged today with multicore CPUs.

The discussion on how to create the graphical representation of Brain’s Brain is also interesting, and goes to show how good the interoperability between Clojure and Java can get.

I remember when learning functional programming back in school that the most difficult part of writing Lisp or ML code was not the coding itself, but the breaking down of the problem at hand into small pieces and then put those pieces together in a functional manner. I remember that the hard part of it was to unlearn the ‘loops’ that are so pervasive in imperative languages, and by unlearning those loops one’s brain could start thinking freely again. I recall those moments as ones of ecstasy. This article brought back to me some of those memories :)

“Brian’s functional brain”

Share this post:
  • Twitter
  • RSS
  • email
  • Reddit
  • Digg
  • FriendFeed
  • Print
  • HackerNews
  • Mixx
  • Google Bookmarks
  • DZone
  • LinkedIn
  • Posterous
  • StumbleUpon
  • Suggest to Techmeme via Twitter
  • Technorati
  • Facebook
  • Tumblr

3 Comments to “A good intro on how to build parallelizable clojure code”

  1. LauJensen 2 October 2009 at 4:09 am #

    Hi there,

    Since your posts aren’t signed I’m not sure who I am adressing, but thank you very much for that positive review. It’s always nice to see people take something away from my posts, so I’m very happy that you got something out of it.

    /Lau

  2. tbatchelli 2 October 2009 at 8:54 am #

    Lau, the articles are actually signed (by tbatchelli). I really enjoy your articles and I am happy to give them more exposure in exchange for the pleasure of reading them.

  3. LauJensen 2 October 2009 at 2:13 pm #

    Mr. Batchelli – I see you’re right of course :)

    You’ve got a deal – Good weekend!


Leave a Reply