Skip to Content
Functional Python Programming - Second Edition
book

Functional Python Programming - Second Edition

by Steven F. Lott
April 2018
Intermediate to advanced content levelIntermediate to advanced
408 pages
10h 42m
English
Packt Publishing
Content preview from Functional Python Programming - Second Edition

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 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Functional Python Programming - Third Edition

Functional Python Programming - Third Edition

Steven F. Lott

Publisher Resources

ISBN: 9781788627061Supplemental Content