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)?

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

1 Answer 1

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.

$\endgroup$

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