The `Maybe` container seems to be a close analog of `Option` in Rust https://doc.rust-lang.org/rust-by-example/std/option.html
Functional Programming in Python
31–40 of 64 posts
Re: Functional Programming in Python
#32writing `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
#33The examples in the README and this blog post just give me huge "nope" vibes. Obviously Python could learn a lot more from functional programming, but this is the wrong way to go about a lot of it.
0. https://sobolevn.me/2019/02/python-exceptions-considered-an-...
Re: Functional Programming in Python
#34If 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 whe…
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
#35Re: Functional Programming in Python
#36Python is all about the magic. Lambda expressions are a bit of a black box for me in Python, it would be nice if that was made more transparent, or perhaps a different function entirely
For many FP programmers, the lambdas are not nearly magic enough.
In Swift, you can do something like this :
reversedNames = names.sorted(by: { $0 > $1 } )
It's a closure expression, and is an anonymous block of code that will magically bind parameters passed to it to numbered variables.So you see the position Python is in: it has to balance a bit of magic for the power user, and yet not too much for the casual user.
It's a very delicate exercice, and it receives a lot of critics for it.
An F# or Lisp dev comming to Python will complain that it's not expressive enough.
A geographer comming to Python will struggle reading advanced code.
Yet we have to catters to all of them, give the huge Python popularity and it's goal to be "the second best language for everything".
I think Guido did a very decent job at it, although he gets a lot of heat for it. People don't like to hear "no" when they ask for a poney.
And the lambda expression is one of the most controversial decisions. Beginners have a hard time with it, but professional coders may snap when they hear it's limited to one line.
Re: Functional Programming in Python
#37The project cites this blog post[0] on the "anti-pattern" that is Python exceptions, and I honestly couldn't be turned off on this project anymore after reading it if this is the inspiration behind it. The examples in the README and this blog post just give me huge "nope" vibes. Obviously Python could learn a lot more from functional programming, but this is the wrong way to go about a lot of it. 0. https://sobolevn.…
Re: Functional Programming in Python
#38It overloads map() with a lambda function, to compose function pipelining.
Then, it introduces flow() as the new pipelining tool. But you have to use bind() on the last function call to return the value.
Interesting concepts, but unless Python incorporates this as a standard feature, then this will remain a fringe idea. And it will add significant load to the development and maintenance process of Python programs.
Admittedly, I like Elixir’s pipe forward concept |>
That makes it super simple to do functional composition, and it will automatically bind and return the last value.
Then getting the user profile, can be like so:
user_profile =
userid |> get_request |> parse_jsonRe: Functional Programming in Python
#39This is neat. It reminds me of an silly project [0] I made a while back to implement do-notation in python. In OP's project you still end up with code that's basically this: y = (Maybe(just=x) if x > 0 else Maybe()).bind(lambda a: Maybe(just=x*a) .bind(lambda b: Maybe.mreturn(a+b))) It's functionally sound and standard, but ergonomically painful. I built a really fun horrible hack to allow you to write that instead a…
Edit: Oh, nevermind - the "y" is how you access it after the with statement. Weird for python, but it makes sense given the rewritten version. I was thinking your version would have a non-rewritten "result = something" within the with block in a real use, but that's not what it's doing.
Re: Functional Programming in Python
#40The project cites this blog post[0] on the "anti-pattern" that is Python exceptions, and I honestly couldn't be turned off on this project anymore after reading it if this is the inspiration behind it. The examples in the README and this blog post just give me huge "nope" vibes. Obviously Python could learn a lot more from functional programming, but this is the wrong way to go about a lot of it. 0. https://sobolevn.…
It would be helpful if you could provide specific things in the blog post that you don't agree with. I skimmed through it and it seemed reasonable.
The answer is simple: let the `DivsionByZero` exception raise! You don't have to return anything!
Better yet, you should have input cleansing/data validation before it gets to that point. The alternative presented in the blog post is absurd over-engineering.