What Is Functional Programming?
Wed better start with the hardest question: What is functionalprogramming (FP), anyway?
One answer would be to say that functional programming is what you dowhen you program in languages like Lisp, Scheme, Clojure, Scala,Haskell, ML, OCAML, Erlang, or a few others. That is a safe answer, butnot one that clarifies very much. Unfortunately, it is hard to get aconsistent opinion on just what functional programming is, even fromfunctional programmers themselves. A story about elephants and blind menseems apropos here. It is also safe to contrast functional programmingwith imperative programming (what you do in languages like C, Pascal,C++, Java, Perl, Awk, TCL, and most others, at least for the most part).Functional programming is also not object-oriented programming (OOP),although some languages are both. And it is not Logic Programming (e.g.,Prolog), but again some languages are multiparadigm.
Personally, I would roughly characterize functional programming ashaving at least several of the following characteristics. Languages thatget called functional make these things easy, and make other thingseither hard or impossible:
Functions are first class (objects). That is, everything you can dowith data can be done with functions themselves (such as passing afunction to another function).
Recursion is used as a primary control structure. In some languages,no other loop construct exists.
There is a focus on list processing (for example, it is the source of the name Lisp).Lists are often used with recursion on sublists as a substitute forloops.
Pure functional languages eschew side effects. This excludes thealmost ubiquitous pattern in imperative languages of assigning firstone, then another value to the same variable to track the program state.
Functional programming either discourages or outright disallowsstatements, and instead works with the evaluation of expressions (inother words, functions plus arguments). In the pure case, one program isone expression (plus supporting definitions).
Functional programming worries about what is to be computed ratherthan how it is to be computed.
Much functional programming utilizes higher order functions (inother words, functions that operate on functions that operate onfunctions).
Advocates of functional programming argue that all these characteristicsmake for more rapidly developed, shorter, and less bug-prone code.Moreover, high theorists of computer science, logic, and math find it alot easier to prove formal properties of functional languages andprograms than of imperative languages and programs. One crucial conceptin functional programming is that of a pure functionone that always returns the same result given the same argumentswhich is more closely akin to the meaning of function in mathematics than that in imperative programming.
Python is most definitely not a pure functional programminglanguage; side effects are widespread in most Python programs. That is,variables are frequently rebound, mutable data collections often changecontents, and I/O is freely interleaved with computation. It is also noteven a functional programming language more generally. However, Pythonis a multiparadigm language that makes functional programming easy todo when desired, and easy to mix with other programming styles.
Beyond the Standard Library
While they will not be discussed withing the limited space of thisreport, a large number of useful third-party Python libraries forfunctional programming are available. The one exception here is that Iwill discuss Matthew Rocklinsmultipledispatch
as the best current implementation of the concept it implements.
Most third-party libraries around functional programming are collectionsof higher-order functions, and sometimes enhancements to the tools forworking lazily with iterators contained in itertools
. Some notableexamples include the following, but this list should not be taken asexhaustive:
pyrsistent
contains a number of immutable collections. All methods on a datastructure that would normally mutate it instead return a new copy of thestructure containing the requested updates. The original structure isleft untouched.
toolz
provides a set ofutility functions for iterators, functions, and dictionaries. Thesefunctions interoperate well and form the building blocks of common dataanalytic operations. They extend the standard libraries itertools
andfunctools
and borrow heavily from the standard libraries ofcontemporary functional languages.
hypothesis
is alibrary for creating unit tests for finding edge cases in your code youwouldnt have thought to look for. It works by generating random datamatching your specification and checking that your guarantee still holdsin that case. This is often called property-based testing, and waspopularized by the Haskell library QuickCheck.
more_itertools
tries to collect useful compositions of iterators that neitheritertools
nor the recipes included in its docs address. Thesecompositions are deceptively tricky to get right and this well-craftedlibrary helps users avoid pitfalls of rolling them themselves.
Resources
There are a large number of other papers, articles, and books writtenabout functional programming, in Python and otherwise. The Pythonstandard documentation itself contains an excellent introduction calledFunctional Programming HOWTO, by Andrew Kuchling, that discusses some of themotivation for functional programming styles, as well as particularcapabilities in Python.
Mentioned in Kuchlings introduction are several very old public domainarticles this author wrote in the 2000s, on which portions of this reportare based. These include: