Last active
February 20, 2021 17:39
-
-
Save Diapolo10/e2141b455267b1e2658ef96e6fba2b84 to your computer and use it in GitHub Desktop.
Python map-function example implementation
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
#!/usr/bin/env python3 | |
from typing import Any, Callable, Generator, Iterable, List | |
def my_map(func: Callable[[Any, ...], Any], *iterables: List[Iterable[Any]]) -> Generator[Any, None, None]: | |
""" | |
Mimics the built-in map function, accepting any number of iterables | |
and yielding values returned by the given function. | |
""" | |
iterators = [iter(iterable) for iterable in iterables] | |
while True: | |
try: | |
yield func(*[next(iterator) for iterator in iterators]) | |
except StopIteration: | |
break | |
def my_simpler_map(func: Callable[[Any], Any], iterable: Iterable[Any]) -> Generator[Any, None, None]: | |
"""Unlike the built-in map, this version only accepts one iterable.""" | |
for value in iterable: | |
yield func(value) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment