Saturday, June 17, 2017

Composing Functions

The code below shows a way of teaching operator overloading in Python, and about lambda.

Function type objects do not ordinarily multiply, leaving it up to the developer to ascribe meaning to said operation.  We might use it for "compose" such that (F * G)(x) == F(G(x)).

The __call__ method invokes the original function, while Compose wraps the function inside the type that knows how to multiply and therefore power.

At the end, decorator syntax is introduced.  I explain decorator syntax in more detail in this Youtube.

Hit Run for the demo, output appears below.