Unwrapping data while mapping

When we use a construct, such as (f(x) for x, y in C), we use multiple assignments in the for statement to unwrap a multi-valued tuple and then apply a function. The whole expression is a mapping. This is a common Python optimization to change the structure and apply a function.

We'll use our trip data from Chapter 4, Working with Collections. The following is a concrete example of unwrapping while mapping:

from typing import Callable, Iterable, Tuple, Iterator, AnyConv_F = Callable[[float], float]Leg = Tuple[Any, Any, float]def convert(        conversion: Conv_F,         trip: Iterable[Leg]) -> Iterator[float]:    return (        conversion(distance) for start, end, distance in trip    )

This higher-order function would be supported by ...

Get Functional Python Programming - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.