Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Re: Think for yourself.

by demerphq (Chancellor)
on Oct 06, 2003 at 23:30 UTC ( [id://297113]=note: print w/replies, xml ) Need Help??


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

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


Replies are listed 'Best First'.
Re: Think for yourself.
by Abigail-II (Bishop) on Oct 07, 2003 at 09:29 UTC
    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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2024-03-19 09:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found