Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

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


Math Forum » Discussions » Software » comp.soft-sys.math.mathematica

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

Advanced Search

Back to Topic List Back to Topic List  
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
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

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}}


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







Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.