http://www.perlmonks.org?node_id=1039774


in reply to Transform Sequence Problem

> This works okay, but I wonder if there is a better and faster way.

Like always... it depends what you wanna do and how flexible you need to be!

If you are calling a fixed trans() very often, you could consider eval to generate it with chained map statements.

something like

 eval 'sub trans {  map {$_ + 1} map {log($_)} map {$_ * 3} @_ }'

You'll need to hold the map-codes as an array of strings.

But your functional approach is more flexible (eg. for debugging) ! =)

edit

you could avoid 3 loops by applying the transformations directly in one loop

 eval 'sub trans {  map { log($_ * 3) + 1} @_ }'

again this can be constructed as strings (s///ubstituting $_) and evaled ....

...OR functionally with

 sub trans {  map { $f1->($f2->($f3->($_))) } @_ }

update

or better (untested)

sub trans { for my $val (@_) { $val = $_->( $val) for @transforms; } return @_; }

@transform needs to be in the closure, maybe consider passing it as arr-ref as first argument.

 $a_transforms =  shift

Cheers Rolf

( addicted to the Perl Programming Language)