Last active
December 17, 2015 16:59
-
-
Save maknoll/5642577 to your computer and use it in GitHub Desktop.
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 Array | |
def comprehend &block | |
return self unless block | |
head, *tail = self.map(&:to_a) | |
return head.map(&block).compact if tail.empty? | |
return head.product(*tail).map(&block).compact | |
end | |
end | |
def sieve list | |
return [] if list.empty? | |
[x = list.shift] + sieve([list].comprehend { |y| y if y % x != 0 }) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment