Skip to content

Instantly share code, notes, and snippets.

@maknoll
Last active December 17, 2015 16:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maknoll/5642577 to your computer and use it in GitHub Desktop.
Save maknoll/5642577 to your computer and use it in GitHub Desktop.
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