Created
May 25, 2014 06:08
-
-
Save mebusw/535cc4477c6d945628a8 to your computer and use it in GitHub Desktop.
simulate "lazy map" of Clojure in Python, which can just iterate given times with an infinite loop
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
### In Clojure, it's written as "take 5 (nToTicket (iterate inc))" | |
def inc(): | |
n = 0 | |
while True: | |
yield n | |
n += 1 | |
def lazyMap(f, xs, times=None): | |
time = 0 | |
for x in xs(): | |
if times and time < times: | |
time += 1 | |
yield f(x) | |
else: | |
return | |
### Test | |
def nToTicket(n): | |
return "Ticket %d" % n | |
for m in lazyMap(nToTicket, inc, 5): | |
print m |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment