MathGroup Archive 2011

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

Search the Archive

Re: How to roll up multiple Line calls into a loop

  • To: mathgroup at smc.vnet.net
  • Subject: [mg118341] Re: How to roll up multiple Line calls into a loop
  • From: Dan <dflatin at rcn.com>
  • Date: Sat, 23 Apr 2011 07:50:00 -0400 (EDT)
  • References: <iorih5$o6t$1@smc.vnet.net>

On Apr 22, 5:41 am, Sol Lederman <sol.leder... at gmail.com> wrote:
> Hi,
>
> I'm new to Mathematica.
>
> I have the following bit of graphics:
>
>    Graphics[{
>   Line[{{0, 10}, {1, 0}}],
>   Line[{{0, 9}, {2, 0}}],
>   Line[{{0, 8}, {3, 0}}],
>   Line[{{0, 7}, {4, 0}}],
>   Line[{{0, 6}, {5, 0}}],
>   Line[{{0, 5}, {6, 0}}],
>   Line[{{0, 4}, {7, 0}}],
>   Line[{{0, 3}, {8, 0}}],
>   Line[{{0, 2}, {9, 0}}],
>   Line[{{0, 1}, {10, 0}}],
>   }]
>
> I want to simplify it to do some sort of iteration to compute the end points
> of the lines. I can do this easily in a variety of procedural languages but
> I haven't yet grokked how Mathematica would do this.
>
> Would someone please show me the short way, I imagine using Table somehow?
>
> Thanks.
>
> Sol

Graphics[(Line[{{0,11-#},{#,0}}]&)/@Range[10]]

But let's take this appart a little. This short Mathematica expression
has a number of parts and it is worth looking at each of them.

Beginning at the right hand side, look at Range[10]

In[10]:= Range[10]
Out[10]= {1,2,3,4,5,6,7,8,9,10}

See how it can generate a sequence of numbers. This function is very
general, having the flexibility for creating almost any sort of
uniformly spaced set of values.

The next key element is the operator /@. This is shorthand for Map
which takes a function and applies it to each element of a list.

The function applied is a pure function, also called an anonymous
function in some languages:

(Line[{{0,11-#},{#,0}}]&)

The key syntactical elements are the Slot symbol, #, and the function
terminator, &. The outer parentheses are not needed but it is a good
practice to delimit your inline functions so it is obvious when
reading your code what the function is.

Dan


  • Prev by Date: Re: How to roll up multiple Line calls into a loop
  • Next by Date: Re: problems plotting curvature
  • Previous by thread: Re: How to roll up multiple Line calls into a loop
  • Next by thread: Re: How to roll up multiple Line calls into a loop