Python's equivalents:
Several notes:
Python uses the reverse order for the arguments for the map()
/filter()
defined in the book: in Python, the function comes first, then the collection.
Python has banished reduce()
to the functools
module; Python's creator, Guido van Rossum, finds it conceptually complex (he's not wrong). Consequently there are multiple options for doing reduce()
-like behavior in Python:
- Guido's preferred normal
for
loop. - Use
functools.reduce()
. - Pull in a third party library, like pydash, which has its own reduce() implementation.
Python also has list comprehensions, which do mapping and filtering inline:
friends = [person for person in people if person.is_friend] # filters
greetings = [greet(name) for name in friends] # maps
greetings = [greet(person.name) for person in people if person.is_friend] # maps & filters
The downside to comprehension (list and dict) is that they're not first class constructs, so they're not easily composable into a chain of operations.