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

Applying a function to data via starmap() and map()

The built-in map() function is a higher-order function that applies a function to items from an iterable. We can think of the simple version of the map() function, as follows:

map = (lambda function, arg_iter:     (function(a) for a in arg_iter))

This works well when the arg_iter parameter is an iterable that provides individual values. The actual map() function is a bit more sophisticated than this, and will work with a number of iterables.

The starmap() function in the itertools module is simply the *a version of the map() function. We can imagine the definition as follows:

starmap = (lambda function, arg_iter:     (function(*a) for a in arg_iter))

This reflects a small shift in the semantics ...

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