from typing import Callable
from typing import Any
from typing import Iterable
from typing import Iterator
import itertools


def flatmap(
        func: Callable[[Any], Any],
        iterable: Iterable[Any],
) -> Iterator[Any]:
    """Apply a function to items of an iterable and flatten the result.

    Arguments:
        func: single-argument callable to be applied to each item
        iterable: object with items to be flattened

    Examples:

    >>> shows = [
    ...     {'id': 1, 'topics': [1, 2, 3]},
    ...     {'id': 2, 'topics': [1, 4, 5]},
    ...     {'id': 3, 'topics': [2, 5, 6]},
    ... ]
    >>> def show_topics(show):
    ...     return [
    ...         {'id': show['id'], 'topic': topic}
    ...         for topic in show['topics']
    ...     ]
    >>> list(flatmap(show_topics, shows))  # doctest: +ELLIPSIS
    [{'id': 1, 'topic': 1}, ..., {'id': 3, 'topic': 5}, {'id': 3, 'topic': 6}]

    """
    return itertools.chain.from_iterable(map(func, iterable))