Book Image

Functional Python Programming

By : Steven F. Lott, Steven F. Lott
Book Image

Functional Python Programming

By: Steven F. Lott, Steven F. Lott

Overview of this book

Table of Contents (23 chapters)
Functional Python Programming
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Functional composition and the PyMonad multiplication operator


One of the significant values of curried functions is the ability to combine them via functional composition. We looked at functional composition in Chapter 5, Higher-order Functions, and Chapter 11, Decorator Design Techniques.

When we've created a curried function, we can easily perform function composition to create a new, more complex curried function. In this case, the PyMonad package defines the * operator for composing two functions. To show how this works, we'll define two curried functions that we can compose. First, we'll define a function that computes the product, and then we'll define a function that computes a specialized range of values.

Here's our first function that computes the product:

import  operator
prod = myreduce(operator.mul)

This is based on our curried myreduce() function that was defined previously. It uses the operator.mul() function to compute a "times-reduction" of an iterable: we can call a product...