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,…}],…}}.
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 all close allBasic Examples (2)
Scope (2)
Make a separate sublist for each tag being reaped:
Reap[Sow[1, {x, x}];Sow[2, y];Sow[3, x], {x, x, y}]Count the number of instances of each integer separating the negative:
Reap[Sow[1, RandomInteger[{-9, 9}, 100]], {_ ? Negative, _ ? NonNegative}, #1 -> Total[#2]&]Generalizations & Extensions (1)
Applications (2)
Find the list of values sampled by Plot:
Reap[Plot[Sin[x], {x, 0, 10}, EvaluationMonitor :> Sow[x]];]//ShortRemove duplicates from a list, keeping original order:
unsortedUnion[x_] := Reap[Sow[1, x], _, #1&][[2]]unsortedUnion[{b, b, c, a, c, a, b, d}]This is like Union without the sorting:
Union[{b, b, c, a, c, a, b, d}]Properties & Relations (3)
If no expressions are sown, Reap returns an empty list of expressions it has reaped:
Reap[x]Reap collects expressions in exactly the order they are sown:
Reap[MapAll[Sow, (a + b)(c + x ^ 2)];]The list associated with the first tag to be encountered is given first:
Reap[Sow[1, y];Sow[2, x];Sow[3, y]]Reap[Sow[1, y];Sow[2, x];Sow[3, y], _, Rule]Tech Notes
Related Guides
History
Introduced in 2003 (5.0)
Text
Wolfram Research (2003), Reap, Wolfram Language function, https://reference.wolfram.com/language/ref/Reap.html.
CMS
Wolfram Language. 2003. "Reap." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/Reap.html.
APA
Wolfram Language. (2003). Reap. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/Reap.html
BibTeX
@misc{reference.wolfram_2026_reap, author="Wolfram Research", title="{Reap}", year="2003", howpublished="\url{https://reference.wolfram.com/language/ref/Reap.html}", note=[Accessed: 12-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_reap, organization={Wolfram Research}, title={Reap}, year={2003}, url={https://reference.wolfram.com/language/ref/Reap.html}, note=[Accessed: 12-June-2026]}