how to distinguish slot in multiple levels of pure functions in mathematica
For example, I may have
{1, 2, 3, 4, 5} // Select[#1, ((*** + 1 &) > 2) &] &
Here, ***
also wants to 开发者_开发问答be #1, but not recognized as the outmost layer's #1. Is there any way to distinguish the two?
Thanks.
I'm not sure I understand the question. What is your expected output?.. {2,3,4,5}?... If so, there's no logical confusion between slots: every element in the list that's the first argument of Select
will be fed into a function (the second argument). The following works just fine:
{1, 2, 3, 4, 5} // Select[#, ((# + 1) > 2) &] &
In case there ever arises a conflict, instead of slot/ampersand notation, you can use Function[{x,y,...},...]
notation, e.g.
{1, 2, 3, 4, 5} // Select[#, Function[{x}, (x + 1) > 2]] &
You have one too many ampersands in your code, try
{1, 2, 3, 4, 5} // Select[#1, ((# + 1) > 2) &] &
This way #1
picks up an element from the list and passes it to the comparison test function. So in effect #1
and #
do get the same element.
It makes no real sense to have ((# + 1&) > 2)&
as a comparison function since the outer function cannot pass it's arguments on. You have effectively written (F > 2)&
, and even though F is a pure function there is no slot for it's arguments. For your way to work you'd have to write ((# + 1&[#]) > 2)&
, which equates to (F[#] > 2)&
.
精彩评论