Skip to content

Instantly share code, notes, and snippets.

@mebusw
Created May 25, 2014 06:08
Show Gist options
  • Save mebusw/535cc4477c6d945628a8 to your computer and use it in GitHub Desktop.
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
### 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