Python DOES Functional

Charles L. Yost

2019-05

Speaker Bio

Charles Yost is currently the Vision Agent Team Lead and a Security Developer at Binary Defense in Stow Ohio. He has worked in the IT industry for over a decade in a wide variety of languages including (but not limited to) VB6, VB.Net, C#, F#, Python, and C/C++. Throughout life, his number one passion has been learning new skills, which lends itself nicely to technology (a field known for its fast pace of change and evolution). Charles enjoys teaching and talking to others about technology. He is a member of NEOISF, and attends as many conferences as he can justify with his wife.

Binary Defense

Binary Defense Systems is a local Managed Security Service Provider. We provide a full range of MSSP services including: monitoring, consulting, threat intelligence, and incident response. We also are the creators of Vision, a real-time software solution which gives organizations immediate visibility into the latest attack vectors. Contact us at https://binarydefense.com/

Speaker Contact

Twitter: @CHARLESLYOST

GitHub & YouTube: Yoshi325

Pre-Talk Q & A

  • What are you looking for from this Talk?
  • I might call a L.A.F.O.

Summary

Secret

Want More Later?

Want more information, with more accurate terms?

https://docs.python.org/3.7/howto/functional.html

Terms & Concepts

Let's learn the lingo.

  • Side Effects
  • Pure Functions
  • Referential Transparency
  • First-Class Functions
  • Higher-Order Functions
  • Partial Function Application
  • Currying
  • Function Composition
  • Pipeline Style

Side Effects

Manipulation of state outside the function.

Pure Functions

Functions without Side Effects.

Referential Transparency

One or more Pure Functions (composed to) create a result that is repeatable from the same set of initial inputs.

First-Class Functions

This means the language supports passing functions as arguments to other functions, returning them as the values from other functions, and assigning them to variables or storing them in data structures

https://en.wikipedia.org/wiki/First-class_function

Higher-Order Functions

Functions that operate on (and return) Functions.

Partial Function Application

[...] fixing a number of arguments to a function, producing another function of smaller arity.

https://en.wikipedia.org/wiki/Partial_application

Partial Function Application

Currying

Taking a function with multiple arguments, and re-expressing it as a series of functions with one argument.

Function Composition

Composing multiple functions to produce a single result.

Pipeline Style

Nope.

Terms & Concepts

More lingo.

  • Tail Call Optimization
  • Pattern Matching
  • Data Modeling
  • Immutability
  • Algebraic Data Types

Tail Call Optimization

Nope. Python doesn't have this either.

Tail Call Optimization

Tail-call optimization is where you are able to avoid allocating a new stack frame for a function because the calling function will simply return the value that it gets from the called function. The most common use is tail-recursion, where a recursive function written to take advantage of tail-call optimization can use constant stack space.

https://stackoverflow.com/a/310980/135342

Tail Call Optimization

You'll know you want this when you hit the recursion limit in Python.

Pattern Matching

Ugh. Still Nope. Python doesn't have this either.

Data Modeling

Phew. Back on track.

Accurate Data Modeling is very important to Functional Programming, because the data and functionality are firmly separated.

Data Modeling

  • dataclasses
  • type hints

Data Modeling

Immutability

by ref? by val?

stop the madness!

Algebraic Data Types

Here we go again...

Python does not natively have Algebraic Data Types.

Algebraic Data Types

But! We can emulate this with things from typing.

What is it?

Functional programming can be considered the opposite of object-oriented programming.

What is it?

Functional programming decomposes a problem into a set of functions. Ideally, functions only take inputs and produce outputs, and don’t have any internal state that affects the output produced for a given input.

Native Python Functional Affordances

(batteries included doesn't just apply to system calls)

  • functions are first class objects
  • boolean expressions are short-circuited
  • enumerate
  • any
  • all

Native Python Functional Affordances

  • iterators
  • generators
  • itertools
  • functools
    • map / filter / reduce
  • dataclasses (with frozen=True for immutability)
  • named tuples (which are immutable by nature)

Lambda

Drawbacks

(specifically when using Functional concepts in Python)

  • Tail Call Optimization (recursion limit)
  • Pattern Matching
  • Automatic Currying
  • Pipeline Syntax
  • Clean Algebraic Data Types
  • Clean Function Composition

Benefits

  • Modularity
  • Composability
  • Optimization
  • Testing

Modularity

Small pieces (functions) that are composed and applied to a model allows for reusibility.

Composability

Instead of trying to write W.E.T. functions, or putting them into odd places, composability allows you to build small pieces that you can fit in your head.

Optimization

These small pieces of functionality lend themselves to inspection, and therefore easy optimization.

Testing

Similar to what may be said about optimization, testing is easy with Functional Programming.

What does it mean to me?

Bonus

Coconut

"Coconut is a functional programming language that compiles to Python."

Pipeline Style

add_two = partial(add, 2)
mul_two = partial(mul, 2)
x = add_two(mul_two(10))

10 |> (mul_two..add_two) |> print

Tail Call Optimization

Yup. Coconut does this too.

Pattern Matching

Of course! Coconut adds the match keyword.

match [head] + tail in [0, 1, 2, 3]:
  print(head, tail)

Other

toolz <https://pypi.org/project/toolz/>

A functional standard library for Python.

Other

https://www.pyfunctional.org

PyFunctional’s API takes inspiration from Scala collections, Apache Spark RDDs, and Microsoft LINQ.

Other

https://github.com/radix/sumtypes/

Sum Types, aka Tagged Unions, for Python