Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re (tilly) 1 (perl): What Happened...(perils of porting from c)

by tilly (Archbishop)
on Jan 05, 2001 at 23:43 UTC ( #50132=note: print w/ replies, xml ) Need Help??


in reply to What Happened...(perils of porting from c)

Here is a native Perlish thing that basically does what I think you really want. Note how much shorter this is than the C version.

use strict; nested_for( sub {print "@_\n";}, [1..2], ['a'..'c'], ['A'..'D'] ); sub nested_for { ret_iter(@_)->(); } sub ret_iter { my $fn = shift; my $range = shift; my $sub = sub {$fn->($_, @_) for @$range}; return @_ ? ret_iter($sub, @_) : $sub; }
Also note the lack of pointer arithmetic to mess up on. :-)


Comment on Re (tilly) 1 (perl): What Happened...(perils of porting from c)
Download Code
Replies are listed 'Best First'.
Re: Re (tilly) 1 (perl): What Happened...(perils of porting from c)
by Madams (Monk) on Jan 06, 2001 at 05:20 UTC
    Oh Wow!

    I bow to tilly!

    I am going to REALLY study this: it works! correctly! it's one-third the size! it looks tricky but not f**** insane! and NO POINTER ARITHMETIC!!!!

    This is the sort of answer that shows that i'm a tyro at perl AND why! I'm obviously still stuck in some sort of tunnel vision caused by programming in c/c++.

    To me this is a great Zen Koan to meditate on!

    Thanks tilly, and everyone who responded to this query!

    ---madams :) x $POSIX::LDBL_MAX
Re: ancient masterpiece
by Roy Johnson (Monsignor) on Jun 02, 2005 at 22:27 UTC
    That was beautiful. I'm writing primarily so more people can see it. I also did a little rewrite that isn't an improvement, per se, but is AWTDI and may help people understand what's going on. (Iterating instead of recursing.)
    sub ret_iter { my $sub = shift; while (my $range = pop) { my $inner = $sub; $sub = sub { $inner->(@_, $_) for @$range }; } return $sub; }
    I made it so it changes elements on the right fastest, like a counter.

    Caution: Contents may have been coded under pressure.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://50132]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (14)
As of 2015-07-29 21:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (269 votes), past polls