Re: Arbitrarily Many Nested Loops



On 30 Mar 2006 01:32:11 -0800, Jacob JKW <jacobcdf@xxxxxxxxx> wrote:

David Formosa (aka ? the Platypus) wrote:

[...]

I quickly worked out one way to do this, no guarties as to effeceny.

sub closefor (&$@) {
my $sub = shift;
my $range = shift;

return sub {
for my $i (0..$range){
$sub->($i,@_)
}
}
}

sub mulitloop (&@) {
my $sub = shift;
for (@_) {
my $oldsub = $sub;
$sub = closefor {$oldsub->(@_)} $_;
}
$sub->();
}
[...]
mulitloop {
my $sum = 0;
my $product = 1;
for (my $i; $i<=@#_; $i++) {
$sum += $_[$i];
$product *= $f_raa->[$i]->[$_[$i]];
}
$prob_ra->[$sum] += $product;
} @$n_ra;

You know as clever and elegant as this method is, it actually runs
significantly than my original ugly cut-and-paste style,

Yeah after thinking about it the above code does some things
redundently and actually causes perl to do alot of extra needless
work. This should be a little more effecent.

sub calc_prob_ra ($$$;@) {
my $sum = shift;
my $prod = shift;
my $depth = shift;

if (@_) {
my $n = shift;

calc_prob_ra($sum+$n,$prod * $f_aa->[$depth]->[$_],$depth+1,@_)
for (0..$n);

} else {
$prob_ra->[$sum] += $prod;
}
}

calc_prob_ra(0,1,1,@$n_ra);

There should be signifigently less overhead in this case (I've
eleminated a needless loop, and all the closure creation and
derefrencing).

[...]

Anyway, I'm going to keep this code for further use and some later
date, but I eventyually went with John Krahn eval method posted
above.

I should bench test Mr Krahn's eval method vs my new method. But Its
to late and I'm far to tired.

--
Please excuse my spelling as I suffer from agraphia. See
http://dformosa.zeta.org.au/~dformosa/Spelling.html to find out more.
Free the Memes.
.



Relevant Pages

  • sitemap generator for Perl
    ... I want to run the sitemap generator ... Returns the minimum number of links to traverse from the root URL of ... my $class = shift; ...
    (perl.beginners)
  • Re: How can I create instantiable objects (not classes)?
    ... a child object inherits not only its parent object's ... sub fee { ... my $class = shift; ... For example, an object of type Car might receive a message named "ticket," and since a car does not know what to do with a ticket, it would pass that message to an object of type Driver. ...
    (comp.lang.perl.misc)
  • Re: Packages and returning errors
    ... > array intact. ... sub is_a_instance_method { ... my $class = shift; ... You need to fix the scope of $error by moving its declaration outside ...
    (comp.lang.perl.misc)
  • Re: passing database data to a sub
    ... > I'm not sure of the difference, why isn't it a subroutine? ... > sure about this 'shift' thing anyway :-) ... > sub teardown ... > # Setup the template to use for the output. ...
    (perl.beginners)
  • Re: Massive failed FTP attempts.
    ... made it a little more generic so that it could monitor any log file. ... To run as a daemon and monitor proftpd, sending lockout notifications to ... sub prune_old_entries ... my $line = shift; ...
    (Security-Basics)