30/04: iterators in perl using unix pipes.
sub yieldfor
(&\
&) {
if (open(TO
, "-|")) {
while(<TO>) {
&{$_[0]};
}
close(TO
);
} else {
&{$_[1]};
}
}
sub yield
($) {
print "$_[0]\n";
}
example usage
sub fib
() {
my ($a, $b) = (0, 1);
while (1) {
yield
$b;
($a, $b) = ($b, $a + $b);
}
}
yieldfor
{
print;
} &fib
;