Next | Tricks of the Wizards | 175 |
If the iterator returns the list items in some canonical order, you can do this:
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; } }
This function works for any iterators that return items in alphabetical order
If an iterator represents a database query, this is the OR operation
Next | Copyright © 2003 M. J. Dominus |