Live data from Hacker News

Functional Python Programming

docs.python.org

21–30 of 105 posts

Re: Functional Python Programming

#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/

Re: Functional Python Programming

#22
post #14

@dataclass is the new final

Does the type system let you express that a class shouldn't be subclassed? I remember this possibility was mentioned in a PEP and got deferred. It would be really useful with the new match/case pattern matching feature, because then you could have proper sum types, and mypy could enforce exhaustiveness. AFAIK you have to do a workaround with a "assert False" or similar at the end.

It's not what you were asking for (class that can't be subclassed), but `typing` has an `assert_never` to check exhaustiveness:

https://typing.readthedocs.io/en/latest/source/unreachable.h...

Re: Functional Python Programming

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

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.

Re: Functional Python Programming

#25
post #6

can you make anonymous functions with more than 1 expression now?

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

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 implement most of their features through their standard library, usually using higher-order functions, overloaded operators ...

Re: Functional Python Programming

#26
post #6

can you make anonymous functions with more than 1 expression now?

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.

Re: Functional Python Programming

#27
post #14

Earlier quoted context omitted.

Does the type system let you express that a class shouldn't be subclassed? I remember this possibility was mentioned in a PEP and got deferred. It would be really useful with the new match/case pattern matching feature, because then you could have proper sum types, and mypy could enforce exhaustiveness. AFAIK you have to do a workaround with a "assert False" or similar at the end.

It's not what you were asking for (class that can't be subclassed), but `typing` has an `assert_never` to check exhaustiveness: https://typing.readthedocs.io/en/latest/source/unreachable.h...

Thanks for this trick for exhaustiveness checking, I've just been putting

  assert False, "This is unreachable."
in my code until now.

Re: Functional Python Programming

#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 puzzled by x=x+1. X equals to X+1? That was nonsense in their eyes.)

Re: Functional Python Programming

#29
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 a function on other variable (a bit like the let function in ocaml).

I always thought of LaTeX as being the prime example of declarative programming.

Re: Functional Python Programming

#30

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?

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"), _)
Post reply on HN