|Perl Monk, Perl Meditation|
In hopes of better understanding what you were doing, I implemented powersets first as a recursive function and then as an iterator that tries to parallel the recursive version. I thought you might be interested to see what I came up with.
The recursive version returns a list of three sub-lists: the first element, the first element mapped onto all the powerset of the tail, and the powerset of the tail. These are the three modes that an iterator can be in.
My iterator factory begins by recursively calling itself, creating an iterator for the tail of the list, then it returns a closure. The code in the closure checks the mode and uses the tail-iterator (if necessary) to generate the appropriate set: In Mode 0, just the first element is returned; in Mode 1, the first element and the next powerset of the tail; Mode 1 may "fall through" into Mode 2, where just the next powerset of the tail is returned. Upon reaching the end of its powerset, the iterator returns an empty arrayref, and resets the mode to 0 so it can be re-run.
The recursive use of the factory simplifies bookkeeping quite a bit, I think, and it only generates N iterators for a list of N elements, so it's in keeping with the no-explosive-growth objective of having iterators. It also begins cranking out results immediately, even when called with 1..45 as arguments, unlike the recursive version.
Caution: Contents may have been coded under pressure.
In reply to Re^3: Generating powerset with progressive ordering
by Roy Johnson