Skip to content

Instantly share code, notes, and snippets.

@spinclad
Forked from pmichaud/more zip versions
Created January 26, 2010 04:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spinclad/286535 to your computer and use it in GitHub Desktop.
Save spinclad/286535 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:<Z>(*@@args) {
my @iters = @@args>>.iterator;
gather {
loop {
my @results = @iters>>.get;
last if any(@results) == SENTINEL;
take @results;
}
}
}
# version 4, using two-continuation .get2
# assume $stream.get2(:on_end(&code)) does like so:
# case Just $value: return $value
# case End: call code(), never return
sub infix:<Z>(*@@args) {
my @iters = @@args>>.iterator;
gather {
loop {
my @results = @iters>>.get2(:on_end{ last; });
take @results;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment