8.1.3 using a collection of functions in C #

Source: Internet
Author: User

8.1.3 using a collection of functions in C #

When considering a loan to a customer, we want to perform all tests and count the number of tests that return true (referring to high risk). If the number is 0 or one, the program recommends a loan. A common imperative solution is to declare a variable, use a foreach statement to enumerate the tests, perform tests in the loop body, and, if true, increase the variables; using the extension Method of LINQ Count, the implementation can be more elegant (listing 8.3).

Listing 8.3 Performing Tests (C #)

void TestClient (list<func<client,bool>> tests, client client) {

int issuescount = Tests.    Count (f=> F (client)); [1]

BOOL suitable = Issuescount <= 1; |

Console.WriteLine ("Client:{0}\noffer a loan: {1}", | output test results

Client. Name, suitable? "          YES ":" NO "); |

}

var john = new Client {<--uses the object initializer to create the client

Name = "JohnDoe", Income = 40000, Yearsinjob = 1,

Usescreditcard =true, Criminalrecord = False

};

TestClient (Gettests (), John); <--give this client a loan?

With functional terminology, count is a higher-order function whose parameters are conditional and the number of elements that the statistic condition is true. [1] We used it to count how many tests were considered to be unsuitable for loans to clients. In the example, the element in the collection is a function, so the parameter of the condition must be a function that returns a Boolean value. We write a lambda function that executes the function passed as a parameter value, specifying the customer as the parameter value, returning the result of the test as the result of the condition. After counting the number of failed tests, it is easy to calculate and output the results. Describing how it works (even in this relatively simple case) is complicated, but it's not hard to understand if you think you're trying to deal with each element.

As we mentioned earlier, the F # version of the example is essentially the same. This is because all the necessary features, such as higher-order functions, lambda functions, and the ability to save functions in a collection, are also available in C # 3.0. Now let's take a look at the F # code.

8.1.3 using a collection of functions in C #

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.