memoize done right

My recent post on how to make memoize more flexible has sparked an interesting discussion about the actual implementation. Christophe Grand and Eugen Dück joined to discuss almost everything Clojure gives you as a tool to handle concurrency. Don't miss the evolution of a quite simple function in a non-concurrent world to a quite complex function in case multiple threads of execution are involved.

The Rule of Three

As Joshua Bloch says in his talk „API Design and why it matters“:

Write multiple plug-ins before release!

  • If you write one, it probably won't support another.
  • If you write two, it will support more with difficulty.
  • If you write three, it will work fine.

In a recent post of mine on the Clojure group I encountered exactly this rule. So let's explore it using the example of clojure.core/memoize.

Note: You might also be interested in the follow-up.

proxy – gen-class little brother

Clojure provides two basic ways to interface with the host platform. The more comprehending is gen-class which I touched in [a previous post]gc. It's little brother is proxy. Although less powerful it is more dynamic than gen-class. Let's see how it works…