in reply to Scanning a list for indices

Why use map without using it's return value? why not two for loops? merlyn doesn't like that, and I agree with him.
# for loop for my $i (0 .. $#a1) { for my $j (0 .. $#a2) { push @indices, $i if $a2[$j] eq $a1[$i]; } }
# schwartzian [sp?] transform (perhaps not a paradigmatic example thou +gh) my @indicies = map {$_->[0]} grep {$a1[$_->[0]] eq $a2[$_->[1]]} map { my $l = $_; (map {[$l,$_]} 0 .. $#a2) } 0 .. $#a2;

-Paul

Replies are listed 'Best First'.
Re^2: Scanning a list for indices
by cmv (Chaplain) on Feb 13, 2008 at 15:11 UTC
    Paul-

    Why use map without using it's return value? why not two for loops?

    Well, I'm just now maturing enough with perl to begin to understand how to think about things with map (look ma, I'm using map!), so naturally I want to show off a bit. However, I also thought map might be more efficient here than 2 for loops (is that correct?).

    I couldn't figure out how to use the return value from map to get done what I want. Any pointers here would be great!

    Thanks!

    -Craig Update: Oh, you just did... Thanks!

      Nope, worse. The map still loops just like a for loop would have to, but it also has to manipulate an argument stack (or something like it) so that it can set the $_ and then return it. I'm really not very sure about what I just said, but it is my understanding that is the case.

      The only way to know for sure it so try it.

      use strict; use Benchmark qw(cmpthese); my @a1 = (1 .. 4_000); cmpthese(4_000, { for_loops => sub { for(@a1) { $_ = $_ + 1 } }, map_loops => sub { map { $_ = $_ + 1 } @a1 }, });

      -Paul

        Actually since 5.8.1 map in void context is optimized to not construct a return list. (Update: although I'm still get map 7-14% slower no matter how I twiddle your benchmark; scratch that, see below)

        Having said that, it's still poor form to use map as a replacement for a proper for loop though (IMHO). Using for says you're iterating over a list of values, whereas using map emphasizes the fact that the point of the iteration is constructing a new list from the existing list (or put another way: for means "I'm walking this list for some reason", whereas map screams "HEY, I WANT TO HAVE A NEW LIST HERE").

        Update: Aha, I think I know what the problem is. Try appending a 1; in your sub after the map so that you're explicitly putting the map in void context (since it'll no longer be the last expression in the sub and hence the return value). That makes map a hair (7%) faster for me (5.8.8 on OS X).

        Oop: I also changed to use map EXPR, LIST rather than map BLOCK LIST so that's saving me overhead vs for as well.

        use strict; use Benchmark qw(cmpthese); my @a1 = (1)x4_000; cmpthese(4_000, { for_loops => sub { for(@a1) { $_ = $_ + 1 } }, map_loops => sub { map $_ = $_ + 1, @a1; 1 }, }); __END__             Rate for_loops map_loops for_loops 1724/s        --       -6% map_loops 1826/s        6%        --

        So you can make it a tiny bit faster (due to being able to "cheat" a little and avoid the enter/leave block overhead), but again I'd stick to using map just for map'ing not general iteration.

        The cake is a lie.
        The cake is a lie.
        The cake is a lie.