MathGroup Archive 2008

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

Search the Archive

Re: Functional programming?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg92058] Re: Functional programming?
  • From: bertmayo <bertmayo at earthlink.net>
  • Date: Thu, 18 Sep 2008 07:25:44 -0400 (EDT)
  • References: <200809130957.FAA03536@smc.vnet.net> <gal3dg$dql$1@smc.vnet.net>

David Bailey says:
>I think you have chosen a rather favourable example here. Suppose
>instead that you need to remove every zero together with the preceding
>element (sweeping from left to right).

A quick and dirty fp answer is (safer code would check that leading
elements were not already zero)

In[1]:=  v =  {1, 2, 0, 3, 4, 0};
         v1 = MapAt[0 &, v, Position[v, 0] - 1];
         v2 = Select[v1, # != 0 &]

Out[1]=  {1, 3}

It looks like even index manipulation can be written more concisely
with functional methodology.  It is also easier to read, to me at
least. The v1 line reads "Find the positions of all zeros, subtract 1,
and replace those elements by zero."

  --Bert Mayo


  • Prev by Date: Re: Re: Apparent error integrating product of DiracDelta's
  • Next by Date: Re: Variable names in Mathematica
  • Previous by thread: Re: Functional programming?
  • Next by thread: Re: Functional programming?