MathGroup Archive 2006

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Re: Can anybody help?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg63641] Re: [mg63603] Re: Can anybody help?
  • From: ggroup at sarj.ca
  • Date: Sun, 8 Jan 2006 03:33:07 -0500 (EST)
  • References: <dpg0pr$pkt$1@smc.vnet.net> <200601070729.CAA06903@smc.vnet.net>
  • Reply-to: ggroup at sarj.ca
  • Sender: owner-wri-mathgroup at wolfram.com

On Saturday, January 7, 2006 at 02:29 GMT -0500, Plizak wrote:

> It boggles my mind that some people call that code elegant.

> Short, yes, elegant, no.  It is far from intuitive to know what
> Position[v1, #1]&/@v3 does.

That's a matter of opinion.

>   Anyone care to enlighten me?

Break it down, you are using 3 built-in functions:

1. First you create a pure function: Position[v1,#1]&
In[1]:=?Function
Out[1]:= Function[body] or body& is a pure function. The formal \
             parameters are # (or #1), #2, etc. Function[x, body] is \
             a pure function with a single formal parameter x. <snip>

2. This function is a simple use of the built-in function:
In[1]:= ?Position
Out[1]:= Position[expr, pattern] gives a list of the positions at \
             which objects matching pattern appear in expr. <snip>

3. The pure function is mapped onto the list of elements of interest.
In[2]:=?/@
Out[2]:= Map[f, expr] or f /@ expr applies f to each element on the \
             first level in expr. <snip>

So Position[v1, #1]&/@v3 finds the locations of the elements in v1
that match an element in v3.

I would argue this is very elegant if you're conversant with
Mathematica and functional programming; you've avoided any explicit
looping. Note that if you were to try to achieve the same thing in a
purely procedural manner, you would likely have multiple levels of
nested looping.

If you prefer, rewrite this example in a couple of steps:
f=Function[x, Position[v1,x]]
Map[f,v3]


  • Prev by Date: Re: Odd answer from Mathematica
  • Next by Date: Re: trouble with NDSolve
  • Previous by thread: Re: Can anybody help?
  • Next by thread: Re: Can anybody help?