|We don't bite newbies here... much|
Re: Think for yourself.by Abigail-II (Bishop)
|on Oct 07, 2003 at 09:29 UTC||Need Help??|
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:
to be far more natural than:
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.