Re^2: map vs for\foreach.
by karlgoethebier (Abbot) on Mar 11, 2015 at 21:36 UTC
|
"I strictly reserve map for coercion of a set of FOO items into a set of BAR items (@lista -> map -> @listb)..."
And i don't understand why you insist on this.
Just because there must be an example:
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Dump;
my @foo = ( 1 .. 10 );
dd \@foo;
my @bar = map { ++$_ } @foo;
dd \@foo;
dd \@bar;
So why is map { ++$_ } @foo; wrong?
I vaguely remember that there where some performance issues...patched?
Best regards, Karl
«The Crux of the Biscuit is the Apostrophe»
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
It isn't wrong, it is just not saying what you mean. map is a transformation. for is a loop. If I use map, it is understood that there are to be no modifications to the original data set (and conversely, if there are side effects, I did it wrong). If I use for, then there is the potential for modification of the original data.
It is, for me, about clarity of intent. That is why I insist (in my code) on this usage.
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
DB<111> @b = map {my $x=$_; ++$x} @a
=> (2 .. 11)
DB<112> @a
=> (1 .. 10)
Then again its pretty easy to replace ++$_ with $_+1 and s/x/y/ with s/x/y/r in current Perl versions.
Not sure if there are any modifying operators left without a non-modifying counterpart.
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Dump;
my @original_data_set = ( 1 .. 10 );
dd \@original_data_set;
my @modified_data_set = map { ++$_ } @original_data_set;
dd \@original_data_set, \@modified_data_set;
__END__
karls-mac-mini:monks karl$ ./1119774.pl
[1 .. 10]
([2 .. 11], [2 .. 11])
Best regards, Karl
«The Crux of the Biscuit is the Apostrophe»
| [reply] [Watch: Dir/Any] [d/l] |
|
|
|
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
$ perl -MO=Deparse -e'$_++ for @a;'
++$_ foreach (@a);
-e syntax OK
$ perl -MO=Deparse -e'map { $_++ } @a;'
map {$_++;} @a;
-e syntax OK
| [reply] [Watch: Dir/Any] [d/l] |
|
|
Re^2: map vs for\foreach.
by builat (Monk) on Mar 11, 2015 at 14:33 UTC
|
Is it good practice to mix map with lambda functions? like:
map{ sub{...} } @list; | [reply] [Watch: Dir/Any] [d/l] |
|
That is essentially what a map is - although your syntax is wrong. Unless you do something like: map { sub {...}->(...) } @foo to force the execution of the sub, you will simply get a set of sub {...} anonymous subroutines the same size as @foo.
The contents of the BLOCK in the map call are what I think you are thinking of as the sub. OTOH, something like:
@subs = map {
my $something = $_;
sub {
blah( $something, ... );
}
} @data;
would give you a set of subroutines that each operate on a specific item in @data. In this case, you are still transforming @data into a set of subs working on a closures.
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
Ok, that just blew my mind. (Gotta love those Perl "aha!" moments!)
Thanks, MidLifeXis :-)
-Bib
| [reply] [Watch: Dir/Any] |