Understanding Sow and Reap documentation
Understanding Sow and Reap documentation
Both in Sow and in Reap documentation there is the example:
Sow
Reap
Reap[Sow[a]; b; Sow[c]; Sow[d]; e]
which gives the result e,a,c,d. What happens to b?
e,a,c,d
b
;
CompoundExpression
b; e
e
$begingroup$
It's ignored because it's followed by
;. This has nothing to do with Sow/Reap. It's not clear to me what you are asking / expecting.$endgroup$
– Szabolcs
Sep 10 '18 at 9:24
;
Sow
Reap
$begingroup$
@mattiav27 Maybe you are actually looking for something like this:
Reap[ Sow[a, "list1"]; Sow[b, "list2"]; Sow[c, "list1"]; Sow[d, "list1"]; Sow[e, "list2"], _, Rule ] ? This uses tags to tell to which list a sown expression should be appended.$endgroup$
– Henrik Schumacher
Sep 10 '18 at 9:31
Reap[ Sow[a, "list1"]; Sow[b, "list2"]; Sow[c, "list1"]; Sow[d, "list1"]; Sow[e, "list2"], _, Rule ]
1 Answer
1
I'll be totally honest with you and admit that I had no idea how these functions worked (or what the point of them was) for a long time, so you're not alone in your struggle trying to understand the documentation here. This is one of those things where you first need to build the right mental picture of the language before you can start making sense of the code you're seeing here.
In this situation, it's very important that you understand the difference between a returned value and a side effect. I'll take a rather large detour here before I get back to Sow and Reap, so please bear with me.
Sow
Reap
Every time an expression is evaluated, it produces a return value. So, for example, the expression 1 simply returns 1 and 1 + 1 returns 2. There is, however, also a special return value called Null which indicates that "nothing" was returned. In particular, if Null is returned from an evaluation, no output cell will be printed in the notebook. The most well-known way to suppress any return value and turn it into Null, is by finishing your input with ;.
1
1
1 + 1
2
Null
Null
Null
;
If you type the expression in a notebook cell, the return value will get printed in a new cell below the input except if the returned values is Null. You can still test that the returned value really is Null:
Null
Null
In[1]:= (1 + 1;) === Null
Out[1]= True
So what happens when you enter multiple expressions separated by semicolons? This is what is known in Mathematica as a CompoundExpression. You can verify this with Hold and FullForm:
CompoundExpression
Hold
FullForm
In[2]:= Hold[
1 + 1;
3
] // FullForm
Out[2]= Hold[CompoundExpression[1 + 1, 3]]
A compound expression simply evaluates each argument in turn and then returns the last result. You'll notice that if you finish the last expression with a semicolon as well, a Null will be tagged on the end:
Null
In[3]:= Hold[1 + 1; 3;] // FullForm
Out[3]= Hold[CompoundExpression[1 + 1, 3, Null]]
In this example, the 1 + 1 expression is really quite meaningless: it gets evaluated (if you remove the Hold, of course), but the result is never returned: it is simply forgotten about.
1 + 1
Hold
So is there any value in using CompoundExpression to chain expressions together? Yes, there is, but typically only when the expressions that don't get returned have side effects. The term "side effect" is basically an expensive-sounding term for things like variable/function assignment. If the evaluation of an expression changes how other expressions evaluate in the future, we say that we have produced a side effect. For example, if we evaluate x, we simply get x back:
CompoundExpression
x
x
In[4]:= x
Out[4]= x
However, if we Set a value to x by evaluating
Set
x
In[5]:= x = 1
Out[5]= 1
(or equivalently Set[x, 1]), 2 things happen:
Set[x, 1]
x
OwnValue
x
So Set produces a side effect AND a return value. The the side effect is obvious, since the same input in cell 4 now produces a different output:
Set
In[6]:= x
Out[6]= 1
To put everything together: when you chain up a compound expression like this:
expr1;
expr2;
expr3;
...
finalExpression
the idea is that all expressions expr1, expr2, ... etc. build up side effects that get used in the evaluation of finalExpression.
expr1
expr2
finalExpression
Ok, so what does this mean for the example from the documentation?
In[7]:= Reap[Sow[a]; b; Sow[c]; Sow[d]; e]
Out[7]= e, a, c, d
We have seen that the inner expression is just
CompoundExpression[Sow[a], b, Sow[c], Sow[d], e]
and the return value of this is obvious: just the last argument e. This is the value you see in as the first element in the list returned by Reap.
e
Reap
The second element of the list returned by Reap represents side effects from Sow. All that Sow does, is:
Reap
Sow
Sow
value
AppendTo[reapList, value]
value
The job of Reap is to collect all values stored inside of the internal data structure and spit it out in the second element of the list it returns.
Reap
Hopefully you now understand enough of the example to work out for yourself to read the rest of the Sow/Reap documentation to figure out how the tags work. It's a good exercise ;).
Sow
Reap
$begingroup$
Thanks for the expalantion!
$endgroup$
– mattiav27
Sep 10 '18 at 10:23
$begingroup$
Thanks for writing this!
$endgroup$
– user6546
Sep 11 '18 at 10:48
Thanks for contributing an answer to Mathematica Stack Exchange!
But avoid …
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
$begingroup$
;is the same asCompoundExpressionwhich returns the last argument. E.g.b; ereturnse. What output did you expect?$endgroup$
– Coolwater
Sep 10 '18 at 8:20