Reap

Reap[expr]

gives the value of expr together with all expressions to which Sow has been applied during its evaluation. Expressions sown using Sow[e] or Sow[e,tagi] with different tags are given in different lists.

Reap[expr,patt]

reaps only expressions sown with tags that match patt.

Reap[expr,{patt1,patt2,}]

puts expressions associated with each of the patti in a separate list.

Reap[expr,patt,f]

returns {expr,{f[tag1,{e11,e12,}],}}.

Details

  • Sow and Reap provide a convenient way to accumulate a list of intermediate results in a computation.
  • Reap accumulates expressions in the order in which Sow is applied to them.
  • Expressions sown with a particular tag are collected by the innermost Reap whose pattern matches the tag.
  • Reap[expr] is equivalent to Reap[expr,_].
  • Reap has attribute HoldFirst.

Examples

open allclose all

Basic Examples  (2)

Evaluate a sequence of expressions, "reaping" ones that have been "sown":

In[1]:=
Click for copyable input
Out[1]=

Compute a sum, "reaping" i^2 "sown" at each step:

In[1]:=
Click for copyable input
Out[1]=

Scope  (2)

Generalizations & Extensions  (1)

Applications  (2)

Properties & Relations  (3)

See Also

Sow  Catch  AppendTo  Print

Tutorials

Introduced in 2003
(5.0)