Skip to content

Instantly share code, notes, and snippets.

@thuhak
Last active August 18, 2020 11:08
Show Gist options
  • Save thuhak/18fdd503d111cfe418685569c4e314da to your computer and use it in GitHub Desktop.
Save thuhak/18fdd503d111cfe418685569c4e314da to your computer and use it in GitHub Desktop.
def cache(iterable, count: int):
"""
range(10), count=3 -> [0,1,2],[3,4,5],[6,7,8],[9]
"""
assert(count > 0)
saved = []
index = 0
for item in iterable:
if index < count:
saved.append(item)
index += 1
else:
yield saved
index = 1
saved = [item]
yield saved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment