9
$\begingroup$

A minimal (toy) example for my question:

    i= 1;
    Reap[Do[Sow[i = (i + 1)^2], {4}]]
(* {Null, {{4, 25, 676, 458329}}} *)

I presume the entire result is a list with first entry Null because the Do loop returns Null.

But why is the result of the Reap the nested list {{4, 25, 676, 458329}} rather than just {4, 25, 676, 458329}?

An even simpler example:

    Reap[Sow[i = 2]]
(*  2, {{2}}} *)

Why last entry {{2}} instead of {2} (or perhaps even just plain 2)?

| improve this question | | | | |
$\endgroup$
  • 10
    $\begingroup$ The second element is a list of lists because there may be several different tags sown. $\endgroup$ – ilian Sep 3 '15 at 0:47
  • 2
    $\begingroup$ Example Reap[Sow[1, {x, y}]; Sow[2, y]; Sow[3, x], _, tag] $\endgroup$ – Dr. belisarius Sep 3 '15 at 0:48
  • 4
    $\begingroup$ Related to this $\endgroup$ – Ymareth Sep 3 '15 at 13:56
  • $\begingroup$ Some related answers of mine which I hope may be helpful: (6415), (31076), (44118), (67625) $\endgroup$ – Mr.Wizard Sep 3 '15 at 19:08
12
$\begingroup$

To summarize the comments into an answer:

The second element is a list of lists because there may be several different tags sown. For example,

Reap[Sow[1, x]; Sow[2, y]; result]

(* {result, {{1}, {2}}} *)

Another example by belisarius,

Reap[Sow[1, {x, y}]; Sow[2, y]; Sow[3, x], _, tag]

(* {3, {tag[x, {1, 3}], tag[y, {1, 2}]}} *)

See also this previous question pointed out by Ymareth.

| improve this answer | | | | |
$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.