Further Examples: Reap
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.
When one Reap encloses another, sown expressions will go to the first matching Reap.
This gives the same result.
Items sown for each Sow tag are collected separately. Here tag comparison is based on SameQ.
The tag in Reap can be a pattern. Sow[expr, tag] collects expr for each tag that matches the pattern.
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.
For Reap, the matches for each tag are collected separately and the result is returned as a list.
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.
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.
If the Reap tags are in a list, the function is applied to the Sow tags for each Reap tag separately.
This groups elements based on whether or not they are integers.
This defines the function SelectEquivalents, a kind of generalization. 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.)
This groups elements in a list based on their value modulo 10.
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.)