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.

For the following example:

{#, #} & /@ {1, 2}

I am trying to find a method to hold one of the slots (or both ) so that the function do not pass the values to the slot I want to protect. for example I want to get something like this:

{{1, #}, {2, #}}

I tried all type of Hold on the second slot but without and success.

any suggestion?

share|improve this question

5 Answers 5

up vote 10 down vote accepted

Maybe something like (using Inactive):

Activate[{#, Inactive[Slot][1]}] & /@ {1, 2}
(* {{1, #1}, {2, #1}} *)
share|improve this answer
    
IMO Activate should be internal to the function. I assumed this as a requirement; if not other options such as simple replacement open up. I am editing your answer accordingly; if you disapprove simply revert the edit. –  Mr.Wizard Aug 1 at 23:11

It depends on what you want to do with result, but you could try like this:

Function[{x}, {x, #}] /@ {1, 2}

{{1, #1}, {2, #1}}

share|improve this answer

Using Function with a named parameter, as halmir showed, is the standard way to do this, however anything that prevents a literal Slot[1] from appearing in the body of the Function will work. Inactive as chuy showed is one possibility, but I find this cleaner:

{#, Slot @@ {1}} & /@ {1, 2}
{{1, #1}, {2, #1}}

If the body will not be evaluated you can use With instead:

With[{$ = Slot}, Hold[#, $@1] ] & /@ {1, 2}
{Hold[1, #1], Hold[2, #1]}
share|improve this answer

You can also use Block for this:

Block[{slot = #}, {#, slot} & /@ {1, 2}]

{{1, #1}, {2, #1}}

This works because Block implements dynamic scoping, so the symbol slot is not replaced by #1 until slot is evaluated (i.e. after the Function has been created and mapped over the list). As pointed out by Mr. Wizard, this means that if slot is never evaluated the replacement will not occur. For example:

Block[{slot = #}, Hold[#, slot] & /@ {1, 2}]

{Hold[1, slot], Hold[2, slot]}

share|improve this answer
    
Not generally you cannot: Block[{slot = #}, Hold[#, slot] & /@ {1, 2}]. This is why I showed With. And for the case where the body does evaluate I still prefer Slot @@ {1}. –  Mr.Wizard Aug 1 at 23:08
    
The good thing about this method is if you want to use the result one following operation you don't need to Evaluate first. –  Algohi Aug 2 at 1:04
    
@Mr.Wizard if one goes to extremes, the With solution will also rename lexical stuff inside. With[{$ = Slot}, Hold[#, Module[{y}, $@1]]] & /@ {1, 2} –  Rojo Aug 2 at 5:50
    
@Mr.Wizard, good point, thanks. –  Simon Woods Aug 2 at 9:01

I stand by @MrWizard's answer. The natural way is with named functions. If it is evaluated, an alternative is anything that prevents Slot[1] from appearing literally. Personally I like Identity[Slot][1] more than Slot@@{1}. If you insist on "protecting" a slot, the only way I know is with another function, so this would work.

{#, First[# &]} & /@ {1, 2}
share|improve this answer

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.