Next Tricks of the Wizards 175

Iterator Operations

        sub both {
          my ($it1, $it2) = @_;
          my ($a, $b) = ($it1->(), $it2->());
          sub {
            return undef unless defined $a || defined $b;
            my $rv;
            if ($a lt $b || ! defined $b) {
              $rv = $a;
              $a = $it1->();
            } if ($b lt $a || ! defined $a) {
              $rv = $b;
              $b = $it2->();
            } else {          # $a eq $b
              $rv = $a;
              ($a, $b) = ($it1->(), $it2->());
            } 
            return $rv;
          }  
        }


Next Copyright © 2003 M. J. Dominus