6.4.41 using option types in C #
Extension methods can write code that uses bindings and mappings in a smooth manner. Because the numbers in parentheses can be confusing, be aware that call Map is nested inside a lambda function as a parameter value for Bind:
Option<int> Readandadd () {
Returnreadinput (). Bind (n =
Readinput (). Map (m=> m + N));
}
In C #, the distinction between using higher-order functions and processing option types is more pronounced. C # does not directly support types such as differential unions, but the code becomes readable if the type can provide the appropriate handler function. Writing a functional program in C # is important to remember: while some of the underlying constructs may feel unnatural, because of the lambda function, you can write elegant function code in C # as well.
So far, we've discussed using higher-order functions to handle multivalued and optional values. In the previous chapter, we also talked about the last value is a function, and in the next section we will see that it is also very useful to handle the value of a function in higher-order functions.
6.4.41 using option types in C #