Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Think for yourself.

by Abigail-II (Bishop)
on Oct 06, 2003 at 19:50 UTC ( [id://297055]=note: print w/replies, xml ) Need Help??


in reply to Re: Re: Think for yourself.
in thread is the use of map in a void context deprecated ?

I emphasised one point of your comment however. When i see a map in void context that isnt part of an obfu or some CB whip-it-together, my first thought is "Hmm, what happend to the target of the map?" And then I go looking to see what I dont understand. And then I get annoyed and change it to a for loop so that nobody else wastes time trying to figure out the misleading code.

And that is something I don't understand. What makes map so special? Any function in Perl will return something, be it a buildin or a user defined sub, and even operators return values. Why is it that people get all confused if they see a map in void context, and start looking where the return value goes, but they don't have problems with other functions?

What makes map so special?

Abigail (really, I'd like to know)

Replies are listed 'Best First'.
Re: Re: Think for yourself.
by demerphq (Chancellor) on Oct 06, 2003 at 23:30 UTC

    What makes map so special?

    Because its name and general usage strongly suggests that it should return something. Take a sub like get_value if you saw that in void context wouldn't you wonder what is going on? Wouldn't it annoy you to discover that in fact the routine has nothing to do with getting a value? Well likewise for me and map. If you want to do some action for each member of a list, then use foreach. If you want to transform each member of a list into something else and get the resulting list back, then use map. The cues in the name help you understand the intent of the author more quickly and with less effort. If someone plays silly bugger and uses map as for then they have just thrown away a useful cue to a future developer as to what was on their mind when they wrote the code.

    Think about it for a minute. We do our best to not confuse ourselves and theose that come after us. We don't use $var1 and $var2 for our variable names so that the programmer that follows us (more often than not itll be ourselves a few days or weeks later :-) has a hope in hell of figuring out what it going on. We don't name our subroutines A, B, C etc, for similar reason. We dont use goto's as our primary flow control mechanism etc. All of these things are to provide as much of a cognitive model of what is happening as possible. The same goes for map.

    I mean why make life hard? Perl has a hugely expansive vocabulary as far as programming goes. Why waste the expressiveness that it provides? Say what you mean and mean what you say. If you want to map a list then do so, if you want to iterate over the list then do so, but don't pretend to one when you are really doing the other.


    ---
    demerphq

      First they ignore you, then they laugh at you, then they fight you, then you win.
      -- Gandhi


      Because its name and general usage strongly suggests that it should return something.

      But where is this notion coming from? "map" as an operation that maps a function over all the elements of a list doesn't come from Perl (I've been familiar with the operation and the name even before Larry released Perl 1.000), and its name doesn't suggest anything about return values shall be used.

      Someone, or something, has seeded this notion that the first argument of map should be side-effect free, and it wasn't Larry - otherwise $_ wouldn't be an alias.

      Take a sub like get_value if you saw that in void context wouldn't you wonder what is going on?

      Sure, I'd figure that either the caller hadn't understand the working of get_value, or that the name of the function was misleading. But "map" isn't called "get_value". It's just "map". Short for "mapping a function over a list". And that's just what it's doing. It isn't called "construct a list by collecting return values". In fact, if I want to add one to all elements of an array, I find:

      map {$_ += 1} @array;
      to be far more natural than:
      for (@array) {$_ += 1}

      I find it more surprising that $_ is an alias for the list element in a 'for' loop than in a 'map' operation.

      Let's look at the first sentence of 'perldoc -f map':

      Evaluates the BLOCK or EXPR for each element of LIST (locally setting $_ to each element) and returns the list value composed of the results of each such evaluation.
      Before it talks about return values, it describes what map does: evaluating a piece of code for each element of the list.

      Anyway, you haven't convinced me. I refuse to deal with "map" differently than other functions. And if I can use other functions in void context, I can use "map" in void context.

      Abigail

        IMO the winner is
        ++$_ for @array;
        which reads more naturally than either of your propositions. (Although between the two of yours, I'd prefer the map as well.)
        But where is this notion coming from? "map" as an operation that maps a function over all the elements of a list doesn't come from Perl (I've been familiar with the operation and the name even before Larry released Perl 1.000), and its name doesn't suggest anything about return values shall be used.
        That's you. Most people don't have such a background. For the majority of Perl programmers it is the first list oriented language they have encountered. So maybe it would be more appropriate to say that you already had a preconception that map in void context is fine, which other people don't come with. It certainly explains why you regard map in void context as a completely normal construct and find the opposition to it exasperating.

        Makeshifts last the longest.

Re: Re: Think for yourself.
by Petruchio (Vicar) on Oct 07, 2003 at 15:07 UTC
    And that is something I don't understand. What makes map so special? Any function in Perl will return something, be it a buildin or a user defined sub, and even operators return values. Why is it that people get all confused if they see a map in void context, and start looking where the return value goes, but they don't have problems with other functions?

    What makes map so special?

    Map is special, in my opinion, because there is a nearly analogous construct which does not return values.

    Good style involves choosing appropriate tools. Given a situation where no return value will be used, I tend to expect someone to pick the tool which returns no value... unless there is a good reason to do otherwise. Forcing list context, for example.

    If I see someone make a different choice, I will wonder why, and I will investigate. If, at the end of my investigation, I conclude that the author could have used a foreach loop just as effectively, I will be annoyed. By passing up the more appropriate (and more obvious) tool, he has obliged me to go looking for something subtle which was not there. Likewise, I wouldn't expect a person to choose

    () = do_something ($_) for @some_list;

    over

    do_something ($_) for @some_list;

    unless there was actually a need to force list context; if the author consistenly chose the former as his idiom without good reason, I'd find it very irritating (though not half so irritating as an author who inconsistently chose this, or map in void context, without good reason).

    On the other hand, perhaps the author intends to alert me to the significance of context here. Saying

    () = do_something ($_) for @some_list;

    would then be a louder hint, and I'd generally prefer it; but if the author was consistent in using map for this purpose, I wouldn't call it bad style. I might appreciate a note somewhere, as this is not common practice.

    An author might choose map for this, I think, on the grounds that he was choosing the most appropriate tool. In a very fastidious author, whose code was so clean and consistent that it gave the reader a sense of confidence that each thing was as it was for a reason, this might be a bit of minimalism which I would admire. However I would be surprised to see such a person call his own functions in such a fashion, because I wouldn't expect him to write a function which altered its arguments differently depending on the context in which it was called. I cannot think of a good reason to do this.

      Good style involves choosing appropriate tools. Given a situation where no return value will be used, I tend to expect someone to pick the tool which returns no value...

      But map in void context does not return a value. No function in void context returns a value - it can't, because there's nothing to put the value in. That return values of functions are context driven is a very essential thing that makes Perl what it is. It's not map that decides whether nothing, a scalar or a list will be returned - it's the context. And map doesn't behave any different than any other function.

      By passing up the more appropriate (and more obvious) tool, he has obliged me to go looking for something subtle which was not there.
      That sounds like "for is the more obvious tool, because it's more obvious".

      Abigail

Re^?: Think for yourself.
by Aristotle (Chancellor) on Oct 06, 2003 at 20:08 UTC

    Vaguely, because map has control structure characteristics rather than reading purely like a function. Unlike any of the "real" loops it does have a return value, however.

    Not so coincidentally, I've sometimes thought it would be nice if for could build a list as well - in which case any distinction between the two would really go out the window for good. Ruby works this way.

    Makeshifts last the longest.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://297055]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (10)
As of 2024-04-23 08:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found