Last active
August 26, 2020 12:15
-
-
Save vlad-bezden/6cbb1e45914e5d5680e219ac4526c08d to your computer and use it in GitHub Desktop.
For Each example in Python
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
from typing import Any, Iterable, Callable | |
def for_each(xs: Iterable[Any], do: Callable[[Iterable[Any]], None]) -> None: | |
it = iter(xs) | |
try: | |
while True: | |
do(next(it)) | |
except StopIteration: | |
pass | |
# works with lists | |
for_each([1, 2, 3], print) | |
# works with iterators | |
for_each(range(10), print) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment