http://www.perlmonks.org?node_id=1039774


in reply to Transform Sequence Problem

> This works okay, but I wonder if there is a better and faster way.

Like always... it depends what you wanna do and how flexible you need to be!

If you are calling a fixed trans() very often, you could consider eval to generate it with chained map statements.

something like

 eval 'sub trans {  map {$_ + 1} map {log($_)} map {$_ * 3} @_ }'

You'll need to hold the map-codes as an array of strings.

But your functional approach is more flexible (eg. for debugging) ! =)

edit

you could avoid 3 loops by applying the transformations directly in one loop

 eval 'sub trans {  map { log($_ * 3) + 1} @_ }'

again this can be constructed as strings (s///ubstituting $_) and evaled ....

...OR functionally with

 sub trans {  map { $f1->($f2->($f3->($_))) } @_ }

update

or better (untested)

sub trans { for my $val (@_) { $val = $_->( $val) for @transforms; } return @_; }

@transform needs to be in the closure, maybe consider passing it as arr-ref as first argument.

 $a_transforms =  shift

Cheers Rolf

( addicted to the Perl Programming Language)

Replies are listed 'Best First'.
Re^2: Transform Sequence Problem
by roboticus (Chancellor) on Jun 19, 2013 at 23:14 UTC

    LanX:

    I was thinking along the same lines--I coded up a a little something this morning, but never got around to writing a response. It looks like the code I put together goes well with your comments, so without further ado:

    #!/usr/bin/perl use strict; use warnings; use autodie; use Benchmark qw[ cmpthese ]; # operate on an array my $fa_inc = sub { map {$_ + 1} @_ }; my $fa_log = sub { map {log $_} @_ }; my $fa_x3 = sub { map {$_ * 3} @_ }; # edit an array in place my $fea_inc = sub { map {$_ = $_ + 1} @_ }; my $fea_log = sub { map {$_ = log $_} @_ }; my $fea_x3 = sub { map {$_ = $_ * 3} @_ }; # operate on a scalar my $fs_inc = sub { 1 + shift }; my $fs_log = sub { log shift }; my $fs_x3 = sub { 3 * shift }; # operate on a scalar by reference my $fsr_inc = sub { $_[0] + 1 }; my $fsr_log = sub { log $_[0] }; my $fsr_x3 = sub { $_[0] * 3 }; # edit a scalar in place my $fser_inc = sub { $_[0] = $_[0] + 1 }; my $fser_log = sub { $_[0] = log $_[0] }; my $fser_x3 = sub { $_[0] = $_[0] * 3 }; my (@l1, @l2, @l3); push @l1, 100*rand for 0 .. 1000; sub t_fa { $fa_inc->($fa_log->($fa_x3->(@_))) } sub t_fea { @l3 = @_; $fea_inc->($fea_log->($fea_x3->(@l3))) } sub t_fs { map { $fs_inc->($fs_log->($fs_x3->($_))) } @_ } sub t_fsr { map { $fsr_inc->($fsr_log->($fsr_x3->($_))) } @_ } sub t_fser { @l3 = @_; map { $fser_inc->($fser_log->($fser_x3->($_))) } @l3; } # verify that they all have the same result my (@t, @u); @t = t_fa(@l1); @u = t_fea(@l1); cmp_array('fea', \@t, \@u); @u = t_fs(@l1); cmp_array('fea', \@t, \@u); @u = t_fsr(@l1); cmp_array('fea', \@t, \@u); @u = t_fser(@l1); cmp_array('fea', \@t, \@u); sub cmp_array { my ($funcname, $ra1, $ra2) = @_; my ($la1, $la2) = ($#$ra1, $#$ra2); die "$funcname: Mismatched length! $la1 != $la2\n" unless $la1 == $la2; for (0 .. $la1) { die "$funcname: Mismatch $_: $ra1->[$_] != $ra2->[$_]\n" if $ra1->[$_] != $ra2->[$_]; } } cmpthese -1, { fa => q[ @l2 = t_fa(@l1) ], fea => q[ @l2 = t_fea(@l1) ], fs => q[ @l2 = t_fs(@l1) ], fsr => q[ @l2 = t_fsr(@l1) ], fser => q[ @l2 = t_fser(@l1) ], };

    I didn't explicitly test the case you mentioned (combining the functions into a single one). I was more interested in array operations (fa, fea) vs composing scalar functions and applying them to an array.

    In order to make them return the same result without destroying the original array, the "edit in place" functions (fea, fser) perform an array copy. If the original array isn't necessary, you can make it a little faster still--perhaps enough to have the rankings shuffle a little. I was surprised that fs was faster than fser. (I expected that the shift might be a bit more expensive the array indirection.)

    $ perl chain_transform.pl Rate fea fa fser fsr fs fea 199110/s -- -8% -60% -68% -69% fa 216392/s 9% -- -57% -66% -67% fser 499367/s 151% 131% -- -21% -23% fsr 628278/s 216% 190% 26% -- -4% fs 651987/s 227% 201% 31% 4% --

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      Thanx for testing it out.

      Though I'm not sure what you tried to achieve in 'fea_*()'

      In sub { map {$_ = $_ + 1} @_ }; you are changing the array alias in place but effectively only returning the result from map.

      So whats the intended benefit?

      Cheers Rolf

      ( addicted to the Perl Programming Language)

        LanX:

        There's no benefit at all. I was thinking along the lines of testing access speed, and then got to playing around with edit in place without actually realizing that destroying the original and *also* returning a copy of the destroyed array was a worthless enterprise.

        Oh, well, at least I had fun toying around with it.

        ...roboticus

        When your only tool is a hammer, all problems look like your thumb.