C # language-higher-order functions

Source: Internet
Author: User

We all know that the function is the basic module in the program, the code snippet. What about the higher-order function? It sounds good to understand, that's the higher-order (level) version of the function. How does it get higher?  Let's take a look at its basic definition: 1: The function itself accepts one or more functions as input 2: The function itself can output a function. A function production function satisfies one of them and can be called a higher order function. High-order functions are widely used in functional programming. C # also began to use slowly after the introduction of lambda expressions in 3.0. Directory

1: Accept function

2: Output function

3:currying (in the Department of Localization)

An acceptance function

Custom is used for ease of understanding.

In code, takewhileself can accept a function, which can be called a higher order function.

 //Custom Delegate     Public DelegateTResult function<inchT outTresult>(T Arg); //Defining extension Methods     Public Static classextensionbyienumerable { Public StaticIenumerable<tsource> takewhileself<tsource> ( ThisIenumerable<tsource> Source, Function<tsource,BOOL>predicate) {            foreach(TSource IteratorVariable0inchsource) {                if(!predicate (ITERATORVARIABLE0)) {                     Break; }                yield returnIteratorVariable0; }        }    }    classProgram {//define a delegate        Static voidMain (string[] args) {List<int> myary =Newlist<int> {1,2,3,4,5,6,7,8,9,0 }; Function<int,BOOL> predicate = (num) = num <4;//Define a functionIEnumerable<int> q2 = myary.takewhileself (predicate);//            foreach(varIteminchQ2)            {Console.WriteLine (item); }            /** Output: * 1 * 2 * 3*/        }    }

Two output functions

The outputmehtod function in the code outputs a function for invocation.

   vart = Outputmehtod ();//output Function            BOOLresult = T (1); /** Output: * True*/  Staticfunction<int,BOOL>Outputmehtod () {Function<int,BOOL> predicate = (num) = num <4;//Define a function            returnpredicate; }

San Currying (in the Department of Localization)

A mathematical logic biologist (Haskell Curry), even the Haskell language, was named after him. Then the concept of currying is named according to the surname.

The above example is an example of a unary function f (x) =y.

How did that currying go on? Here is a fragment of the Garden Brothers.

Consider the following function: F (x, y, Z) = x/y +z. Requires the value f (4,2, 1).

First, replace the x in F (x, Y, Z) with 4 to get the new function g (Y, Z) = f (4, y, z) = 4/y + Z

Then, replace the parameter Y in g (Y, Z) with 2 to get H (z) = g (2, Z) = 4/2 + Z

Finally, replace the z in H (z) with 1 to get H (1) = g (2, 1) = f (4, 2, 1) = 4/2 + 1 = 3

Obviously, if it is an n-ary function evaluation, this substitution will occur n times, notice that each substitution here is in order, and this is not the same as we do in the number of hours to directly bring the 4,2,1 into X/y + Z.

In this sequential substitution process, each step takes into one parameter, each step has a new unary function, and finally a nested unary function chain is created.

Thus, by currying, we can simplify any multivariate function so that it can perform lambda calculus.

Examples of these currying in C # are:

var fun=currying (); Console.WriteLine (Fun (6) (2) (1));/** output:* 4*/static function<int, Function<int, Function<int, int> >> currying ()  {     return x = y = z = x/y + Z;}

  

  

Resources

Http://www.cnblogs.com/fox23/archive/2009/10/22/intro-to-Lambda-calculus-and-currying.html

C # language-higher-order functions

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.