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.

Let say I need and option for Plot:

RegionFunction -> (-6 < #2 < 6 &)

But I also want those -6 and 6 to be take as arguments from overlapped function. The following is not going to work, I only want to show an example

RegionFunction -> ((#1_2 < #2 < #2_2 &)&_2 @@ {-6, 6})

This construction #2_2 means second argument reffered to the function closed by &_2.

I know I can do this in about 432 different ways, this is an abstract question about particular construction/input syntax.

Edit

After reading answers of Mr. Wizard and jVincent I have to clarify the question.

My goal was to find out if there is a way to reffer # to desired & if there is some type of mix like in the example ((#1 < #_2 < #2)&)&_2.

jVincent note about \[Function] seems to be the closest to the general idea.

share|improve this question
2  
Strongly related question –  Leonid Shifrin Jul 22 '13 at 13:08

2 Answers 2

up vote 11 down vote accepted

The problem is just name collisions, that isn't at all abstract and will happen in any programing language, so it would be odd to claim that it's impossible due to the way Mathematica works. The solution is simply to name your parameters when you write your functions so they don't collide, so you write for instance:

RegionFunction -> Function[{a1, b1}, Function[{a}, a1 < a < b1]] @@ {-6, 6}

This can also be typed using the EscfnEsc short form for Function which will make it look like:

{a, b} -> x -> a < x < b

Update:

Since you asked to do this with only Slot and Function. Well naturally you can also do all sorts of tricks with Slot and Function in order to get similar behavior, by changing the resolution order such overlapping names never coincide:

Function[f[Slot[1] < s[1] < Slot[2]] /. {f -> Function, 
s -> Slot}] @@ {-x, x}

But really I would argue that you aren't gaining anything from this sort of trickery, except adding confusion when you should just be naming the parameters.

share|improve this answer
    
I was thinking is this is possible to do with Slot and &, your example's syntax is a little bit different because functions are not overlapping but one enclose another. –  Kuba Jul 22 '13 at 9:19
    
@Kuba I don't undestand what you mean by "overlapping" if this doesn't adress your example. The litteral translation of your given example (with fuzzy syntax) is Function[{a, b}, Function[{a}, a_2 < a_1 < b_2]] @@ {-6, 6}. So if you consider yours to be an example of overlapping functions my example should be to (just without the name collision). –  jVincent Jul 22 '13 at 9:24
    
I'm sorry, you are right. So the question is only if it is possible to do this with Slots and &. And, ofcourse +1 :) –  Kuba Jul 22 '13 at 9:25
    
\[Function] Is what I was looking for in the dark. Thank you. :) –  Kuba Jul 22 '13 at 9:52
1  
@Kuba Just to be tedious; \[Function] is just an input syntax thing, and the underlying solutions are the same as stated in my and Mr. Wizards answers. You are resolving the name collision in the same manor, just in a syntactically nicer way. –  jVincent Jul 22 '13 at 9:57

If I understand the question here are three ways to "nest" functions:

f1 = Function[x, (# + x)/2 &];
f2 = With[{x = #}, (# + x)/2 &] &;
f3 = # /. x_ :> ((# + x)/2 &) &;

All work the same:

#@7 & /@ {f1, f2, f3}
{(#1 + 7)/2 &, (#1 + 7)/2 &, (#1 + 7)/2 &}

Note that with the first form I used the Slot based function on the inside. If this is inverted the behavior will change if the function is given x itself as an argument:

f4 = Function[x, (# + x)/2] &;

#[x][y] & /@ {f1, f2, f3, f4}
{(x + y)/2, (x + y)/2, (x + y)/2, y}

Regarding f3 see also: Mathematica Destructuring and "injector pattern."

share|improve this answer
    
My goal was to find out if there is a way to reffer # to desired & if there is some type of mix like in the example ((#1 #_2 #2)&)&_2. It seems there is not but this post is very useful for topic exhaustion. Thanks. –  Kuba Jul 22 '13 at 10:30

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.