How can this MapAt application be done more efficiently
- To: mathgroup at smc.vnet.net
- Subject: [mg4180] How can this MapAt application be done more efficiently
- From: Joel Cannon <cannon at alpha.centenary.edu>
- Date: Tue, 11 Jun 1996 00:53:41 -0400
- Sender: owner-wri-mathgroup at wolfram.com
I have written a function to Drop any zero's from a table of numbers. Here is an example: In[161]:= dropzero[ {{5, 0}, {4, 1}, {3, 2, 0}, {3, 1, 1}} ] Out[161]= {{5}, {4, 1}, {3, 2}, {3, 1, 1}} I would like to learn how to do it more efficiently. In what I am doing, the 0's will always occur as the last element of the inner lists (although it would be nice to have it work in general). 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. Thanks in advance, ------- Joel W. Cannon Dept. of Physics Centenary College of Louisiana P. O. Box 41188 Shreveport, LA 71134-1188 (318)869-5160 (318)869-5026 FAX ==== [MESSAGE SEPARATOR] ====