MathGroup Archive 1996

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

Search the Archive

Re: How can this MapAt application be done more efficiently

  • To: mathgroup at smc.vnet.net
  • Subject: [mg4219] Re: [mg4180] How can this MapAt application be done more efficiently
  • From: Allan Hayes <hay at haystack.demon.co.uk>
  • Date: Tue, 18 Jun 1996 03:26:30 -0400
  • Sender: owner-wri-mathgroup at wolfram.com

Joel Cannon <cannon at alpha.centenary.edu>
[mg4180] How can this MapAt application be done more efficiently
wants to drop the zeros form an expression like
	lst = {{5, 0}, {4, 1}, {3, 2, 0}, {3, 1, 1}}
Joel:
   DeleteCases[#,0]&/@lst

      {{5}, {4, 1}, {3, 2}, {3, 1, 1}}


You add
>
>Here is my function, with another function that it calls:
>     dropzero[a_] :=
>     	MapAt[droplast,a, Partition[ Map[First,Position[a,0]],1] ]
>      droplast[a_] := Drop[a, -1]
>
>It feels to me like two things could be improved.
>
>1. I had to define the function droplast because I couldn't figure >out
>how to use Drop[#,-1] with in the MapAt expression and get it to >work.
>
>2. Of less importance,
>the argument "Partition[Map[First,Position[a,0]],1]" seems a bit  
>clumsy.

Pure functions are very useful.

Re 1. :

	MapAt[Drop[#,-1]&,a, Partition[ Map[First,Position[a,0]],1] ]

Re 2:
	MapAt[Drop[#,-1]&,a, Position[lst,_?(MemberQ[#,0]&)] ]
	
Allan Hayes
hay at haystack.demon.co.uk
12-06-96

==== [MESSAGE SEPARATOR] ====


  • Prev by Date: Re: How can this MapAt application be done more efficiently
  • Next by Date: Mandelbrot & Julia sets
  • Previous by thread: Re: How can this MapAt application be done more efficiently
  • Next by thread: Re: How can this MapAt application be done more efficiently