Live data from Hacker News

Functional Programming in Python

github.com

41–50 of 64 posts

Re: Functional Programming in Python

#42

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 (lik…

Union[Duck, OtherDuck] is another nice trick you can use in case of ducks.

Re: Functional Programming in Python

#43
I'm a big fan of functional programming but this documentation is kind of funny, unintentionally. I realize there is no better way to do it in Python.

And that’s how your initial refactored code will look like:

  user: Optional[User]

  can_buy_stuff: Maybe[bool] = Maybe.from_value(user).map(  # type hint is not required
      lambda real_user: real_user.get_balance(),
  ).map(
      lambda balance: balance.credit_amount(),
  ).map(
      lambda balance_credit: balance_credit > 0,
  )
Much better, isn’t it?

Re: Functional Programming in Python

#44
post #8

Earlier quoted context omitted.

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

I'm sure there are cases where multiple lines would be nice, but I don't think this is one. This seems easier to read to me: lambda i: -i * i if i Also, the functional way of doing multiple lines in a lot of situations would typically be to compose smaller lambdas.

Yeah, this is a trivial example. I picked it specifically to show that multi-line lambdas don't have to be stateful, not as an example of a lambda that couldn't be written in one line.

Re: Functional Programming in Python

#45
> But, having null checks here and there makes your code unreadable.

    if user is not None:                                                                                                                                                                                                                                                                                                                                    
         balance = user.get_balance()                                                                                                                                                                                                                                                                                                                       
         if balance is not None:                                                                                                                                                                                                                                                                                                                            
             balance_credit = balance.credit_amount()                                                                                                                                                                                                                                                                                                       
             if balance_credit is not None and balance_credit > 0:                                                                                                                                                                                                                                                                                          
                 can_buy_stuff = True                                                                                                                                                                                                                                                                                                                       
    else:                                                                                                                                                                                                                                                                                                                                                   
        can_buy_stuff = False                                                                                                                                                                                                                                                                                                                               
                                                                                                                                                                                                                                                                                                                                                            
Actually I think that's very readable

    user: Optional[User]                                                                                                                                                                                                                                                                                                                                    
                                                                                                                                                                                                                                                                                                                                                            
    can_buy_stuff: Maybe[bool] = Maybe.from_value(user).map(  # type hint is not required                                                                                                                                                                                                                                                                   
        lambda real_user: real_user.get_balance(),                                                                                                                                                                                                                                                                                                          
    ).map(                                                                                                                                                                                                                                                                                                                                                  
        lambda balance: balance.credit_amount(),                                                                                                                                                                                                                                                                                                            
    ).map(                                                                                                                                                                                                                                                                                                                                                  
        lambda balance_credit: balance_credit > 0,                                                                                                                                                                                                                                                                                                          
    )                                                                                                                                                                                                                                                                                                                                                       
                                                                                                                                                                                                                                                                                                                                                            
    Much better, isn't it?                                                                                                                                                                                                                                                                                                                                  
                                                                                                                                                                                                                                                                                                                                                            
No?!? That's much worse.

Re: Functional Programming in Python

#46

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.

lambda use in Python is discouraged in general. If you need something beyond an expression, PEP-8 encourages you to use a def in that case instead.

The issue with that is the lack of lexical scope. Lambdas have lexical scoping, def functions do not. To “fake” lexical scope, you have to use the nonlocal or global statements, which are gross and self-defeating.

Re: Functional Programming in Python

#47

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 (lik…

Python 3 threw away pattern matching in function heads; used to be able to deconstruct tuples. I had just discovered that feature in Python 2 when I realized 2 was about to go EOL, and upon porting my code I discovered the change.

A sad day for this Erlang enthusiast.

Re: Functional Programming in Python

#49
post #14

writing `lambda` must get really old

I look at it as a reminder that there may be a more Pythonic way of doing whatever it is you are using all the lambdas for.

Unfortunately, the 'Pythonic' way is terrible. The fact that the functional way is even worse really says something about the language.

Re: Functional Programming in Python

#50
Stuff like this seems so... masturbatory.

Your writing something in python so speed is right out the window. Fine, I've written stuff in python too and it gets the job done. I like python. Why bother with functional programming just to stroke your CS wang with no tangible benefit/speedup for the end user? Security and Stability you say? That is literally a tacit admission that the advantage is in cloaking your own ineptitude at your job - writing stable, safe code.

This is why Lotus 1-2-3 on a 486 had a better, more responsive UX than your clever code in some abstract conceptual language build atop a raging dumpster fire with a modern PC that has multiple CPU cores and gigabytes of RAM.

When did CS become so... contemptible?

Post reply on HN