Live data from Hacker News

Functional Programming in Python

github.com

1–10 of 64 posts

Re: Functional Programming in Python

#6
I do hope Python keeps incorporating more of the "good stuff" from functional programming. List/dict/set comprehensions make code better - I can look at something and see very clearly that it's correct. Type hinting is a great compromise between beginner/quick scripting needs and offering a fully-baked type system. Type hints do a lot more than most people expect - you can create useful compound and custom types (like Tuple[Int, Str, Iterable]). If we can get some pattern matching in there I'm not sure what I'd do. Spend less time debugging test failures I guess. Not sure what I'd do with that time. Maybe see my family or learn how to bake pastry. Or finally clean the top of the stove. Or just write more Python in the same amount of time? I don't know. Pattern matching.

Cool library, though.

Re: Functional Programming in Python

#7
I like strongly typed and functional programming. But I would rather use a "pythonic" approach to solve problems in python. OFC there might be some cases where for example one of your dependency is python, and you still want to be able to write correct code. But in general, If you want types and functional constructs, IMO you should use something else.

Re: Functional Programming in Python

#8

If there was one thing I'd change with python to make it more 'functional' it would be having lambdas that weren't expression-only. I know it's not a functional feature because it's for stateful programming, but the lack of it disallows some nice stuff.

I agree with you that having more than a single expression in lambdas would be super nice. That being said, I don't think it's necessarily stateful to allow more than a single expression! If you don't reassign any variables, then it's equivalent to a single expression. For instance, something like this (using made up syntax for extended lambdas) isn't stateful:

    lambda i:
        square = i * i

        if i 
A decent heuristic for whether an algorithm is stateful might be to check if you can map it pretty easily to something like Haskell. In this case, it's not hard to do at all:

    \i ->
        let square  = i * i in
        if x 
Of course, it might be more natural for some programmers to write the Python code in a stateful way like this:

    lambda i:
        square = i * i

        if i 
Post reply on HN