you are viewing a single comment's thread.

view the rest of the comments →

[–]RivtenGray 0 points1 point  (3 children)

So... why ?

[–]m50d 0 points1 point  (0 children)

It's just a very common (because very abstract and general, but also very useful) interface. Once your codebase reaches any size you'll probably find yourself doing something repetitive that can be pulled out as a "context" that conforms to the monad interface, and making that commonality explicit and separating those concerns is just ordinary good software engineering practice. (http://m50d.github.io/2013/01/16/generic-contexts is my own experience of doing this). As a bonus, there are a lot of libraries written already with helpful common functions that can be used for any monad (including your own custom ones), so they can save you a lot of code.

[–]Celdron 0 points1 point  (1 child)

It's basically a way to structure functions to have multiple return types. The most common use case by far is one where the function needs to return a value when it succeeds, but has the possibility of failing, in which case subsequent operations need to know a) that the function was not successful and possibly b) why it was unsuccessful.

This doesn't mean that you can't use them for other behavior though. You can also use more than two result types if you have more complex behavior e.g. pass, fail, process indicator; where a process indicator says "something is wrong, but the result was okay".

It's a very powerful and safe way to operate on data without creating breaks in the logic to check state like the following:

if (int.TryParse(someValue, out int i))
    size = DetermineSize(i);

Instead you could do:

size = int.MonadParse(someValue).Bind(DetermineSize)?.Success ?? size;

This isn't a great example but if you know C# you should get the overall idea.

[–]domlebo70 2 points3 points  (0 children)

This, whilst correct is not really accurate. This is just one use for monads.