6

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.

Kuba
  • 136,707
  • 13
  • 279
  • 740

2 Answers2

12

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.

jVincent
  • 14,766
  • 1
  • 42
  • 74
  • 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 09: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 09: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 09:25
  • \[Function] Is what I was looking for in the dark. Thank you. :) – Kuba Jul 22 '13 at 09: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 09:57
  • Yes, I'm aware of that. It is not ideal and my goal was rather to connect # with & within other function expressed by theirs #s. It seems such construct does not exist. – Kuba Jul 22 '13 at 10:02
  • In my experience, is some cases, for instance with Map(s) constructs there is the possibility to nest pure functions and use only Slot(s) and &, but many times the scoping of variables gets confusing and there is the name collision as jVincent mentioned. Here is a possible alternative to the solution of jVincent, where I just used Slot for the innermost pure function, because there is not need to use {a} as variable. RegionFunction -> (Function[{a1, b1}, a1 < # < b1 &][#1, #2][1] &) – bobknight Jul 22 '13 at 10:08
  • @bobknight @jVincent I know this is a problem with scoping. x is not worse than #_2 while Funtion[x,Function[a, a x]] is worse than (# #_2 & )&_2. Worse in terms of compactness. – Kuba Jul 22 '13 at 10:18
  • @Kuba In the future if your goal is compact/terse code please include that in the problem description; I'm happy to post all the tricks I know if requested. jVincent: good thinking to include \[Function]. – Mr.Wizard Jul 22 '13 at 12:01
  • @Mr.Wizard It wasn't my goal, it was rather about programming style or input syntax style in general. Well, not in general, it is quite narrow, only about # style programming. – Kuba Jul 22 '13 at 12:04
  • +1. Just so everyone who didn't yet encounter this knows, it is good to be aware that pure functions with named arguments may exhibit certain scoping issues due to the imperfect nature of lexical scoping emulation used for Function. I have discussed this e.g. in this answer to a similar question asked on SO. – Leonid Shifrin Jul 22 '13 at 13:14
8

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."

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
  • 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