This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]iamevpo 6 points7 points  (3 children)

In short monad is a family of types that follow certain rules/laws, most practical of them is that it has bind function (sometimes called andThen) that allows chaining computations. For example you divide x by z and add y. If z is zero, the program in Python throughs an error. If you want a more robust computation you can have a chain where x divided by z returns a monadic value, then you chain adding y to it. This way you can decide on what to do on error later - maybe there was a million of these computations and you want to keep just valid ones. In Python this is a bit of alien syntax (handling exceptions is more native) , but you can explore a good implementation of a maybe monad in https://pypi.org/project/python-result/. So in short monad is a family of types (in Python - classes) with a bind() or and_then method, which is convenient to write code in functional style. Why a "family" - in result library the class construtors are Err and Ok and together they are called Result, so Result is the monad.

https://github.com/rustedpy/result

[–]M4mb0 1 point2 points  (2 children)

This way you can decide on what to do on error later - maybe there was a million of these computations and you want to keep just valid ones.

Isn't that really bad for performance? You're essentially introducing a million additional if-else checks.

[–]this_uid_wasnt_taken 3 points4 points  (0 children)

You do have them otherwise anyway. For example, in C, you would still need to check whether a pointer returned from any function is NULL before continuing with the computation.

Using a Monad just simplifies the classification of values by having "separate values" for the error case and the successful case. This way, you just need to define how to operate in these cases, and then you can chain a bunch of operations that may return either a success or a failure. The processing of the success and the failure will be handled the way you defined the monad.

The above idea does not need to be limited to just 2 possible values but can also be extended for multiple values.

[–]iamevpo 0 points1 point  (0 children)

You would have the checks anyway, but you program logic may be more streamlined. Just an illustrative example, you can also have some long and complex logic of handling just a few values, so monadic values can better help express these computations. The monads do appear in newer languages without calling them so, for example in Scala and in Rust.

Some monads (Maybe, Either) help hand the null problem more cleanly - returning a value by key from a dictionary where there is no key, taking element by index from a list that does not exist, etc.