I'm trying to create a function that returns only the second argument of Reap. Here's my naive guess of how it would go : Reap2[x_]:= Flatten[Reap[x][[2]],1]
However, even when X contains Sow
, my function always returns empty.
I've made a simplified version here, which shows what I mean.
Reap[Sow[1]]
{1, {{1}}}
Clear[Reap2];
Reap2[x_] := Reap[x]
Reap2[Sow[1]]
{1, {}}
Can anyone explain this behavior? How would I go about building this function?
HoldFirst
. Try:SetAttributes[Reap2, HoldFirst]
. $\endgroup$Reap
is not a simple function. It is closer to scoping constructs in spirit. It has aHold
attribute, so that the code evaluates only inside of it, but not before being passed. You need to setHoldAll
orHoldFirst
attribute to yourReap2
, if you want it to behave properly. $\endgroup$Join
andFlatten
. $\endgroup$