Skip to content

Reap ​

Evaluates an expression together with everything Sow recorded during it.

✓ checked against that system · ! checked, with disagreements (hover a chip)Wolfram LanguageReap✓
Reap(expr){value,{{sown,...},...}}\{value, \{\{sown, ...\}, ...\}\}: exprexpr's value, and one list per distinct tag Sow used while evaluating it (untagged sows share one group), in first-appearance order. {value,{}}\{value, \{\}\} if nothing was sown.
Reap(expr, tag)Like Reap(expr)Reap(expr), but the second element is the list of groups whose tag matches tagtag exactly. With a list of tags it holds one such list per tag, so a tag nothing was sown under gives {}\{\} in its place. Sow(e,{tag1,tag2})Sow(e, \{tag1, tag2\}) sows under each of its tags.
Typed signatures
TypePackageApplies
(any, any?) -> anycombinatoricsalways

Domain: Collections

Details
  • Only exact tag equality is matched here, not Wolfram's fuller pattern-matching form of the second argument.
  • Sequencing several SowSows in one exprexpr: use [Last,[List,...]][Last, [List, ...]] (Wolfram's a;b;ca; b; c), not compute-engine's own BlockBlock — see Sow's examples and control.ts's module doc for why.

Examples

Basic #

collect everything sown, untagged sows sharing one group

live ↗
InReap(Last([Sow(1),Sow(2),3]))\mathrm{Reap}(\mathrm{Last}(\bigl\lbrack\mathrm{Sow}(1), \mathrm{Sow}(2), 3\bigr\rbrack))Out[3,[[1,2]]]\bigl\lbrack3, \bigl\lbrack\bigl\lbrack1, 2\bigr\rbrack\bigr\rbrack\bigr\rbrack

nothing sown gives an empty group list

live ↗
InReap(5)\mathrm{Reap}(5)Out[5,[]]\bigl\lbrack5, \bigl\lbrack\bigr\rbrack\bigr\rbrack
live ↗
InReap(∑k=10(Sow(k2)+1))\mathrm{Reap}(\sum_{k=10}(\mathrm{Sow}(k^2)+1))Out[395,[[1,4,9,16,25,36,49,64,81,100]]]\bigl\lbrack395, \bigl\lbrack\bigl\lbrack1, 4, 9, 16, 25, 36, 49, 64, 81, 100\bigr\rbrack\bigr\rbrack\bigr\rbrack
Scope #

Reap(expr, tag) collects only the matching tag's group

live ↗
InReap(Last([Sow(1,a),Sow(2,b),Sow(3,a),0]),a)\mathrm{Reap}(\mathrm{Last}(\bigl\lbrack\mathrm{Sow}(1, a), \mathrm{Sow}(2, b), \mathrm{Sow}(3, a), 0\bigr\rbrack), a)Out[0,[[1,3]]]\bigl\lbrack0, \bigl\lbrack\bigl\lbrack1, 3\bigr\rbrack\bigr\rbrack\bigr\rbrack

A list of tags gives one list of groups per tag; a tag nothing was sown under gives {}\{\}

live ↗
InReap(Last([Sow(1,a),Sow(2,b),0]),[a,c])\mathrm{Reap}(\mathrm{Last}(\bigl\lbrack\mathrm{Sow}(1, a), \mathrm{Sow}(2, b), 0\bigr\rbrack), \bigl\lbrack a, c\bigr\rbrack)Out[0,[[[1]],[]]]\bigl\lbrack0, \bigl\lbrack\bigl\lbrack\bigl\lbrack1\bigr\rbrack\bigr\rbrack, \bigl\lbrack\bigr\rbrack\bigr\rbrack\bigr\rbrack

A list of tags sows under each of them

live ↗
InReap(Last([Sow(1,[a,b]),0]),b)\mathrm{Reap}(\mathrm{Last}(\bigl\lbrack\mathrm{Sow}(1, \bigl\lbrack a, b\bigr\rbrack), 0\bigr\rbrack), b)Out[0,[[1]]]\bigl\lbrack0, \bigl\lbrack\bigl\lbrack1\bigr\rbrack\bigr\rbrack\bigr\rbrack
Properties #

With nothing sown, the result is the value and an empty list of sown items

live ↗
InReap(x)\mathrm{Reap}(x)Out[x,[]]\bigl\lbrack x, \bigl\lbrack\bigr\rbrack\bigr\rbrack
About
Examples7
Checked againstWolfram Language 7 agree
CrosswalkWolfram Language

See also: Sow