Skip to content

Instantly share code, notes, and snippets.

@Diapolo10
Last active February 20, 2021 17:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Diapolo10/e2141b455267b1e2658ef96e6fba2b84 to your computer and use it in GitHub Desktop.
Save Diapolo10/e2141b455267b1e2658ef96e6fba2b84 to your computer and use it in GitHub Desktop.
Python map-function example implementation
#!/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