Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Transform Sequence Problem

by hdb (Parson)
on Jun 19, 2013 at 12:09 UTC ( #1039768=note: print w/ replies, xml ) Need Help??


in reply to Transform Sequence Problem

Using an array of code references and closures:

use strict; use warnings; sub fgenerator { # some code references my @f = ( sub {map {$_ + 1} @_}, sub {map {log($_)} @_}, sub {map {$_ * 3} @_}, ); my @idx = @_; return sub { @_ = $f[$_]->(@_) for @idx; return @_; }; } # create transform based on indices of function # read: first apply f0, then f1, then f2 my $trans1 = fgenerator( 0, 1, 2 ); my $trans2 = fgenerator( 0, 0, 0 ); # some data my @data = (1, 2, 3); # call the transforms my @trans = $trans1->( @data ); print "@trans\n"; @trans = $trans2->( @data ); print "@trans\n";


Comment on Re: Transform Sequence Problem
Download Code
Re^2: Transform Sequence Problem
by hdb (Parson) on Jun 19, 2013 at 14:45 UTC

    A few minor changes I like:

    • Using a hash instead an array of code refs allows to give names to the transformations.
    • A bit of error checking can be added, unknown transformations can be ignored and warned about.
    • Instead of passing the indices into the closure, it is more efficient to pass the code references into it. This will generate no additional overhead from the niceties added to the generator function.
    It could look like this:

    use strict; use warnings; sub fgenerator { my %f = ( add_one => sub {map {$_ + 1} @_}, log => sub {map {log($_)} @_}, times_3 => sub {map {$_ * 3} @_}, ); my @t = @f{ grep { exists $f{$_} } @_ }; # ignore unknown name +s warn "Don't know sub(s) ".join ", ",(grep { !exists $f{$_} } @ +_) if @t < @_; return sub { @_ = $_->(@_) for @t; return @_; }; } my $trans1 = fgenerator qw( add_one log times_3 ); my $trans2 = fgenerator qw( add_one add_one add_one times_3 fun ); my @data = (1, 2, 3); my @trans = $trans1->( @data ); print "@trans\n"; @trans = $trans2->( @data ); print "@trans\n";

      Something was missing, now I know:

      think_of_a_number => sub {map {rand $_} @_},

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1039768]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (8)
As of 2014-07-12 13:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (240 votes), past polls