Thanks for posting this. Many years ago (2006?) I was a just-out-of-university programmer and a friend of a friend who wrote Python for Canonical sat me down and tried to teach me functional programming using Python. I was a poor learner: I didn't 'get it' (functional programming in general) and, to my regret, learned nothing. Much time has passed and I hope I'm a more aware and open-to-new-things person today :) I u…
Functional Python Programming
11–20 of 105 posts
Re: Functional Python Programming
#12Re: Functional Python Programming
#13Earlier quoted context omitted.
functional I can't say, but match was highly related to structural typing which was very much central in FP/LP culture
Think this is answer. It isn't technically needed to be called 'functional', but it is so integral to type systems that it seems to be used in all functional languages. Maybe we can call 'match' statements an emergent phenomena of functional languages.
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)
Re: Functional Python Programming
#14@dataclass is the new final
Re: Functional Python Programming
#15Thanks for posting this. Many years ago (2006?) I was a just-out-of-university programmer and a friend of a friend who wrote Python for Canonical sat me down and tried to teach me functional programming using Python. I was a poor learner: I didn't 'get it' (functional programming in general) and, to my regret, learned nothing. Much time has passed and I hope I'm a more aware and open-to-new-things person today :) I u…
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.
Re: Functional Python Programming
#16Sadly, Python is a pretty poor functional language. 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 an…
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 :PRe: Functional Python Programming
#17Re: Functional Python Programming
#18can you make anonymous functions with more than 1 expression now?
Re: Functional Python Programming
#19@dataclass is the new final
Does the type system let you express that a class shouldn't be subclassed? I remember this possibility was mentioned in a PEP and got deferred. It would be really useful with the new match/case pattern matching feature, because then you could have proper sum types, and mypy could enforce exhaustiveness. AFAIK you have to do a workaround with a "assert False" or similar at the end.
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!")Re: Functional Python Programming
#20Sadly, Python is a pretty poor functional language. 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 an…
I agree but it's not so dire, the tuple type (and variants like namedtuple) is pretty powerful, non-mutable and often my first choice for data structure in python. Everything else can be built on top of it if you're really motivated.