Skip to content

Instantly share code, notes, and snippets.

@mebusw
Last active August 20, 2016 06:45
Show Gist options
  • Save mebusw/858c4f3bd6d0de41472d6e0a4ae2bfe0 to your computer and use it in GitHub Desktop.
Save mebusw/858c4f3bd6d0de41472d6e0a4ae2bfe0 to your computer and use it in GitHub Desktop.
functional programming for Primes, inspired by Haskell example
# !encoding=utf8
# primes = filterPrime [2..]
# where filterPrime (p:xs) =
# p : filterPrime [x | x <- xs, x `mod` p /= 0]
# Infinite List
from pipe import *
from pipe import itertools
def primes(xs):
while 1:
p = next(xs)
yield p
xs = (lambda xs, p: (x for x in xs if x % p != 0))(xs, p)
print primes(itertools.count(2)) | take(25) | as_list
# Finite List
def rc(s):
return [s[0]] + rc([x for x in s[1:] if x % s[0] != 0]) if len(s) > 1 else []
print rc(range(2, 103))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment