3
$\begingroup$

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?

$\endgroup$
  • 1
    $\begingroup$ You are only missing HoldFirst. Try: SetAttributes[Reap2, HoldFirst]. $\endgroup$ – Mr.Wizard Dec 12 '14 at 19:05
  • 1
    $\begingroup$ Reap is not a simple function. It is closer to scoping constructs in spirit. It has a Hold attribute, so that the code evaluates only inside of it, but not before being passed. You need to set HoldAll or HoldFirst attribute to your Reap2, if you want it to behave properly. $\endgroup$ – Leonid Shifrin Dec 12 '14 at 19:05
  • $\begingroup$ Lokdal, I am glad you have your solution but there is no need to include it in the question. In fact it is unnecessary clutter and draws attention away from the Accepted answer. By the way I do not believe that you will need to use both Join and Flatten. $\endgroup$ – Mr.Wizard Dec 12 '14 at 19:38
  • $\begingroup$ Mr.Wizard : edited accordingly! And you're right, for my specific need I had to use Flatten because I Sow lists, and using Join removed the structure. $\endgroup$ – Lokdal Dec 13 '14 at 2:36
8
$\begingroup$

The issue is lack of HoldFirst in attributes of your function, Sow is being called too early otherwise before Reap can capture

ClearAll[reap2]
SetAttributes[reap2,HoldFirst]
reap2[x_]:=Join @@ Reap[x][[2]]

idea to use Join@@ taken from @Mr.Wizard from here What is shorthand way of Reap list that may be empty because of zero Sow

$\endgroup$
  • 2
    $\begingroup$ This is what I like to see: new user jump in and write up answers! +1 $\endgroup$ – halirutan Dec 12 '14 at 19:08
  • $\begingroup$ @halirutan Yep, same feelings here. +1. $\endgroup$ – Leonid Shifrin Dec 12 '14 at 19:09
  • $\begingroup$ agree with @Mr.Wizard should use HoldFirst instead of HoldAll $\endgroup$ – Manuel --Moe-- G Dec 12 '14 at 19:13
  • $\begingroup$ From a profile: "An irritating idiot with a high opinion of himself. Best ignored." -- lol -- okay, we've all been duly warned. :^) $\endgroup$ – Mr.Wizard Dec 12 '14 at 19:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.