Created
September 4, 2012 08:49
-
-
Save uchan-nos/3618684 to your computer and use it in GitHub Desktop.
generator for generator (one liner prime generator example)
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
def gen(init, ret, next): | |
while True: | |
yield ret(init) | |
init = next(init) | |
def myfor(iter, n): | |
a = [] | |
for i, x in enumerate(iter): | |
if i == n: | |
break | |
if x: | |
a.append(x) | |
return a | |
print myfor(gen( (3, [2]), lambda (p, ps) : (p - 2 if p - 2 in ps else ''), lambda (p, ps) : (p + 2, (ps if (0 in map(lambda x : p % x, ps)) else ps + [p]))) , 10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment