In the documentation of Reap
under "Applications" paragraph there is this example:
Reap[Sow[1, {b, b, c, a, c, a, b, d}], _, # &]
(* {1, {b, c, a, d}} *)
The second part of the output represents unsorted union of {b, b, c, a, c, a, b, d}
.
I do not understand how this works. Neither Reap
nor Sow
represent any cycle or condition if I am not mistaken.
Sow[1, {b, b, c, a, c, a, b, d}]
works like mapping over the second argument. Anyway, I have never usedReap - Sow
in any code and I don't think I ever will. $\endgroup$Reap
/Sow
can be useful to monitor steps or evaluations when numerically solving a problem with functions likeNDSolve
,FindRoot
etc. For example EvaluationMonitor $\endgroup$Reap[Sow[1,{b, b, c, a, c, a, b, d}],_,Pick[#1,Unitize[Length@#2-1],1]&][[2]]
or to delete ALL duplicates (using Reap/Sow):Reap[Sow[1,{b, b, c, a, c, a, b, d}],_,Pick[#1,#2,{1}]&][[2]]
$\endgroup$