Function-building functions of C # Functional programming

Source: Internet
Author: User

In object-oriented programming, if we need to reuse other classes, we can implement them through inheritance. In functional programming We can also use these functions in different ways. Today's tutorial will cover two ways, one of which is the combination, combining multiple functions into one function, and the other being some of the applications we've introduced before, and of course we'll talk about how to make it more advanced to meet our usage requirements.

Combination

As the name implies, the combination is passing the result of function A to function B. But we are not concerned with the result of function A, but most certainly do so:

1 var r1 = Funca (1); 2 var r2 = FUNCB (r1);

This is obviously not the way we want it to be, assuming we need to take advantage of such a function later on. The problem arises, so we need to use a combination to synthesize them into a new function, first we write two functions to combine:

1  Public Static intFunca (intx)2 {3      returnX +3;4 }5 6  Public Static intFUNCB (intx)7 {8     returnX +6;9}

If we do not use any of the automation functions, we can use this method to combine:

func<int,int> FUNCC = x = FUNCB (Funca (x));

However, we cannot use VAR here because the automatic inference type of C # cannot infer this type. So we have a new function FUNCC, we can try to execute this function to see the final result. We completed the combination manually, and we will write an automated function to do this:

1  Public Static FUNC<T1, t3> compose<t1, T2, t3> (func<t1, t2> func1, Func<t2, t3> func2)2  {3     return x = Func2 (func1 (x)); 4 }

We then use this function to implement the above functions:

var funcc = compose<intint int > (Funca, FUNCB);

But we find that we need to provide generic parameters, not relying on type inference. However, if Funca and FUNCB explicitly declared before, you do not need to provide generic parameters, such as writing Funca and FUNCB as follows:

func<intint2; Func<intint6;

In this way, the compose function is not required to provide generic parameters, by the way, in the other languages described below how to implement the same function, in F # through the FUNCB >> Funca to achieve, and in Haskell is used Funca. FUNCB, which is very simple to implement compared to C #. From the above example, we also found that the return type of function A must be the same as the parameter type of function B, and only the first function in the chain can have more than one parameter, and the other functions can only have a function. Of course, the last function of the function chain can be an action, that is, there can be no return value, the following author writes a three functions can be combined with the automation function:

1  Public Static FUNC<T1, t4> compose<t1, T2, T3, t4> (func<t1, t2> func1, Func<t2, t3> Func2, FUNC<T3, T4>
   
     func3)
    2
    {
    3      
    return x =
     func3 (Func2 (func1 (x) )); 
    4 }
   

Of course, we do not need to write in actual development, we can directly take advantage of the functions provided in Fcslib.

Advanced part of the application

The person who has learned the part application of functional programming must know that part of the application is to take a function that needs multiple parameters, to break into a function chain, each function chain needs only one parameter, if Funca need three parameters, then call this function after using partial application will need to use Funca (2) in the following way (3) (2), so the following content the author will not repeat the introduction of the content has been introduced, if the reader has not learned, you can go to the corresponding page to learn.

We know that in C # if the incoming part applies the parameters in this automation function as a method, type inference does not work, then we need to enter cumbersome type parameters, such as the following:

functional.curry<converter<int,int>,Ienumerable<int>,Ienumerable< int>> (functional.map<int,int>);

The reader will find that the type parameter occupies half, we also described how to solve this problem, so we can write a function that has been explicitly declared over the type to encapsulate the following map function:

 Public Static func<converter<intint;, ienumerable<int;, ienumerable<int >> mapdelegate<t1, t2>() {      return map<t1, t2>;}

This way we do not need to provide a type parameter when calling the Curry function:

Functional.curry (functional.mapdelegate<int,int> ());

At this point, we have solved the problem of type inference. While some applications are useful in real-world development, they are cumbersome in some cases, such as the function filter requires two algorithms, and the last parameter is data. In the actual use we will be two algorithms are assigned, and in the later use only will change the corresponding data, but in the adoption of some applications will appear troublesome, the following is the implementation of the filter function:

1          Public StaticIenumerable<r> filter<t,r> (func<t,r> map,func<t,BOOL> Compare, ienumerable<t>datas)2         {3             foreach(T Iteminchdatas)4             {5                 if(compare (item))6                 {7                     yield returnmap (item);8                 }9             }Ten}

The specific function is to determine whether the condition is met by the compare function, and then return the desired part through the map function. We can call this function in the following way:

1             foreach(intXinchfilter<int,int> (x = x, x = x <=Ten,New int[] {2,3,1,4,5,3, the }))2             {3 Console.WriteLine (x);4             }5Console.readkey ();

Before we take a partial application, we write out the delegate version of the function, so that we can infer by using the type:

1          Public Static bool;, Ienumerable<t>, ienumerable<r>> filterdelegate<t, r>()2         {3             return filter<t, r>; 4         }

Then we can easily use the Currey function to apply its parts, here I directly implemented a Currey function, and did not use the fcslib provided. Readers can refer to the following:

1          Public Static Func<t1,func<t2,func<t3,r>>> currey<t1,t2,t3,r> (func<t1,t2,t3,r> Func) 2         {3             return x = y = z = = func (x, y, z); 4         }

Finally we take a look at the actual use:

1             varf = Currey (filterdelegate<int,int>());2             foreach(intXinchF (x = x) (x = x <=Ten)(New int[] {2,3,1,4,5,3, the }))3             {4 Console.WriteLine (x);5             }6Console.readkey ();

Even though this is cumbersome, we need to do some more advanced parts of the application, and here we need another automation function to help us implement:

1          Public Static Func<t3,r> apply<t1, T2, T3, r> (Func<t1, Func<t2, Func<t3, r>>> func,T1 arg1,T2 arg2) /c4>2        {3             return x = = func (arg1) (ARG2) (x); 4         }

The function is to make the original part of the applied function into a receive two parameters and return a function that receives only one parameter, because the algorithm part does not change, but the data will change frequently. Below we show through a practical application:

1             varf = Apply (Currey (filterdelegate<int,int> ()), x = x, x = x <=Ten);2 3             foreach(intXinchFNew int[] {2,3,1,4,5,3, the }))4             {5 Console.WriteLine (x);6             }7             foreach(intXinchFNew int[] {1,2,3,4,5,6,7,8,9,Ten, One, A, - }))8             {9 Console.WriteLine (x);Ten             } OneConsole.readkey ();

After such a toss-up, we get the function we really need, and we determine the algorithm at the beginning. Then in the later use we can just pass the data.

Function-building functions of C # Functional programming

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.