Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: Re: [mg4180] How can this MapAt application be done more efficiently
Replies: 0

 Allan Hayes Posts: 1,508 Registered: 12/6/04
Re: [mg4180] How can this MapAt application be done more efficiently
Posted: Jun 16, 1996 2:07 AM

Joel Cannon <cannon@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}}

>
>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@haystack.demon.co.uk
12-06-96