in reply to map() abuse or child with a chainsaw

Im assuming that your "arrested" comment only applies to "some people". I use map all the time. And when its used right it makes code clearer not the opposite. The above is just a joke as it really says
shift @ARGV;
My guess is that it should have said
# drop first two arguments map shift(@ARGV),(0,1);
And your colleague got confused. Which really isnt anything to do with map.

Yves / DeMerphq
---
Writing a good benchmark isnt as easy as it might look.

Replies are listed 'Best First'.
Re: Re: map() abuse or child with a chainsaw
by kappa (Chaplain) on May 13, 2002 at 14:29 UTC
    If I think of future readers of my code I'll write:
    map shift(@ARGV), qw(two times);
    ...or (best of all)...
    shift(@ARGV); shift(@ARGV);

    But this guy I wrote about, should only be allowed to use straight foreach loops from now on :=E (I'm a little emotive right now, excuse me). At least he won't be able to abuse arrayref constructor for plain old parens.

      Just for sheer TIMTOWTDI:
      @ARGV = @ARGV[2..$#ARGV]; # or for the paran paranoid: @ARGV = (@ARGV)[(2..$#ARGV)];
      ... but i don't like copying the ARGV array back to itself. More readable might be:
      my $discard1 = shift; my $discard2 = shift;
      Out of curiosity, why would one even want to discard the first two arguments like that anyway? Seems like a bad idea in the first place.

      Hmmmm ... sounds like time to pull out one of the Getopt modules to me. ;)

      jeffa

      L-LL-L--L-LL-L--L-LL-L--
      -R--R-RR-R--R-RR-R--R-RR
      B--B--B--B--B--B--B--B--
      H---H---H---H---H---H---
      (the triplet paradiddle with high-hat)
      

        Why store the results from the two shift's in variables if the intention is to discard the values?

        shift;shift; # Should do it


        Everything went worng, just as foreseen.

        I admire you, guys :) Correctly identifying this very needed refactoring by seeing shifting of @ARGV is long beyond my abilities :)) Yes, this utility has home-grown option processing.
      map shift(@ARGV), qw(two times)
      Well, no dont do that.

      :-)

      A very good hint is that you are using map in void context. Thats dumb. (no offense to obfuscators at large ;-). Map is _meant_ to return a set of values, its not meant to be used as a for loop. (In fact my personal belief is that map() and grep() are sorely sorely misnamed. They should be called apply() and filter(), but thats another flame-war iykwim)

      I would say that your second choice is preferred, or possibly

      shift @argv for (1,2);
      But map in void context? No thanks.

      Oh and of course, using Getopt would be a good idea too. (jeffa++)

      Yves / DeMerphq
      ---
      Writing a good benchmark isnt as easy as it might look.