Live data from Hacker News

Functional Python Programming

docs.python.org

41–50 of 105 posts

Re: Functional Python Programming

#41
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…

You want tail call optimization, not just tail-recursion, i.e. mutually recursive functions.

Re: Functional Python Programming

#42
post #40

Earlier quoted context omitted.

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

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.

Re: Functional Python Programming

#43
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…

> 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

#44

Earlier 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"), _)

I don't think they were saying you can't do it in Python, just offering what is arguably a nicer syntax, but of course that is subjective. The anonymous function version lets you write things in the order they happen and also "binds" the operations together, ie, someone can't accidentally add a completely unrelated line between the `def` and the `with`. It ultimately doesn't matter, people just get used to the conventions of their preferred languages and we all trudge on.

Re: Functional Python Programming

#45
post #40

Earlier 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.

do I have bots responding to me? whats hard about comprehending this?

> 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

#46
post #40

Earlier quoted context omitted.

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

its in the list they provided of improvements python needs...

Oh... either that was added in an edit or I had a brain blip that caused me to skip the bulleted list. Apologies.

Re: Functional Python Programming

#47
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…

This is false. It was mostly designed as a tool for Unix hackers, with motivations that seem not too dissimilar from perl. It was the ease of use of the language that brought people in from the math/science community who usually weren't programmers. This has often historically led to terrible and non-idiomatic (but working) code, because if you have an idea in your head you can quickly hit the ground running.

Re: Functional Python Programming

#49
post #37

Earlier quoted context omitted.

True, but you're missing the point.

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

#50
post #21

shameless 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))
    )
Post reply on HN