Last active
December 11, 2015 22:08
-
-
Save masak/4666979 to your computer and use it in GitHub Desktop.
Sieve of Eratosthenes in Perl 6, using streams
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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