Live data from Hacker News

Functional Python Programming

docs.python.org

31–40 of 105 posts

Re: Functional Python Programming

#31
post #8

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…

Why do you consider anonymous functions so important? Regular functions can be used the same way?

Re: Functional Python Programming

#32
post #28

What 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…

> Python was designed for maths and data science

I don't think so.

> During Van Rossum's stay at CNRI, he launched the Computer Programming for Everybody (CP4E) initiative, intending to make programming more accessible to more people, with a basic "literacy" in programming languages, similar to the basic English literacy and mathematics skills required by most employers. Python served a central role in this

Re: Functional Python Programming

#33

In their definition of programming languages types, I'm not sure I see how SQL which they cateogrize as declarative really differs from FP, especially with lazy evaluation. Both are a succession of functions applied onto a previously declared variable : just think of a long SQL query with many ctes modifying the previous ones; each of them can be thought of as a variable which takes its value from the application of…

SQL does not support higher-order functions or lambdas (except possibly in some special dialect I'm not aware of)

Re: Functional Python Programming

#34

Earlier quoted context omitted.

Just curious, but from a pragmatic view, what is the advantage of using anonymous functions instead of named functions for any mildly complex code?

Readability and scoping are two big advantages. Anonymous functions are often short and sweet, but also highly specific to a single section of the code. It's not meant to be reused. Having to name that one-off use case and hoist it into a function elsewhere makes it harder to maintain and understand. With proper anonymous functions, you can just read through it in one go.

But you don't need to name it big or maintain it somewhere else? Just use a throwaway name and write it where you use it. I mean python even allows overriding functions with the same name in the same scope. You could literally go with using just the same name everywhere.

Re: Functional Python Programming

#35
post #28

What 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…

Mathematics is an extremely broad field, the mathematics of a numerical analyst and someone doing algebraic geometry are very different and a programming language for each would look quite different.

In python the C and even C++ heritage is quite clear, but it is in no way designed like Miranda and Haskell, which actually target to some extent expressing particular mathematical constructs in code, something which neither C, C++ not python ever aspired to.

Re: Functional Python Programming

#36

In their definition of programming languages types, I'm not sure I see how SQL which they cateogrize as declarative really differs from FP, especially with lazy evaluation. Both are a succession of functions applied onto a previously declared variable : just think of a long SQL query with many ctes modifying the previous ones; each of them can be thought of as a variable which takes its value from the application of…

SQL does not support higher-order functions or lambdas (except possibly in some special dialect I'm not aware of)

Right, good point. I just find the structure and the mindset to be similar.

Re: Functional Python Programming

#37

Earlier quoted context omitted.

Readability and scoping are two big advantages. Anonymous functions are often short and sweet, but also highly specific to a single section of the code. It's not meant to be reused. Having to name that one-off use case and hoist it into a function elsewhere makes it harder to maintain and understand. With proper anonymous functions, you can just read through it in one go.

But you don't need to name it big or maintain it somewhere else? Just use a throwaway name and write it where you use it. I mean python even allows overriding functions with the same name in the same scope. You could literally go with using just the same name everywhere.

True, but you're missing the point.

Re: Functional Python Programming

#38
post #31
post #8

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…

Why do you consider anonymous functions so important? Regular functions can be used the same way?

OP said anonymous functions are NOT as important as immutable data.

Re: Functional Python Programming

#39
post #37

Earlier quoted context omitted.

But you don't need to name it big or maintain it somewhere else? Just use a throwaway name and write it where you use it. I mean python even allows overriding functions with the same name in the same scope. You could literally go with using just the same name everywhere.

True, but you're missing the point.

Then what is the point?

Re: Functional Python Programming

#40
post #31

Earlier quoted context omitted.

Why do you consider anonymous functions so important? Regular functions can be used the same way?

OP said anonymous functions are NOT as important as immutable data.

its in the list they provided of improvements python needs...
Post reply on HN