9

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

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
murray
  • 11,888
  • 2
  • 26
  • 50

1 Answers1

12

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.

ilian
  • 25,474
  • 4
  • 117
  • 186