Monad is a very powerful concept, and before we introduce what Monad is and how it works, we should first confirm what the Monad can solve. Monad is a meta-solution for a variety of programming issues, and it is not a solution to a particular problem, and we will introduce monad in some examples.
Monad is a pattern that uses the amplified type combination function, Ampified type can be seen as a generic type with a generic parameter. Ienumerable<t> is a good example. Monad can eliminate ugly and repetitive code, simplifying the processing of many programming problems. It is a concept from the branch of the category theory in mathematics that was originally used in the Haskell language
Usually we manipulate values when programmed, and values can be simple types that are built into languages, such as int,bool,string, or complex types that we define ourselves, such as customer. These values are passed and processed in various ways. The ideal programming language allows us to express our purpose beautifully.
BOOL Isgreaterthanten (int value) { return;}
We can easily use this function:
if (Isgreaterthanten (x) {//dosomething)
But what if our function throws an exception?
BOOL Isafactoroftwelve (int value) {if(value==0throwNew Exception ("Can ' t divide by zero"); return (0;}
Now we have to wrap it with a boilerplate Try catch.
Try {if(Isafactoroftwelve (x)) {//dosomething}catch (Exception e) {// handle the Exception}
What if our function can return null?
string Trim (string value) {if(value==nullreturnnull ; return value. Trim ();}
So now we have to insert some boilerplate code to check if it's null.
var trimmedvalue=Trim (inputvalue)ifnull) {//so Something differenthere}
Every time we add boilerplate code, it blurs our code and makes our code difficult to read and more prone to bugs. Boilerplate code is not related to our code functions, it could be try-catch blocks, null checks, duplicates, or other code that makes us violate dry principles.
Here's a more enlightening example. How to deal with List or collection (if you do not use LINQ and related extension methods), any time you access the values in list and collection need to add a foreach loop, which is more boilerplate
foreach (var in Range (5,ten)) {//dosomethingwith the number}
Ienumberable<int> is a generic type, and we can also think of it as amplified type. It represents a combination of integers rather than a single integer. We can also use amplified type to represent something else. In the trim example above we can return a nullable<string> (assuming nullable can be used for reference types), we actually enhance (amplify) string as a type that can be both string and nullable. In C # This enhancement is implicit for reference types, which is better if you can support a reference type that returns Nullablle directly.
We can also imagine having the Isafactoftwelve function return a imightthrowanexception<bool>, which would make the code more explicit in its intent to return an enhanced bool, which could be either bool or an exception. Of course, C # 's functions have an implicit exception return type. It would be better if the function signature could tell us that it might return an exception.
Monad allows us to handle these amplified type, allowing us to remove boilerplate code. It can help us write declarative programs, solve the programmer's core problems, control complexity, and make our code clearer.
The focus of Monad