Fun to work on doing FP in languages that don't really support it, but in my view a language has to be built for FP for it to be a practical option in any real applications. Several obvious reasons for Python being a poor lang in which to do FP: - 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…
Scheme is one of the few FP languages with required TCO on the language standard, and alongside Lisp, Caml Light, Standard ML, OCaml, F#, Scala, supports mutable data structures.
Functional Python Programming
91–100 of 105 posts
Re: Functional Python Programming
#92Sadly, 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…
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!
Re: Functional Python Programming
#93Sadly, 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 do not think functional programming is about mutable state at all. 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.
Re: Functional Python Programming
#94Earlier quoted context omitted.
It doesn’t have to be all or nothing though. I’ve definitely seen (and written) functional Python in ‘real applications’ sprinkled here and there in an otherwise normal codebase. 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’…
No, it doesn't need to be all or nothing, but the scenario I was directing my point at was where people try to "do FP in lang X", not just sprinkling in functional concepts here are there. At this point, almost every language has some functional programming concepts in it, but I wasn't addressing that. I could've been clearer, but I've seen many cases where people would rather be programming in Haskell or some other…
Re: Functional Python Programming
#95Earlier quoted context omitted.
Is the difference mostly syntactic?
with dot chaining you are restricted to only the functions that the API you're working with provides. With functional pipes you can use whatever functions you want.
Re: Functional Python Programming
#96Earlier quoted context omitted.
I love both python and functional programming and I do write functional style python routinely. One barrier I hit is there is no immutable dict type. There is MappingProxyType but it's insufficient. I prefer using custom dataclass like objects built with pydantic and type checked with mypy. It's better than dict and can be made immutable but requires tons of boilerplate code which is a bit unpythonic.
> there is no immutable dict type 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 i…
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.
Re: Functional Python Programming
#97What blows my mind is that Python was designed for maths and data science by a maths and data science guy. It should be natural to work with immutable values and pipelines in Python, which are natural functional constructs. Yet everything in Python is mutable and pipeline-style programming is not really supported. ??? (A long time ago I tried to teach programming fundamentals to a maths person. She was completely puz…
> What blows my mind is that Python was designed for maths and data science by a maths and data science guy. 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.
Re: Functional Python Programming
#98Earlier quoted context omitted.
No, it doesn't need to be all or nothing, but the scenario I was directing my point at was where people try to "do FP in lang X", not just sprinkling in functional concepts here are there. At this point, almost every language has some functional programming concepts in it, but I wasn't addressing that. I could've been clearer, but I've seen many cases where people would rather be programming in Haskell or some other…
This I agree with completely! I’m a big believer of “when in Rome”. As in, try to be idiomatic in whatever language you need to use. I once saw a Python codebase where someone had tried very hard to write Java in Python.. That was equally horrible!
Re: Functional Python Programming
#99Sadly, 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…
> The core of functional programming is about avoiding mutable states, not much about anonymous functions or passing functions as data. 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 lambd…
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.
Re: Functional Python Programming
#100Earlier quoted context omitted.
> there is no immutable dict type 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 i…
> My attitude is that if you really need them you should switch to e.g. OCaml or something that does them right. 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 ecosy…
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.