map is a slow
Why do you say that?
map{} takes an input array and makes a new output array
map transforms lists, not only arrays. Likewise, for iterates over lists.
As well, map doesn't have to produce any output. A void context map is poor style in my book, but there's no performance penalty in modern Perls.
| [reply] |
my @sqrt_results = map { sqrt ($_ ) } @input;
This is a perfect thing for map{}: translate one thing to another. map{} can also make 1 to many and many to one translations. In general, I use map{} when the transformation can be expressed as a "one liner+" and foreach() when the code is longer. As a matter of style, this allows me to put foreach(@input) at the top instead of at the end of the program text, compare with: @output = map{lots of lines}@input. | [reply] [d/l] |
| [reply] |
You asked about the difference between lists and arrays. Here's an example of map operating on lists, not arrays:
say for map { "$_ squared is " . $_ ** 2 } 1 .. 10;
If an operator operates on lists, Perl's list context ensures that it can handle arrays. If an operator operates on arrays (push, splice), it won't necessarily operate on lists.
| [reply] [d/l] |