daj
daj

Reputation: 7183

What's an alternative to the "functional" package for the function composition operation?

In particular, I'm looking for a package w/ support for function composition.

Googling around, there's a lot of references to Collin Winter's functional and its compose() function. However documentation for that is gone: http://www.oakwinter.com/code/functional/

which doesn't lend me much confidence in its continued support.

A few references such as https://mathieularose.com/function-composition-in-python/ provide relatively simple hand-rolled implementation. However, unless it's absolutely necessary, I'd rather go with an existing library of there's one that the community is converging around rather than reimplementing such a basic operation.

Upvotes: 2

Views: 217

Answers (1)

karlson
karlson

Reputation: 5433

I'm a big fan of toolz and use it a lot in both personal projects and at work. It's actively maintained, well documented, and seems to be quite mature despite the 0.x version number.

It offers a compose function that might be what you're looking for.

For example:

from toolz import compose

add_one = lambda x: x + 1
square = lambda x: x**2

# Add 1, then square
compose(square, add_one)(2)  # == 9

Upvotes: 1

Related Questions