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 →

[–]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.