Some applications in C # functional programming

Source: Internet
Author: User


What is functional programming

I believe that in the actual development, in many cases to complete a function requires the use of multiple classes, then our basic unit here is the class. And functional programming is more refined, so that we solve a function of the basic unit is a function, not a class, each function is composed of multiple functions, and there is no direct relationship between functions. If the simple text description is not enough for you to understand, here's a diagram to illustrate.

As shown, the left side of the graph is the three functions we have designed, and the right is the function we need to implement. And what we need to do is to use these three functions to complete the corresponding three functions, the author here is just a simple and image of the expression, the actual development process may require more functions, and need to use different functional programming combinations in order to complete the corresponding function.

After we assume that F1 and F2 are combined to complete the function G1, the result is as follows:

corresponding to the other functions we are still in accordance with the above method can be combined to complete the corresponding function, this must have its corresponding advantages, the greatest advantage to the author is that the function is not affected by the external environment, here we can not be compared with the method in the class, because the method will be affected by the class context variables, Especially in the case of multithreading will be shared read and write problems, and the function will not, because he just through the parameters of the way to receive external variables, there is a very strong reusability, if the early design of the full, in the late development process function can play the greatest role. Having said so much nonsense, we can begin the first part of our functional programming--some applications.


Some applications

You do not have to be frightened by this noun, he is mainly the function of our multiple parameters to split, split into a number of only one parameter function, such as the following function, we normally write the words are as follows:


The code is as follows:


Func<int, int, int> ADD = (x, y) = = x + y;

How to invoke believe the author does not need to introduce too much, below we will let him to support some applications:


The code is as follows:


Func<int, func<int, int>> Add = x = y = x + y;

This should be understood, just after receiving a value returned to the next function, and then we call the returned function to complete the call, we do not partially use this function? So called partial application. Let's take a look at how to use this function:


The code is as follows:


var Add2 = ADD (2);

var result = ADD2 (4);

This is easier to read in two lines, but we can use just one line, such as the following:


The code is as follows:


var result = Add (2) (5);

Wow, is not an instant feel tall on, if we this method of parameters more points, is the brackets parentheses, I believe that others see you this line of code will be hehe, and then heart 10,000 "so" horse Pentium.

I'm going to see the people who are going to brag that there's nothing too special about this, that's the function return function. Right is the function return function, but the actual use of you will find much more comfortable, below the author simply give a more reliable example to illustrate what some applications can bring us, for example, we often need to execute SQL statements, of course, we need to use SqlConnection, and then attach the corresponding SQL statement , so we can develop a simple function to simplify this process:


The code is as follows:


Func<sqlconnection, func<string, dataset>> execsql = x = y = =

{

using (x)

{

X.open ();

var com = X.createcommand ();

DataSet ds = new DataSet ();

Com.commandtext = y;

SqlDataAdapter adapter = new SqlDataAdapter (COM);

Adapter. Fill (DS);

return DS;

}

};

And then it's much simpler to call, we just pass it to the corresponding SqlConnection object, and then we can use the corresponding return value to execute our SQL statement, as shown in the following example:


The code is as follows:


var esql = Execsql (new SqlConnection ("xxx"));

var rds = esql ("Select xxxx from xxx");

RDS = ESQL ("Select FFFF from FFFF");

But to do this is not over, and in the face of people who always come up with strange problems, we have one more thing we need to do, that is, we may have to pass the SQL statement and then pass the corresponding SqlConnection object, no problem, we write a function specifically for this:


The code is as follows:


Func<string, func<sqlconnection, dataset>> execsqlt = x = y = Execsql (y) (x);

Let's go ahead and call it, but it's all written from the beginning with some application, and the actual situation may be a common way of writing, and it needs to be converted into some kind of application. So below we can manually write a few extensions for later use, first we write the existence of two parameters and the extension of the return value:


The code is as follows:


public static class functional

{

public static func<t1, Func<t2, t3>> currey<t1, T2, t3> (this func<t1, T2, t3> Func)

{

return x = y = func (x, y);

}

}

With this extension, we'll rewrite the example above:


The code is as follows:


var execsql = functional.currey<sqlconnection, String, Dataset> ((x, y) = =

{

using (x)

{

X.open ();

var com = X.createcommand ();

DataSet ds = new DataSet ();

Com.commandtext = y;

SqlDataAdapter adapter = new SqlDataAdapter (COM);

Adapter. Fill (DS);

return DS;

}

});

So we can write according to our normal form, then call functional's Currey, of course, here need to show the passing generic parameters, in some cases it is not required.

If you need to expand more parameters can be corresponding to write down. Of course, we can also extend the action to only the case where there are no parameters:


The code is as follows:


public static func<t1, action<t2>> currey<t1, t2> (this action<t1, t2> Func)

{

return x = y = func (x, y);

}

In this way we have solved the function of converting the normal function into partial application, but the problem is. If we start with a partial application, how do we convert it into a normal function? Naturally we also need the following extension to be able to convert it back:


The code is as follows:


public static func<t1, T2, t3> uncurrey<t1, T2, t3> (this func<t1, func<t2, t3>> Func)

{

return (x, y) = = func (x) (y);

}

In addition to the Declaration, Running GuestArticles are original, reproduced please link to the form of the address of this article
Some applications in C # functional programming

This address: http://www.paobuke.com/develop/c-develop/pbk23159.html






Related content the difference between = =, Equals, referenceequals in C # programming summary (VI) A detailed description of asynchronous programming C # Checking whether a foreach interpretation is null C # method of copying a Hashtable list or list of values to a one-dimensional array
Value types and reference types for C # predefined data types Introduction to C # methods for calculating age accurately analysis of differences between delegates and events in C # Defwndproc, WndProc and IMessageFilter in WinForm

Some applications in 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.