Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Do you have any pointers to practical code generation (other than compiler optimizers)?

"Practical" is a value judgement, an area where my judgement seems to vary widely with the concensus, so you'll have to make up your own mind.

In terms of the OP's question, I consider this practical. It runs a gnat's appendage slower than a hardcoded unrolled loop, but is 'generic':

sub aveAoA { my( $ref, $n ) = @_; my @sums; my $code = qq[ for my \$i ( 0 .. \$#\$ref ) { ]; $code .= qq[ \$sums[ $_ ] += \$ref->[ \$i ][ $_ ];] for 0 .. $n -1 +; $code .= '}'; eval $code; $sums[ $_ ] /= @{ $ref } for 0 .. $n -1; return @sums; } our $M ||= 1e6; our $N ||= 4; my @data; $#data = $M -1; $data[ $_ ] = [ map int(-10+rand 20), 1.. $N ] for 0 .. $#data; my @sums = aveAoA( \@data, $N );

The eval'd code is determanistic, and in my opinion totally 'safe'. Effectively, it is no different from selectively loading an appropriate subroutine (via import) at runtime except that it avoids having to have pre-written subroutines for every possible contingency.

For existing, well-known and popular modules that already use runtime code generation, see CGI, Catalyst, Template::Toolkit, Moose (though you have to drill down a few levels), Test::More etc.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

In reply to Re^6: Averaging Elements in Array of Array by BrowserUk
in thread Averaging Elements in Array of Array by neversaint

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 sharing their wisdom with the Monastery: (4)
As of 2024-04-24 05:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found