Skip to content

Instantly share code, notes, and snippets.

@ashgti
Forked from pmichaud/more zip versions
Created January 25, 2010 20:33
Show Gist options
  • Save ashgti/286221 to your computer and use it in GitHub Desktop.
Save ashgti/286221 to your computer and use it in GitHub Desktop.
# version 1
sub infix:<Z>(*@@args) {
my @lists = @@args>>.list;
gather {
take @lists>>.shift while all(@lists);
}
}
# version 2
# Assume Iterator has .next ("move to next element, return true/false")
# and .value ("give me the element you're currently on") methods.
# Then we have:
sub infix:<Z>(*@@args) {
my @iters = @@args>>.iterator;
gather {
while all(@iters>>.next) { take @iters>>.value; }
}
}
# version 3, using sentinel return from Iterator
sub infix:<A>(*@@args) {
my @iters = @@args>>.iterator;
gather {
loop {
my @results = @iters>>.get;
last if any(@results) == SENTINEL;
take @results;
}
}
}
# version 4, using an exception
sub infix:<A>(*@@args) {
my @iters = @@args>>.iterator;
gather {
loop {
my @results = @iters>>.get;
take @results;
}
CATCH SENTINEL {
last;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment