What's so useful about iterators and generators. The article says how to use them but not why. If you already know how to make list comprehensions and use "for elt in coll", do they let you do anything new?
Functional Python Programming
81–90 of 105 posts
Re: Functional Python Programming
#82Earlier 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…
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.
Re: Functional Python Programming
#83Earlier quoted context omitted.
Readability isn't the best. Also what you present here is method chaining and not functional pipes.
Is the difference mostly syntactic?
Re: Functional Python Programming
#84shameless plug: I maintain a small library to do functional pipes. 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/
Re: Functional Python Programming
#85Sadly, 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…
Re: Functional Python Programming
#86Sadly, 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 can't keep up, does this mean scheme is not a functional language anymore by modern definition?
Re: Functional Python Programming
#87Thanks 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…
IMHO broaden your boundaries a bit and learn a lisp or clojure-based functional style, like grab a getting started in clojure book and work through it. You will learn a lot more than this guide goes into, it's really just looking at functions in a functional style but doesn't spend nearly enough on data structures and state which are really the core of functional programming (and what python doesn't do well out of th…
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.
Re: Functional Python Programming
#88Sadly, 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 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.
Re: Functional Python Programming
#89Thanks 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…
I find that learning functional style in a language that does not enforce it, is extra difficult. If the language does not provide guard rails, then new learners will fall back on non-functional methods, and not even realize it.
Re: Functional Python Programming
#90Fun 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…
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’…