Mathematica 9 is now available
Student Support Forum
-----
Student Support Forum: 'why Reap output is such inconvenient?' topicStudent Support Forum > General > Archives > "why Reap output is such inconvenient?"

Next Comment >Help | Reply To Topic
Author Comment/Response
Dmitry
05/11/12 08:19am

Hi,

I often use Reap and Sow, and do not understand why does Reap's output have different structure depending on the collected result.

If nothing was collected, then the result of reaping is {}. If something was collected, then the result is additionally "wrapped" into a redundant List: {{collected items}}. This relates to both using Reap with and without patterns.

So when I get the collecting results, I have to check whether the list is empty, and if not, take its first element as the list of collected items.

I wrote a function which does this check and returns the results in a proper form (the code is below). But I do not understand, WHY?

SetAttributes[myReap, HoldAllComplete];
myReap[e__] := Module[{r, l},
r = Reap[e];
If[r[[2]] == {}, {},
If[Length[Hold[e]] == 1 || Head[Hold[e][[2]]] =!= List,
r[[2, 1]],
If[# == {}, {}, First[#]] & /@ r[[2]]
]
]
];

URL: ,

Subject (listing for 'why Reap output is such inconvenient?')
Author Date Posted
why Reap output is such inconvenient? Dmitry 05/11/12 08:19am
Re: why Reap output is such inconvenient? toen 05/12/12 00:22am
Re: Re: why Reap output is such inconvenient? Bre 05/16/12 9:09pm
Re: why Reap output is such inconvenient? toen 05/17/12 4:24pm
Re: why Reap output is such inconvenient? Michael 05/23/12 11:39am
Next Comment >Help | Reply To Topic