Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

You use continuations to turn call-backs into iterators.

Oh, you wanted more of an explanation than that? Hmm. Okay.

First, you need to understand why call-backs suck and iterators are better so read Re: Are you looking at XML processing the right way? (merge).

Now, why is a call-backs API easier to provide? Well, for once thing, you can use recursion. Take something like File::Find. Let's make one that is extremely simple:

sub find { my( $callback, $subdir, $path )= @_; $path= "" if ! defined $path; $path .= "/" if "" ne $path; $path .= $subdir; chdir $subdir or die "Can't chdir($path): $!\n"; my @files= ( glob(".*"), glob("*") ); for my $file ( @files ) { next if $file eq "." or $file eq ".."; if( ! -l $file && -d _ ) { find( $callback, $file, $path ); } $callback->( $file, $path ); } chdir("..") or die "Can't chdir out of $path: $!\n"; }
and now let's turn it into an iterator:
sub find { my( $subdir, $path )= @_; $path= "" if ! defined $path; $path .= "/" if "" ne $path; $path .= $subdir; chdir $subdir or die "Can't chdir($path): $!\n"; my @files= ( glob(".*"), glob("*") ); for my $file ( @files ) { next if $file eq "." or $file eq ".."; if( ! -l $file && -d _ ) { find( $file, $path ); } RETURN( $file, $path ); } chdir("..") or die "Can't chdir out of $path: $!\n"; }
Do you see the problem? Here I am N layers deep in recursion with lots of interesting stuff in N instances each of @files, $path, $subdir, and whatever my for loop uses to keep track of where it is, and I want to return! I not only want to return all the way up my N layers of recursion, but I want to be able to come back right where I left off with my whole call stack just the way I left it so I continue right where I left off.

Well that is what continuations let you do (so I've heard, I haven't really studied them so I could well be wrong, but if so, someone will point this out and I'll learn something).

You can certainly turn the above code into an iterator without continuations, but it means getting rid of the recursion by implementing your own stack and stuffing that into some context container that the caller has to give back to you some way (usually via OO).

                - tye

In reply to Re: (Perl6) Groking Continuations (iterators) by tye
in thread (Perl6) Groking Continuations by crenz

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-03-19 11:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found