Function composition in Python

The pointfree module is about using function composition notation in conjunction with automatic partial application. Both of these features are achieved by wrapping functions in the pointfree class (which can also be applied as a decorator).

Several “pre-wrapped” helper functions are provided by the module. For instance, if you wanted to define a function that returns the sum of squares of the lengths of the strings in a list, you could do so by combining the helpers pfmap() and pfreduce():

>>> from pointfree import * >>> from operator import add  >>> fn = pfmap(len) >> pfmap(lambda n: n**2) >> pfreduce(add, initial=0) >>> fn(["foo", "barr", "bazzz"]) 50

Interesting module. I’ve been looking for something like this in order to build a stream-like composition of functions to process huge datasets, and maybe I could use this.

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s