12.5.4 Implementing Query operations in C #
In Listing 12.14, when we discuss the sequence and parse the SelectMany operation, we already know that we can convert the C # query to a method call. We implement a query that only supports the end of a SELECT clause, ignoring those that are only useful for collections, such as grouping, so that the select extension method needs to be implemented.
As we said earlier, the second and subsequent from clause is converted to a call to the SelectMany method. When using query write calculations, we use the FROM clause in a way that is very similar to F # let! Structure, which represents nonstandard value binding, so it is often used, that is to say, we also need to implement SelectMany operations for Valuewrapper < ' t> types.
We already know that the SelectMany method is a bit more complicated than the bind function, because it has extra functions that need to be run before the result is returned. The Select method is simpler, but after we have seen the code, we will discuss that listing 12.20 implements two basic operations.
Listing 12.20 Implementing a query operation (C #)
Static Class Valuewrapperextensions {
public static valuewrapper<r>select<t, r>
(Thisvaluewrapper<t> source,
Func<t, r> selector) {
return newvaluewrapper<r> (selector (source. Value)); <--mapping values with a given function
}
public static valuewrapper<r>selectmany<t, V, r>
(Thisvaluewrapper<t> source,
Func<t, Valuewrapper<v>> Valueselector,
Func<t, V, r> resultselector) {
var newval =valueselector (source. Value); [1]
var resval = resultselector (source. Value,newval.value); [2]
Return newvaluewrapper<r> (Resval); [3]
}
}
Two methods are implemented as extension methods. That is, when processing the value of type Valuewrapper <T>, use standard point notation, which C # will be able to find during conversion from query syntax. The Select operation implements the mapping using the given function, so that only the value of the wrapper is accessed, the given function is run, and then the result is wrapped.
The SelectMany operation started a bit messy, but looking at the type of the parameter is still useful, and it can tell us what arguments can be passed to what function. Starting with the Bind member of F #, after extracting the first parameter value, call the function given by the second parameter value [1]; We must also combine the source value with the return value of the first function, in order to get the result, we use these two values as parameters, call the second function [2]; Package the result into a calculation type [3]] and return from the method.
In Listing 12.18, after the operation is implemented, the query expression can be compiled and run. In this section, we create a calculation type that does not add any additional functionality for the calculation. This is a simple, well-suited template for standard operations. Start with a template and see where you need to modify it to achieve more complex list types. Now, we put the theory into practice and implement similar operations for the option type.
12.5.4 Implementing Query operations in C #