Functional Programming in Python
41–50 of 64 posts
Re: Functional Programming in Python
#42I 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…
Re: Functional Programming in Python
#43And 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
#44Earlier 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.
Re: Functional Programming in Python
#45 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
#46If 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.
Re: Functional Programming in Python
#47I 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…
A sad day for this Erlang enthusiast.
Re: Functional Programming in Python
#48The `Maybe` container seems to be a close analog of `Option` in Rust https://doc.rust-lang.org/rust-by-example/std/option.html
Re: Functional Programming in Python
#49writing `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.
Re: Functional Programming in Python
#50Your 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?