# What is shorthand way of Reap list that may be empty because of zero Sow

Sometimes because of conditionals, you cannot guarantee that Sow will be called at least once.

Reap[Sow[1]][[2,1]] results in {1}, which is perfect if one was going to discard the end value anyway, because only the list of sown values has value.

Reap[Null][[2, 1]] raises exception

"Part::partw: Part 1 of {} does not exist."


I use this code

ClearAll[reapList]
reapList::usage="gracefully collect all Sow expressions into list";
SetAttributes[reapList,HoldAll]
reapList[r__]:=Flatten[Reap[r,{_}][[2]],2]
reapList[Sow[15]]

   {15}

reapList[Sow[{}]]

   {{}}

reapList[Sow[{16,17}];Sow[18];Print["hello"];Sow[{19,20}];21]
hello

   {{16,17},18,{19,20}}

reapList[Null]

   {}

reapList[Sow[Null]; Sow[{Null}]]

   {Null,{Null}}

reapList[For[i = 95, i <= 100, i++, If[PrimeQ[i*2 - 1], Sow[i]]]]

   {96, 97, 99, 100}

reapList[For[i = 101, i <= 105, i++, If[PrimeQ[i*2 - 1], Sow[i]]]]

   {}


but there might be a better way than use of pattern {_} and Flatten[ ... ,2] that is so much shorter not much value in creating helper function.

Also, not clear to me how to generalize the "reapList" helper function for use with tags.

• Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Read the faq! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! Dec 9 '14 at 0:28
• wait, now I am not sure what Sow[Null] and Sow[{Null}] should do Dec 9 '14 at 1:16
• for clarity, I should add a test with some code where the condition if Sow is called zero or at least once is best discovered at run time Dec 9 '14 at 1:24
• @Kuba yes, you are right - Flatten[Reap[r][[2]], 1] works Dec 9 '14 at 17:28
• This isn't a question about Reap or Sow, but about how to deal with functions that may not return output (or, may return output that the next function doesn't expect). The answer is surely "design your functions' input and output carefully."
– djp
Mar 5 '15 at 9:39

Edit: I accidentally used Identity when I meant Sequence, but on reflection I believe Join @@ is better.

My two standard methods are Apply: (reference Formatting text through pattern matching and Quick multiple selections from a list)

Join @@ Reap[Sow[1]][[2]]
Join @@ Reap[Null][[2]]
Join @@ Reap[Sow /@ {1, 2, {}}][[2]]

{1}

{}

{1, 2, {}}

Reap[Sow[1]][[2]]             /. {x_List} :> x
Reap[Null][[2]]               /. {x_List} :> x
Reap[Sow /@ {1, 2, {}}][[2]]  /. {x_List} :> x

{1}

{}

{1, 2, {}}

• Identity@@@ fails with more than one item sown Identity @@@ Reap[Sow[1]; Sow[2]][[2]] Dec 9 '14 at 19:53
• @Manuel Thank you! I meant to use Sequence, not Identity. Dec 9 '14 at 23:47
• @Manuel Come to think of it Join would be better I believe. Dec 9 '14 at 23:53
• @Manuel Thanks for the Accept. Dec 9 '14 at 23:59

In Mathematica v10.1 there's undocumented GeneralUtilitiesReapList function. It accepts two arguments: expression and a tag.

Using it on adapted test suite from OP:

Needs["GeneralUtilities"]
ClearAll[tag]
ReapList[Sow[15, tag], tag]
(* {15} *)
ReapList[Sow[{}, tag], tag]
(* {{}} *)
ReapList[
Sow[{16, 17}, tag]; Sow[18, tag]; Print["hello"]; Sow[{19, 20}, tag]; 21,
tag
]
(* hello *)
(* {{16, 17}, 18, {19, 20}} *)
ReapList[Null, tag]
(* {} *)
ReapList[Sow[Null, tag]; Sow[{Null}, tag], tag]
(* {Null, {Null}} *)
ReapList[For[i = 95, i <= 100, i++, If[PrimeQ[i*2 - 1], Sow[i, tag]]], tag]
(* {96, 97, 99, 100} *)
ReapList[For[i = 101, i <= 105, i++, If[PrimeQ[i*2 - 1], Sow[i, tag]]], tag]
(* {} *)


@Kuba is correct

Flatten[Reap[r][[2]], 1]


is a good substitute for Reap[r][[2,1]] when cannot guarantee at least one Sow

the line above should be substituted with

reapList[r_] := Flatten[Reap[r][[2]], 1]


because the __ is unnecessary as well

Perhaps cleaner to construct your expression in a way that guarantees at least one Sow[] call.

  Table[Rest@First@Last@Reap[Sow[Null]; If[i != 2, Sow[i]]], {i, 3}]


{{1}, {}, {3}}