Standard higher-order functions of C # Functional programming

Source: Internet
Author: User

What is a higher order function

We may not be familiar with this noun, but the expression of this noun is something that we often use. As long as the parameters of our function are able to receive functions, or functions can return functions, of course dynamically generated is included. So let's call this kind of function a high-order function. But today our headline is not a high-order function, but a standard high-order function, since the addition of this standard means that there is a set of standard functions in functional programming, which makes it easy for us to invoke each. Today we will introduce three standard functions, namely map, Filter, Fold.

Map

The purpose of this function is to convert each item in the list from type A to type B and form a new type. Let's look at how it's implemented in Fcslib:

Believe that a lot of people should be able to read this code, just loop the list data. The CONVERT function is then called, and finally a list is returned by yield. Let's demonstrate how to use this standard high-order function in a simple example:

We create a list by declaring an array of data and then extracting the IDs from them using the map function.

Filter

The name should be able to guess his meaning, that is, to filter the data. Believe that some people can write this standard higher-order function according to the specific implementation, of course, regardless of whether the reader has written, we have to see how fcslib is implemented:

Here we are just missing a generic parameter, corresponding to the first parameter is able to receive the type T return bool type function (predicate also. NET comes with, maybe we used to use Func and Action, and use func<t,bool> can also achieve the same effect. ), and the statement is mainly called after the loop predicate to determine whether the condition is satisfied, if the condition is returned.

Fold

This standard high-order function can be difficult because it is divided into two types: left-folding and right-folding. The parameters received are three, the starting value, the summation function, and the list. The approximate execution is the first call to our additive function when the starting value as a parameter, and the first value in the list as the second parameter to call our additive function, the summation function to calculate the result, and the result will be the next call to accumulate the function of the first parameter, and the second argument is the next item in the list And so on So with this higher-order function, we can calculate the result of each data in the list directly, and the implementation in Fcslib is as follows:

By looking at the source code we can also draw the previous execution process, at the first time the value of V is startvalue, and then the return value of the accmulator function is saved in V, so that the next execution Accmulator is passed in as the first parameter. Let's take a simple example to see how to use:

After the left folding, there is also a right folding, of course, according to the name can also be guessed. is to start the loop from the end of the list, so we can implement foldr like this:

It is also pointed out that the left folding and right folding is one of the most commonly used algorithms, and he can fully implement the map and filter as we have described earlier, as long as it is written in the following way:

Here we are using a feature of the fold function, because the additive function passes the last computed result to the next additive function (new list<r> () we pass it directly as the starting value), Finally, we return the list by returning X, so that the next time we call this additive function, x remains the same list. The corresponding filter is also implemented:

and the specific method of invocation and Map,filter is consistent, and there is no particular big difference.

About the map, filter, and Fold in LINQ

In fact, functional programming has slowly entered into our lives unconsciously, in fact, we often use in LINQ Select,where and aggregate is corresponding to the functional programming in the map, Filter and fold.

Standard higher-order functions of C # Functional programming

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.