Reap
Usage
Notes
Further Examples
The first element in the following output is the result of evaluating u;v;w. The second element in the output is the collection of what was sown.
In[1]:=
|
Out[1]=
|
When one Reap encloses another, sown expressions will go to the first matching Reap.
In[2]:=
|
Out[2]=
|
This gives the same result.
In[3]:=
|
Out[3]=
|
Items sown for each Sow tag are collected separately. Here tag comparison is based on SameQ.
In[4]:=
|
Out[4]=
|
The tag in Reap can be a pattern. Sow[expr, tag] collects expr for each tag that matches the pattern.
In[5]:=
|
Out[5]=
|
The tag expression in either Reap or Sow can be a list. For Sow[expr, tags], expr is sown for each tag in the list tags. Here the result consists of the lists of items collected successively for u, v, w and x. When v is repeated in the tag list of Sow, an item is sown once for each instance.
In[6]:=
|
Out[6]=
|
For Reap, the matches for each tag are collected separately and the result is returned as a list.
In[7]:=
|
Out[7]=
|
Sown items are reaped for any Sow tags which match any of the Reap tags. This expression has the same lists for tags v and w as in the previous example, but the matches with the pattern catch all of those items as well.
In[8]:=
|
Out[8]=
|
The third argument of Reap is a function that takes two arguments. It allows you to determine which Sow tags are associated with which collection of items. The default is (#2)& which returns only the reaped items. A convenient value for this function is Rule, since it allows you to see the associations naturally.
In[9]:=
|
Out[9]=
|
If the Reap tags are in a list, the function is applied to the Sow tags for each Reap tag separately.
In[10]:=
|
Out[10]=
|
This groups elements based on whether or not they are integers.
In[11]:=
|
Out[11]=
|
This defines the function SelectEquivalents, a kind of generalization of the previous example. It groups together elements from a list x that have the same image under a function f. (See the Further Examples for Split for a similar function: Classify.)
In[12]:=
|
This groups elements in a list based on their value modulo 10.
In[13]:=
|
Out[13]=
|
This separates a list into symbols and nonsymbols.
In[14]:=
|
Out[14]=
|
This defines the function UnsortedUnion that drops duplicates from a list but, unlike Union, does not sort the remaining elements. (See the Further Examples for Union for another implementation.)
In[15]:=
|
In[16]:=
|
Out[16]=
|
|