Created
August 19, 2016 14:44
-
-
Save adammartinez271828/dda2ecc97f46ab59a46fdca9cd1899be to your computer and use it in GitHub Desktop.
yieldwhile
This file contains hidden or 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
def yieldwhile(predicate, iterable): | |
"""Yield every element from the iterable as long as the predicate is True | |
This is a counterpart to itertools.dropwhile. | |
Args: | |
predicate (function): a function taking a single item from iterable as an argument, | |
and returning a boolean | |
iterable (iterable): an iterable | |
Returns: | |
(generator) all elements from iterable as long as the predicate is True / until the predicate becomes False | |
""" | |
iterable = iter(iterable) | |
for item in iterable: | |
if predicate(item): | |
yield item | |
else: | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment