Skip to content

Instantly share code, notes, and snippets.

@masak
Last active December 11, 2015 22:08
Show Gist options
  • Save masak/4666979 to your computer and use it in GitHub Desktop.
Save masak/4666979 to your computer and use it in GitHub Desktop.
Sieve of Eratosthenes in Perl 6, using streams
class FilteringStream { ... }
role Stream {
method next { ... }
method value { ... }
method filter {
return FilteringStream.new(:factor($.value), :substream(self));
}
}
class StreamOfIntegers does Stream {
has $.value is rw = 1;
method next { ++$.value; self }
}
class FilteringStream does Stream {
has $.factor = die "Must provide factor";
has $.substream = die "Must provide substream";
has $.value is rw;
method next {
repeat until $.value % $.factor {
$.value = $.substream.next.value;
}
return self;
}
}
say .next.value for StreamOfIntegers.new, *.filter ... *;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment