Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

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.


In reply to Re^2: Transform Sequence Problem by roboticus
in thread Transform Sequence Problem by wbirkett

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others musing on the Monastery: (6)
    As of 2014-07-29 09:04 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite superfluous repetitious redundant duplicative phrase is:









      Results (212 votes), past polls