Skip to content

Instantly share code, notes, and snippets.

@vlad-bezden
Last active August 26, 2020 12:15
Show Gist options
  • Save vlad-bezden/6cbb1e45914e5d5680e219ac4526c08d to your computer and use it in GitHub Desktop.
Save vlad-bezden/6cbb1e45914e5d5680e219ac4526c08d to your computer and use it in GitHub Desktop.
For Each example in Python
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