Functional Python Programming
docs.python.org
docs.python.org
The core of functional programming is about avoiding mutable states, not much about anonymous functions or passing functions as data.
To do proper functional programming in Python, there should be IMO:
- a way to enforce non-mutable variables/objects;
- non-mutable collections;
- proper support for recursion and tail-recursion optimization;
- a better syntax for anonymous functions than single statement lambdas.
Generators are nice, but do not have much to do with functional programming.
class Recurse(Exception):
def __init__(self, *args, **kwargs):
self.args = args
self.kwargs = kwargs
class Terminate(Exception):
def __init__(self, retval):
self.retval = retval
def tailrec(func):
def wrapper(*args, **kwargs):
while True:
try:
func(*args, **kwargs)
except Recurse as r:
args = r.args
kwargs = r.kwargs
except Terminate as t:
return t.retval
return wrapper
@tailrec
def fact(n, acc=1):
if n == 0:
raise Terminate(acc)
else:
raise Recurse(n - 1, acc * n)
Of course, it will be slow because it relies on exceptions :P def factorial(n):
res = fac(n)
while callable(res):
res = res()
return res
def fac(n, acc=1):
if n == 1:
return acc
else:
return lambda: fac(n-1, n*acc)> To do proper functional programming in Python, there should be IMO:
> ...
> - a better syntax for anonymous functions than single statement lambdas.
Integers, floats, tuples, named tuples, and frozensets are all immutable, functions are values, etc. E.g.: https://joypy.osdn.io/notebooks/Derivatives_of_Regular_Expre... -or- https://github.com/calroc/xerblin/blob/master/xerblin/btree....
It's not fantastic, but it's not that bad.
Does the namedtuple not suffice? Apologies if I'm being dense.
- - - -
It's not an immutable dict type (for one thing, this has linear lookup, the BTree would be better) but it's fun:
https://stackoverflow.com/questions/13708701/how-to-implemen...
In Python:
from functools import partial
def empty_dict(key):
raise KeyError
def _dict_add(dictionary, key, value, lookup):
return value if key == lookup else dictionary(lookup)
def dict_add(d, key, value):
return partial(_dict_add, d, key, value)
d = empty_dict
d = dict_add(d, 'key0', 23)
d = dict_add(d, 'key1', 18)
And then... >>> d('key0')
23
>>> d('key1')
18
>>> d('keyn')
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
d('keyn')
File "/usr/home/sforman/tmp_fn_dict.py", line 7, in _dict_add
return value if key == lookup else dictionary(lookup)
File "/usr/home/sforman/tmp_fn_dict.py", line 7, in _dict_add
return value if key == lookup else dictionary(lookup)
File "/usr/home/sforman/tmp_fn_dict.py", line 4, in empty_dict
raise KeyError
KeyError
- - - -> objects built with pydantic and type checked with mypy
FWIW, after ~15 years of professional Python development, I'm not into types [in Python]. My attitude is that if you really need them you should switch to e.g. OCaml or something that does them right. I love Python but I wouldn't use it anymore for anything other than scripts and maybe prototyping.
I strongly disagree. I personally think there is no programming problem where types aren't useful. When I write code, I think in terms of types, and rarely in terms of anything else. Python is useful in tons of problems OCaml isn't such as data science, statistics etc. Besides python has a very large ecosystem which matters in order not to reinvent the wheel.
I'd have to disagree with that just on first principles: no absolutes. (Note that that's an absolute.) :)
Anyway, I'm not trying to argue that types are not useful, they are. What I'm saying is that Python's loose approach to typing is useful "in the small" so to speak, but as projects grow larger you want "harder" types, checked by machine. Same thing with DB schema: No-SQL only makes sense in the small domains where the schema can be ignored or treated as an afterthought. Above a certain size and you grow a schema again and might as well do it right.
In Python type hints are just that: hints.
mypy is a very poor static analyzer, and a lot of Python code (especially in frameworks like Django) is inherently dynamic and cannot be type-checked. And that's ok.
Though, documenting your API with type-hints really helps, especially with VSCode (via pyright) popovers when you hover a symbol/function.
I can't keep up, does this mean scheme is not a functional language anymore by modern definition?
I disagree since any paradigm can be done either with or without mutable state. So it is not something that defines functional programming.
In any case you can also use immutable data types with python anyway.
The main annoyance with python is in my opinion your last point: The lambda syntax is too limited. So it is necessary to define lots of nested small functions instead.
Also python syntax is not as nice in chaining function calls over multiple lines.
Not sure if this is exactly what you meant, but I implemented something like this in reply to a similar comment a few months ago which may be of interest.
It's about functions being first class values, nothing more.
Lisp and Scheme are examples of functional languages that do not restrict mutability.
The term for what you are talking about is pure functional programming!
(You can have "quasi functional" if you mutate only local variables, such that the black box view of your functions appears pure. Nobody knows whether the map function in (map f list) internally contains a loop or tail recursion.)
Languages which support functional programming and other paradigms are not called functional; it is not correct to call Common Lisp and Scheme functional.
Not only do these have mutation, they have sequencing via strict evaluation: doing one thing that has a side effect, followed by another. Side effects like I/O and mutation are easily obtained and ordered, just like in Fortran or Java.
I'm not really sure where you are getting your information from but everything that I have read disagrees with this idea that functional programming is about purity.
https://en.wikipedia.org/wiki/Functional_programming
I think in recent times the term functional programming has been overloaded to mean what you describe, and many people use it that way, but I don't think it should be used that way.
Maybe we should just say "non pure functional" or "pure functional" so it's always clear what we are talking about!
Programming with procedures isn't functional programming; it is procedural programming. This is true even when the procedures are first-class procedures, and are used in declarative patterns like mapping, reducing and so on. There is no word which refers to programming with first-class procedures in a declarative style. It is different from "Fortran-like procedural", and is rooted in OOP. First class procedures are objects; and in fact in many languages you can take a solution based on first class procedures and replace those procedures with objects. Mutated lexical variables become member variables (or slots) and so on.
The rhetoric in this area conflates two meanings of "pure" and "purely", causing equivocation.
- "pure X" as in "consisting of nothing but X" and "purely X" meaning "solely X"
- "pure" as in having no side effects: a pure expression can be replaced by its value without a change in meaning.
A "not purely functional language" uses "purely" in the former sense. The language emphasizes functional features, but has procedural features also. It doesn't refer to a language which emphasizes declarative programming patterns around first-class procedures, though such a language can fit the description.
A "not purely functional program" likewise: it emphasizes functional programming but has procedural bits in there. It doesn't refer to the program being composed entirely of the declarative use of higher order procedures, like mapping over collections while doing I/O or mutation and such.
If we take the value of a variable and then filter it through functional stages, only to then assign the transformed value into the same variable by assignment, that is an example of "not purely functional". The data transformation is functional; the assignment isn't.
You can write:
(
range(10)
| Map(lambda x: x * 10)
| Filter(lambda x: x % 2 == 0)
| Reduce(lambda a, b: a + b)
)
instead of: x = range(10)
x = map(lambda x: x * 10, x)
x = filter(lambda x: x % 2 == 0, x)
x = reduce(lambda a, b: a + b, x)
and more. https://tandav.github.io/pipe21/ import pandas as pd
import functools
(
pd.Series(range(10))
.apply(lambda x: x * 10)
.where(lambda x: x % 2 == 0)
.pipe(lambda s: functools.reduce(lambda x, y : x + y, s))
)1. It does not require to wrap your iterable into some wrapper to use functional methods. It takes an iterable/object and returns another iterable/object. You don't have to unwrap it after transformations.
2. it uses oneliners (library is 80LOC single file) for most of the methods. You can just copy-paste it to use instead of install and import. E.g map, filter, reduce is just:
class B:
def __init__(self, f): self.f = f
class Pipe (B): __ror__ = lambda self, x: self.f(x)
class Map (B): __ror__ = lambda self, x: map (self.f, x)
class Filter(B): __ror__ = lambda self, x: filter(self.f, x)
class Reduce(B): __ror__ = lambda self, it: functools.reduce(self.f, it, *self.args)Much time has passed and I hope I'm a more aware and open-to-new-things person today :) I use Python these days though in a traditional procedural / OO way. This post reminded me that maybe I should look into writing it functional-style. Thankyou!
Learning a bit of clojure will really open your eyes to what functional programming means, and you can take some of those learnings back to python.
The great thing is that FP as an approach does translate very well to most imperative/OO contexts. It just isn’t necessarily very obvious how it will, until/unless you’ve been fully immersed and embraced it.
- mutable data structures
- no built-in function composition
- limited support for HOF
- no tail call optimization (AFAIK)
- performance in general isn't great and I imagine it's even worse when relying heavily on recursion, etc
Still, a fun project!In fact, I’d wager that any Python programmer worth their salt would suggest using a list comprehension and a lambda over a loop in most data transformation situations. Also currying comes in handy often once you have it in your toolkit.
It’s a bit silly to call two very mature libraries that are part of the Python standard library a ‘fun project’ just because the language itself supports multiple programming paradigms.
I always thought of LaTeX as being the prime example of declarative programming.
1) mutable programming shifts the idioms very very far away from passive destructuring, they like to deref pointers and reuse memory cells
2) it's also linked to parametric type systems I believe, which was missing until cpp/java5 in the mainstream (while FP had this since milner which was in the 70s)
If you want to smile, here is the presentation I made (warn: old memes ahead)
Practically? Yes, generators are often more memory-efficient, and thereby often more compute-efficient.
An optimizing compiler would need to know what actions are pure in order to rewrite code to be lazy. Python allows so much dynamism, I can only see that happening with a very sophisticated tracing JIT.
Generators allow one to easily transform code from non-buffered to buffered. Consider this example:
for y in x:
do(z)
Now, x may be a collection that was eagerly evaluated in previous steps, let's say a list, but then you've discovered that this list is too big to fit in memory, and you want to generate it in manageable fixed-size chunks, so you replace the code that created x to make x a generator. You don't have to touch the code above -- it will work the same way because generators have the same interface as collections.Another use: generators are used to implement async / await. I personally find this idea ridiculously stupid, but a lot of people (and especially those who don't understand what it does) like it a lot. Yielding mechanism, which is a feature of generators, is the one that's used to communicate / switch between co-routines (tasks) of asyncio.
Another aspect, besides bufferization is that you might want to delegate control over how much looping you want to do to a separate chunk of code. I.e. you may want to separate the generation of elements (hence, generator) from eg. filtering them, or transforming them in some way, or reducing them etc. If you didn't have this ability, you'd have to generate the entire collection upfront, and if your computation takes multiple steps that you'd like to separate into different code chunks, you'd have to also generate intermediate collections, even though, potentially, you don't need some of the elements in those collections. Consider, for example:
def powers(start, end, power):
return (x ** power for x in range(start, end))
def flt(a, b, c):
return a + b == c
def disprove_flt(upto, upto_power):
for power in range(upto_power):
for a, b, c in zip(powers(1, upto - 2, power), powers(2, upto - 1, power), powers(3, upto, power)):
if flt(a, b, c):
return a, b, c
return None, None, None
Which is, of course not a correct way to search for the counterexample to Fermat's last theorem, but I tried to find a popular enough subject so that the example was easier to follow.An exercise to the reader: rewrite the code above in such a way as to eliminate "upto" and "upto_power", i.e. to search until a counterexample is found (or indefinitely).
For example, Python's with statement could have been entirely programmed as a library function if lambas supported code blocks:
with(open("file.txt"), lambda f:
content = f.read()
return sum(int(c) for c in content.split(" "))
)
Functional languages usually have a pretty limited "core" and implement most of their features through their standard library, usually using higher-order functions, overloaded operators ... def _(f):
content = f.read()
return sum(int(c) for c in content.split(" "))
with(open("file.txt"), _) val = my_list.reduce(foobar)
What is even going on here?(A long time ago I tried to teach programming fundamentals to a maths person. She was completely puzzled by x=x+1. X equals to X+1? That was nonsense in their eyes.)
I don't think so.
> During Van Rossum's stay at CNRI, he launched the Computer Programming for Everybody (CP4E) initiative, intending to make programming more accessible to more people, with a basic "literacy" in programming languages, similar to the basic English literacy and mathematics skills required by most employers. Python served a central role in this
In python the C and even C++ heritage is quite clear, but it is in no way designed like Miranda and Haskell, which actually target to some extent expressing particular mathematical constructs in code, something which neither C, C++ not python ever aspired to.
Neither of these is true. I actually still find it a little weird that Python became so widely adopted by data science people, and that definitely doesn’t seem to be the main user GvR originally had in mind when creating the language.
But that's only checked by the type checker. At runtime you still need to do something like this to prevent subclassing:
class DontSubclassMe:
def __init_subclass__(self):
raise TypeError("Don't subclass me!")https://typing.readthedocs.io/en/latest/source/unreachable.h...
assert False, "This is unreachable."
in my code until now. Most programming languages are procedural:
Did the author count? -- The answer is a clear and resounding "No". The author pulled this factoid out if his rear end. Maybe. Maybe not. It's not even clear by what's meant by "all languages" -- all possible languages? all languages known to author? all languages used in Github? And why should anyone care about this kind of multitude? Lisp
Again, people who've never seen any Lisp, or vaguely remember their college days when some course requested from them to write a function to figure out if a string is a palindrome in Scheme think that Lisp is a single language. The author just decided to demonstrate his blistering ignorance by including something that he thought would render him as more experienced than their readers.Later the author perpetuates all sorts of absurd myths about "functional programming" s.a. increased modularity or ease of debugging. Apparently, author had never used step-debugger nor had he wrote anything in any popular programming language that advertises itself as functional to experience first-hand this "ease" he's talking about. Needless to say that nothing in functional programming prevents programmers from writing long functions... In practice, however, some languages which advertise themselves as "functional" have pathologically bad / hard to read syntax (eg. Haskell), and functions longer than some 10 lines or so become too difficult to understand even to people who believe themselves to be proficient in those languages.
Author is simply lying when he claims that generators are a kind of function, which can be simply verified:
>>> def generate_ints(n):
... for i in range(n):
... yield i
...
>>> type(generate_ints(1))
<class 'generator'>
>>> type(generate_ints(1)).mro()
[<class 'generator'>, <class 'object'>]
>>> isinstance(generate_ints(1), type(generate_ints))
False
Generators and functions are unrelated. Neither is a kind of other.On top of that, author frequently violates Python's coding conventions (eg. capital letters in variable names, assigning lambdas to variables).
---
But, bottom line: crappy language deserves no better documentation than this.