Hacker News new | ask | show | jobs
by ed_blackburn 953 days ago
I'm six months into my Python journey. We aren't building a library, so everything runs on 3.11. Having spent most of my career in statically typed and sometimes functional languages, I've found the result package approach and pattern-matching suggestion work well. There's been a suggestion it's not very Pythonic, but I'm willing to continue using a result monad because the trade-off is one-sided; it comfortably pays for itself.
1 comments

What is a result monad?
It just lets you combine results in an intuitive way:

    Ok(2) + Ok(3) = Ok(5)

    Ok(2) + Err() = Err()

    Err() + Ok(3) = Err()
The result package in the article: https://github.com/rustedpy/result

There's a pretty decent explanation in the readme. If you're more interested in Monads, I am not sure I'd cover that in a HN comment very well, but I would encourage you to take a look.

It is linguistically related to the (plus, int) monoid; the fact that it conforms to some algebraic/categorical interface is irrelevant in most contexts and just a distraction.

(A “Result” is Rust-speak for a sum type which is either A or B, where (say) B is conventionally an error)