At the most recent meeting of TriClojure, I had the pleasure of chatting with Michael Fogus, one of the authors of The Joy of Clojure. I mentioned that I had done some Mathematica (Mma) programming, and Michael asked me about a couple of Mma constructs called Sow and Reap. Sow and Reap are always used in tandem, and together form a powerful debugging tool. I’ll try to illustrate the what they do with some simple examples.

By itself, Sow behaves much like the identity function:

In[1]:= 3 + Sow[4]
Out[1]= 7

To make Sow useful, we have to enclose it inside a call to Reap:

In[2]:= Reap[3 + Sow[4]]
Out[2]= {7, {{4}}}

Disregarding all the nesting for a moment, notice we got back the result of the computation, 7, and the value of the expression wrapped in a Sow call, 4. Together Sow and Reap let you easily examine intermediate results inside of computations. Let’s look at a more complicated example.

First we’ll define a couple of zero argument random functions; one returns an even integer, the other an odd integer.

randEven[] := 2 * Random[Integer, {1, 5}]
randOdd[] := randEven[] - 1

Next we define a function of one argument which uses both of the functions above.

f[x_] := x + randEven[] + randOdd[] + randEven[]

Evaluating f[2] will return a random integer. What if we wanted the option to know what random intermediate values were used to compute the final output of f[2]? We can use Sow and Reap to instrument f in a way that does not disturb its normal behavior, but allows us to look inside a call to f by simply wrapping that call in a Reap.

Here’s the instrumented version of f:

f[x_] := x + Sow[randEven[]] + Sow[randOdd[]] + Sow[randEven[]]

The revised version of f behaves exactly like the original version: it returns a random integer. But if we want to look inside a call to f, we can wrap it it a Reap:

In[4]:= Reap[f[2]]
Out[4]= {15, {{2, 5, 6}}}

We see the final value of the call, 15, and the three intermediate results, {2,5,6}. But why does the Reap output have the double nesting {{2, 5, 6}}? It is because we have the option to decorate our calls to Sow with tags of our choosing, and Reap will collect the values in separate buckets for us.

Here’s the idea. Let’s use tags to separate the odds and evens; the tags can be arbitrary.

f[x_] := x + Sow[randEven[], foo] + Sow[randOdd[], bar] + Sow[randEven[], foo]

Now a call to Reap shows us the odds and evens in separate bins as it were:

In[6]:= Reap[f[2]]
Out[6]= {15, {{4, 2}, {7}}}

The instance of Reap used to enclose our Sow calls need not immediately enclose:

In[9]:= Reap[Min[0, f[2]]]
Out[9]= {0, {{2, 10}, {5}}}

My examples are contrived, but I think they illustrate the idea. A bit of thought might just make you wish you had this facility available in your favorite language. Reap and Sow can be just the thing for getting insight into what is going on inside a computation.

And I have to give mad props to whomever picked such evocative names as Sow and Reap. I am reminded of Hosea 8: “They sow the wind, and reap the whirlwind”. Pure genius.