in reply to map in void context

is the use of map in a void context deprecated ?

I don't like map in void context, personally. There's for if you just want a loop. I think of map as returning a list, and if you're discarding the list, I wonder if there's something wrong. I think, "why didn't the programmer just use for?"

Replies are listed 'Best First'.
Re^2: map in void context
by JavaFan (Canon) on Dec 17, 2008 at 13:28 UTC
    I think of map as returning a list, and if you're discarding the list, I wonder if there's something wrong. I think, "why didn't the programmer just use for?"
    But that shows more about you than about the programmer. Nothing in void context (or even non-void scalar context) returns a list.

    And the efficiency bug that made map construct a list internally was solved many years ago. As for more than one way to make a list, the programmer doesn't just have the option between map and for, there's also while, until, goto and bare blocks. And C-style for. I never think seeing one loop "Hmm, the programmer didn't use any of the other 6 types of loops, I wonder what's wrong". That only hinders my ability to understand the code.

      But that shows more about you than...

      That's part of why I used the word "personally".

      In the code at $work right now, I find this (variable names changed to protect the guilty):

      map { push @result, $_ if $_ } @{ $obj->method };

      Why was this not written as...

      push @result, grep { $_ } @{ $obj->method };

      In fact, why would I ever have push as the only thing inside a map (a pattern I see repeated throughout code written by this programmer)? Maybe there used to be more code there that made that make sense?

      Code should have some semantic meaning beyond the nuts and bolts of what it does. To me, map means "transform one list into another list". It does that using a loop, and so it can be used more generally for looping (like the other constructs you mention), but it has a specialty. I'd say the same thing about grep, which, in void context, is identical to map.

        To me, map means "transform one list into another list". It does that using a loop, and so it can be used more generally for looping (like the other constructs you mention), but it has a specialty.
        But that's quite a private meaning you apply to map. Map is a well-known operation in programming, and it means applying a function to the elements of a list. It's the applying of the function that's the crucial part - otherwise, it would have been named differently.
        I'd say the same thing about grep, which, in void context, is identical to map.
        Almost. What differs between map and grep is the context they provide. map provides list context to EXPR/BLOCK, while grep provides scalar context.