Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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";

In reply to Re^2: Transform Sequence Problem by hdb
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (8)
As of 2024-04-18 15:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found