6
$\begingroup$

How does one manipulate the slot numbers in a pure function? Some trick with Evaluate or Hold? I'm aiming for something along the lines of:

(Slot /@ Range[19, 164, 29])& @@ {...}

EDIT

It turns out my actual problem is: why does

Evaluate[{Slot /@ Range[19, 164, 29]}] & @@ Range[164]

yield {{19, 48, 77, 106, 135, 164}} but

{Evaluate[Slot /@ Range[19, 164, 29]]} & @@ Range[164]

yields {{#19, #48, #77, #106, #135, #164}}?

$\endgroup$
1
  • $\begingroup$ This answer to a previous question seems relevant. $\endgroup$
    – m_goldberg
    Commented Apr 25, 2014 at 9:23

2 Answers 2

12
$\begingroup$

Responding to your updated question, which should probably be closed as a duplicate once someone takes the time to find the original: Evaluate only works when it is the explicit head of an argument. In other words Evaluate[ . . . ] must appear as one of the arguments of the Head who's Hold attribute you wish to override. You should read this paper, which teaches this among many other useful things:

As an example consider these lines:

Hold[1 + 1, Evaluate[2 + 2]]
Hold[1 + 1, {Evaluate[2 + 2]}]
Hold[1 + 1, Evaluate @@ {2 + 2}]
Hold[1 + 1, 4]

Hold[1 + 1, {Evaluate[2 + 2]}]

Hold[1 + 1, Evaluate @@ {2 + 2}]

Note that only the first form evaluates. On lines two and three the Heads of the second arguments are List and Apply rather than Evaluate.

A common method to get around this is to use With to inject an expression into the body of the function:

With[{body = Slot /@ Range[19, 164, 29]}, {body} &]

% @@ Range[164]
{{#19, #48, #77, #106, #135, #164}} &

{{19, 48, 77, 106, 135, 164}}

If you want to evaluate the entire body every time you can just Apply Function to a List of the components that form the Function:

(Function @@ {{Slot /@ Range[19, 164, 29]}}) @@ Range[164]
{{19, 48, 77, 106, 135, 164}}
$\endgroup$
8
  • $\begingroup$ +1 for detailed explanation, concur that part is duped somewhere... $\endgroup$
    – ciao
    Commented Apr 25, 2014 at 1:33
  • $\begingroup$ @rasher Thanks. Let me know if you find that duplicate. $\endgroup$
    – Mr.Wizard
    Commented Apr 25, 2014 at 1:37
  • $\begingroup$ @MrWizard Thanks, this is very helpful. Also scanned for the dup but didn't see it $\endgroup$
    – mfvonh
    Commented Apr 25, 2014 at 1:42
  • 1
    $\begingroup$ Thanks for the link. The Villegas paper is a "must read". $\endgroup$
    – m_goldberg
    Commented Apr 25, 2014 at 9:19
  • $\begingroup$ Perhaps this is the previous answer you are trying to remember? $\endgroup$
    – m_goldberg
    Commented Apr 25, 2014 at 9:25
3
$\begingroup$

E.g.,

Evaluate[Slot /@ Range[1, 5, 2]] & @@ {1, 2, a, 4, 5, 6, 7}

(*  {1,a,5}  *)
$\endgroup$
2
  • $\begingroup$ Thanks for the reply. It made me realize I wasn't asking the right question; I've edited the post. $\endgroup$
    – mfvonh
    Commented Apr 25, 2014 at 1:04
  • $\begingroup$ @mfvonh: Look at the FullForm of each. One produces a function with a list of slots, the other produces a function with a list with only one entry: Evaluate[Map[Slot, Range[19, 164, 29]]] giving you the list of unevaluated slots... $\endgroup$
    – ciao
    Commented Apr 25, 2014 at 1:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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