Functional Python Programming
61–70 of 105 posts
Re: Functional Python Programming
#62Fun 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…
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.
Re: Functional Python Programming
#63Earlier quoted context omitted.
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)) )
Readability isn't the best. Also what you present here is method chaining and not functional pipes.
Re: Functional Python Programming
#64shameless 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
#65Earlier quoted context omitted.
Then what is the point?
Readability. If you're using the same name everywhere, then your code is really hard to understand for other devs: val = my_list.reduce(foobar) What is even going on here?
Re: Functional Python Programming
#66Sadly, 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…
Syntactically maybe, but I find it has a quite workable functional subset. 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.
Re: Functional Python Programming
#67Earlier quoted context omitted.
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)) )
Readability isn't the best. Also what you present here is method chaining and not functional pipes.
Re: Functional Python Programming
#68shameless 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
#69can you make anonymous functions with more than 1 expression now?
Re: Functional Python Programming
#70Sadly, 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…
Agreed, Python is designed for something other than functional programming, so a functional programming enthusiast would get better mileage out of something else. Even if you were to write all of your Python programs against the grain in a functional style, you'd still need to operate in a community of modules that don't provide referential transparency.