Sadly, 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…
Functional Python Programming
41–50 of 105 posts
Re: Functional Python Programming
#42Re: Functional Python Programming
#43What 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…
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
#44Earlier quoted context omitted.
It's pretty common to define "higher-order" functions in functional programming. These will sometimes be applied to code blocks. 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…
But I can do all that with python already? Your example would work this way too: def _(f): content = f.read() return sum(int(c) for c in content.split(" ")) with(open("file.txt"), _)
Re: Functional Python Programming
#45Earlier quoted context omitted.
its in the list they provided of improvements python needs...
It's not a list of improvements python needs. It's a list of things python already does / has.
> To do proper functional programming in Python, there should be IMO:
> ...
> - a better syntax for anonymous functions than single statement lambdas.
Re: Functional Python Programming
#46Re: Functional Python Programming
#47What 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…
Re: Functional Python Programming
#48Re: Functional Python Programming
#49Re: Functional Python Programming
#50shameless 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/
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))
)