Created
October 29, 2013 19:46
-
-
Save carymrobbins/7221296 to your computer and use it in GitHub Desktop.
Index function for iterables. Particularly handy when dealing with infinite iterables.
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
from itertools import islice | |
def iindex(iterable, index): | |
""" Indexing for all iterables. Translates to iterable[index]. """ | |
return next(islice(iterable, index, index + 1)) | |
def example(): | |
def fibs(): | |
x, y = 0, 1 | |
yield x | |
yield y | |
while True: | |
x, y = y, x + y | |
yield y | |
# Get the 100th fibonacci number | |
iindex(fibs(), 100) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment