Tell me more ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have the following working construction:

Select[ln125, # == Nearest[ln125, 551.748][[1]] &]

Here, ln125 is a one dimensional list.

What I want further is to apply this construction not only to ln125, for example, but for a list of {ln125,ln126,ln127}.

Let's say,

f[list_]:=Select[list, # == Nearest[list, 551.748][[1]] &]

I want to do this:

f /@ {ln125,ln126,ln127}.

The question is: is it possible to acheive my goal only using pure functions? That is, without defining dummy function (f in my case)? The problem is that definition of f already has pure function (as a criteria for Select) and, thus, there should be two pure function of different depths (don't know how to name it correctly) at once. In other words, I want to turn the list variable into # but in a way that Mathematica can distinguish which # goes with each different pure function.

share|improve this question
1  
Yes if you use the full form Function. – image_doctor Dec 26 '12 at 1:48

2 Answers

In can be done in a terse way with nested pure functions:

lists = RandomReal[{0, 10}, {3, 10}]

{{3.35338, 2.82572, 0.152277, 1.19036, 9.88211, 6.55398, 8.11855, 0.793288, 9.04547, 6.42518}, {4.95417, 7.73982, 5.58323, 3.09912, 5.44546, 8.88474, 2.67437, 8.20605, 4.55918, 1.95303}, {2.53793, 6.67839, 8.71033, 8.4877, 0.634367, 7.99796, 4.74131, 0.679337, 8.29468, 9.91209}}

Select[#, With[{aList = #}, # == Nearest[aList, 5.][[1]] &]] & /@ lists

{{6.42518}, {4.95417}, {4.74131}

Using With to insert expressions into Function, which has the HoldAll attribute, is trick that is useful to keep in mind.

share|improve this answer

You can use the form Function[{x,y,z}, body] to define a pure function with formal parameters x,y,z ( see Documentation >> Function)

Let

 lists = {ln125, ln126, ln127} = RandomReal[{0, 10}, {3, 10}]
 (* {{1.72286, 5.24912, 8.87257, 5.77593, 6.31276, 1.77914, 2.06393,
      0.328725, 9.46436, 4.96257}, 
     {1.71171, 2.54337, 5.93807, 9.46774,  6.99601, 2.76927, 7.58995,
      1.25785, 2.09789, 9.32326}, 
     {6.9821, 8.62002, 2.0763, 8.9402, 4.36728, 3.8571, 4.4175,
      7.15698, 7.64633, 5.23772}} *)

Define, (with 4. playing the role of 551.748 in your example):

 f2 = Function[{x}, Select[x, # == Nearest[x, 4.][[1]] &]]
 f2 /@ lists
 (* {{4.96257}, {2.76927}, {3.8571}} *)   

or

  f3 = With[{temp = Nearest[#1, 4.`][[1]]}, Select[#1, # == temp &]] &
  f3 /@ lists
  (* {{4.96257}, {2.76927}, {3.8571}} *)   
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.