Hmm, I think I own you now - this uses code that writes code:
This code, when parsed by tpage which comes with Template will create a special function for each of the cases with an optimised regexp in it. These functions are then accessed through a hash.[%# Run this through tpage - from Template %] [% numbers = [ 1, 2, 3, 4, 5, 6 ] -%] [% end = '\)' -%] [% start = '\w+\(' -%] my %functions = (); [% FOR level = numbers -%] $functions{ [% level %] } = sub { $_[0] =~ s/[% end %]{[% level %]}$//o; $_[0] =~ s/^([% start %]){[% level %]}//o; return $_[0]; }; [% END -%] sub get_proparg_evdb { $_ = $_[0]; my $level = $_[1] || return $_; if ($level == -1) { m/\(([^()]+)\)+/; return $1; } return &{$functions{ $level }}( $_ ) || die 'Add universal function here'; }
The complete code that is produced is here:
my %functions = (); $functions{ 1 } = sub { $_[0] =~ s/\){1}$//o; $_[0] =~ s/^(\w+\(){1}//o; return $_[0]; }; $functions{ 2 } = sub { $_[0] =~ s/\){2}$//o; $_[0] =~ s/^(\w+\(){2}//o; return $_[0]; }; $functions{ 3 } = sub { $_[0] =~ s/\){3}$//o; $_[0] =~ s/^(\w+\(){3}//o; return $_[0]; }; $functions{ 4 } = sub { $_[0] =~ s/\){4}$//o; $_[0] =~ s/^(\w+\(){4}//o; return $_[0]; }; $functions{ 5 } = sub { $_[0] =~ s/\){5}$//o; $_[0] =~ s/^(\w+\(){5}//o; return $_[0]; }; $functions{ 6 } = sub { $_[0] =~ s/\){6}$//o; $_[0] =~ s/^(\w+\(){6}//o; return $_[0]; }; sub get_proparg_evdb { $_ = $_[0]; my $level = $_[1] || return $_; if ($level == -1) { m/\(([^()]+)\)+/; return $1; } return &{$functions{ $level }}( $_ ) || die 'Add universal function here'; }
Rate ori m-elegant ori++ evdb ori 6234/s -- -36% -40% -51% m-elegant 9690/s 55% -- -7% -24% ori++ 10373/s 66% 7% -- -18% evdb 12690/s 104% 31% 22% --
Code that writes code - tip 29 from the pragmatic programmer.
--tidiness is the memory loss of environmental mnemonics
In reply to Re: Re: Re: Re: Peeling the Peelings
by EvdB
in thread Peeling the Peelings
by PetaMem
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |