Return-Path: Mailing-List: contact mjd-book-help@plover.com; run by ezmlm Delivered-To: mailing list mjd-book@plover.com Received: (qmail 16013 invoked by uid 119); 9 Sep 2001 23:46:19 -0000 Date: 9 Sep 2001 23:46:19 -0000 Message-ID: <20010909234619.16012.qmail@plover.com> From: mjd@plover.com To: mjd-book@plover.com Subject: Mark Dominus book news: Chapter IV Still in Progress Organization: Plover Systems If you forgot what this list is about, or you don't know why you're getting this message, please see http://perl.plover.com/book/ To unsubscribe, send a blank message to mjd-book-unsubscribe@plover.com. It's been a while since I reported anything, so I thought I'd drop you all a note. The summer was hectic as usual, because I had to keep running off to conferences and things. Each time I got home, it took several more days before I was able to continue working. I hoped Chapter IV would be done by now, and the summer conferences are part of the reason why not. But the other part of the reason is that iterators have turned out to be even more interesting than I thought, so that I can't stop writing about them. The draft of Chapter IV is over 21,000 words now, and according to my outline, I still have several major sections to go. Back in June I estimated that the chapter would be about 20,000-22,000 words long when I got it done, but now it's looking more like 28,000-32,000. I will probably break it into two chapters when it's done. (It seems to have eaten part of chapter VI anyway.) I'm now posting daily status reports on my web site, in case you want track the progress of the book more closely. Visit http://perl.plover.com/book/chap04.html to see where I am. This page has a complete table of contents and word count for chapter IV, and I've been updating it daily, so you can watch the chapter growing. When I finish chapter IV, I'll do the same thing for Chapter V. One of the brightest spots in Chapter IV is that I found another way to convert recursive functions to iterators without exercising a lot of ingenuity. It's always exciting and a little scary to write about something like that, because the working out of the technique and the writing about it happen simultaneously, and there's always a chance that I'll get three-quarters of the way through the explanation and then discover that the idea doesn't work after all and that I have to throw the whole thing away. This time it did work, and to check it out I took the nasty recursive 'permute' function from the Perl FAQ and turned it into an iterator. Here's the FAQ version: # tsc-permute: permute each word of input sub permute { my @items = @{ $_[0] }; my @perms = @{ $_[1] }; unless (@items) { print "@perms\n"; } else { my(@newitems,@newperms,$i); foreach $i (0 .. $#items) { @newitems = @items; @newperms = @perms; unshift(@newperms, splice(@newitems, $i, 1)); permute([@newitems], [@newperms]); } } } Here's the iterator version: # it-permute: construct an iterator to permute each word of input sub permute { my @items = @{ $_[0] }; my @perms = @{ $_[1] }; unless (@items) { return \@perms; } else { append(imap { my(@newitems,@newperms); @newitems = @items; @newperms = @perms; unshift(@newperms, splice(@newitems, $_, 1)); permute([@newitems], [@newperms]); } upto(0,$#items)); } } As you can see, the code is almost exactly the same. C, C, and C are stock utility functions that will be used over and over. (For eample, 'imap' is like an iterator version of Perl's built-in 'map' utility.) For the complete code, see [ Sorry, freebies are available only to mailing list subscribers. Send mail to mjd-book-subscribe@plover.com to subscribe. ] The iterator behaves exactly like the original version, even reproducing the out-of-order return values and weird calling syntax of the original. The difference? Instead of printing out a gigantic amount of probably-useless output, it returns one permutation at a time. You can call it a few times to get a few permutations, then go off and do something else for a while, then call for some more permutations, and it will pick up where it left off, just like a filehandle. As usual, please do not distribute this file to anyone, and please do not advertise the URL to anyone else. However, please do invite people to subscribe to this mailing list. (To subscribe, send email to mjd-book-subscribe@plover.com.) The subscription welcome message will tell newcomers where to find the goodies. Thank you all for your interest. I will send another message next time something happens, or perhaps next month even if nothing has happened.