Skip to content

Instantly share code, notes, and snippets.

@juanarrivillaga
Created June 2, 2017 21:39
Show Gist options
  • Save juanarrivillaga/087e5a2084b1d9c187c9ca3fb7e294a4 to your computer and use it in GitHub Desktop.
Save juanarrivillaga/087e5a2084b1d9c187c9ca3fb7e294a4 to your computer and use it in GitHub Desktop.
import string
from collections import deque
from itertools import islice
consumer = deque(maxlen=0)
consume = consumer.extend
def generator(iterable, steps, consume=consume):
it = iter(iterable)
yield next(it)
for step in steps:
consume(islice(it, step))
yield next(it)
# >>> list(generator(string.ascii_lowercase, [0, 1, 2, 3]))
# ['a', 'b', 'd', 'g', 'k']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment