in reply to Dynamic function chains?

Your function outputs don't quite match your expected results, but I left them for you to sort out (apart from round which was a bit verbose :).

#! perl -lw use strict; my %funcs = ( power => sub { return exp( $_[0] ) }, log2 => sub { return log( $_[0] )/log(2.0) }, loge => sub { return log( $_[0] ) }, log10 => sub { return log( $_[0] )/log(10.0) }, round => sub { return int( $_[0] + $_[ 0 ] < 0 ? -0.5 : 0.5 ) }, trunc => sub { return int($_[0]) }, f1 => sub { return sprintf "%.1f", $_[0] }, f2 => sub { return sprintf "%.2f", $_[0] }, f3 => sub { return sprintf "%.3f", $_[0] }, f4 => sub { return sprintf "%.4f", $_[0] }, f5 => sub { return sprintf "%.5f", $_[0] }, ); my @ops = map{ m[^[-+](.*)] && exists $funcs{ $1 } ? $funcs{ $1 } : () } @ARGV; undef @ARGV; while (<> ) { chop; my( $row, @in ) = split; print "$row ", join " ", map{ my $val = $_; $val = $_->( $val ) for @ops; $val; } @in; } __END__ P:\test>perl 391141.pl -power 1 0 0 0 1 1 1 2 2 1 1 1 1 2.71828182845905 2.71828182845905 2.71828182845905 7.389056098 +93065 7.38905609893065 2 1 2 0 1 0 0 0 1 2 2.71828182845905 7.38905609893065 1 2.71828182845905 1 1 1 2.7182818 +2845905 3 2 3 4 5 6 0 1 2 3 7.38905609893065 20.0855369231877 54.5981500331442 148.413159102577 +403.428793492735 1 2.71828182845905 7.38905609893065 ^Z P:\test>perl 391141.pl -power -trunc 1 0 0 0 1 1 1 2 2 1 1 1 1 2 2 2 7 7 2 1 2 0 1 0 0 0 1 2 2 7 1 2 1 1 1 2 3 2 3 4 5 6 0 1 2 3 7 20 54 148 403 1 2 7 ^Z

Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
"Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon

Replies are listed 'Best First'.
Re^2: Dynamic function chains?
by keymon (Beadle) on Sep 15, 2004 at 15:08 UTC
    The output I showed should have been for "power2" (or "exp2").. my mistake.

    In your solution, you are not constructing a composite subroutine, but instead iterating over all of them in the loop:         $val = $_->( $val ) for @ops;

    Wouldn't a composite subroutine be more efficient?

    There's a trivial mistake in your solution in that it performs the operations in the reverse order (which is fixed by using

    reverse @ARGV
    in the map statement).

    I was aiming to keep the order, because then if you want:
    f(g(h(x)))
    you can say:
    funcs.pl -f -g -h

      Wouldn't a composite subroutine be more efficient?

      Generally, evaling at runtime is quite expensive. You'd have to benchmark to say for sure. I guess it depends upon how many time your going to call the composite sub. If your processing large datasets, the cost of eval may become insignificant.

      I usally avoid evaling stuff input from the command line, though ]broquaint]'s use of package->can() seems to avoid most of the risks that usually entails. I liked his solution a lot.

      Yes, I screwed up the order. reverse is the easiest solution.


      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "Think for yourself!" - Abigail
      "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon