Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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}}?

share|improve this question
    
This answer to a previous question seems relevant. –  m_goldberg Apr 25 at 9:23
add comment

2 Answers 2

up vote 9 down vote accepted

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}}
share|improve this answer
    
+1 for detailed explanation, concur that part is duped somewhere... –  rasher Apr 25 at 1:33
    
@rasher Thanks. Let me know if you find that duplicate. –  Mr.Wizard Apr 25 at 1:37
    
@MrWizard Thanks, this is very helpful. Also scanned for the dup but didn't see it –  mfvonh Apr 25 at 1:42
1  
Thanks for the link. The Villegas paper is a "must read". –  m_goldberg Apr 25 at 9:19
    
Perhaps this is the previous answer you are trying to remember? –  m_goldberg Apr 25 at 9:25
show 2 more comments

E.g.,

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

(*  {1,a,5}  *)
share|improve this answer
    
Thanks for the reply. It made me realize I wasn't asking the right question; I've edited the post. –  mfvonh Apr 25 at 1:04
    
@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... –  rasher Apr 25 at 1:15
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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