C + + standard bind function usage and C # simple implementation

Source: Internet
Author: User
Tags instance method

In the C + + standard library book, see the use of bind1st,bind2nd and bind, there is a sense of familiarity, carefully thinking about, is in F # mentioned in the curry. Here is the explanation of Wikipedia: In computer science, currying ( English:currying), also translated into the card or Gariha , is to transform a function that takes multiple arguments into a function that takes a single parameter (the first parameter of the original function), and returns a technique that takes the remaining parameters and returns the result of the new function.

Let's look at a simple example.

voidMultint& A,intb) {cout<<"A:"<< a <<"B:"<< b <<Endl; A+=b;}voidTest24 () {using namespacestd::p laceholders; Vector<int>list; inti =0; Generate_n (Back_inserter (list),Ten, [&i] (){        returni++;    }); For_each (List.begin (), List.end (), Bind (Mult, _1,Ten)); For_each (List.begin (), List.end (), Bind (Mult, -, _1)); Copy (List.begin (), List.end (), Ostream_iterator<int> (cout," "));}
bind usage

  

In this case, For_each finally takes a function of void fun (int p) To do the argument, and P is the data we traverse each time, and here we use the mult, with two parameters. In this we are going to use the curry, turn the mult into the first void fun (int p) Form, below we see how the corresponding function works.

Let's look at bind1st first, which is equivalent to doing the following. F (a), F (a) (b). Simply put, it is the process of turning a function with two parameters into a function with only one parameter. bind2nd, similar to F (A, a)->f (b) (a). Bind is used more broadly, without limiting the number, parameter order, and function type. The use of bind in the first for_each above is equivalent to bind2nd, and the second is equivalent to bind1st.

Let's look at a small example below:

voidtest23 () {using namespacestd::p laceholders; Auto Func= [](intXstringy) {        returnTo_string (x) +y;    }; Auto F= Bind (func,1, _1); Auto FS= Bind (func, _1,"xx"); Auto FSS= Bind (func,3,"XXX"); Auto Fsss=bind (func, _2, _1); cout<< F ("x") <<Endl; cout<< FS (2) <<Endl; cout<< FSS () <<Endl; cout<< Fsss ("xxxx",4) <<Endl;}
C + + bind

The output results are 1x,2xx,3xxx,4xxxx. In the case of two parameters, bind is a method of several simple recombination functions. For the sake of understanding and explanation, I directly write the corresponding wording in F #.

 Letfunc x y = x.tostring () + y Letf x = func1x LetFS x = func x"xx" LetFSS = Func3 "XXX" Letfsss x y = func y x[<entrypoint;] Letmain argv = Printfn"%s"(f"x") Printfn"%s"(FS2) Printfn"%s"(FSS) printfn"%s"(fsss"xxxx" 4) Ignore (System.Console.Read ())0 //returns an integer exit code
F # Bind

F # because it is itself the FP language, the corresponding in C + + also needs to invoke external functions, compared to its own internal support.

The following are the corresponding variable types:

Val func:x: String y:string, ' a
Val f:x:string-String
Val fs:x: ' A-string
Val fss:string = "3xxx"
Val fsss:x:string-y: ' A-string

In this, we materialize the generic a into an int type, good for illustration, Func (int,string), String. While F is a new function generated after the Func first parameter is materialized, FS is the second parameter materializing to generate a new function, where FSS skips over, and Fsss is the original (int,string)->string type function (string,int)- The type function of the >string.

If F # is difficult to understand, here is the C # version of the Bind method, just simple for two parameters of the function case, I hope this will help you understand.

     Public classBindhelper { Public StaticFUNC&LT;T1, t> bind<t1, T2, t> (Func<t1, T2, t>Fun , T2 T2) {            return(T11) =            {                returnFun (T11, T2);        }; }         Public StaticFunc<t2, t> bind<t1, T2, t> (Func<t1, T2, t>Fun , T1 t1) {            return(t22) =            {                returnFun (t1, t22);        }; }         Public StaticFunc<t> bind<t1, T2, t> (Func<t1, T2, t>Fun , T1 t1, T2 T2) {            return() =            {                returnFun (t1, T2);        }; }         Public StaticFunc<t2, T1, t> bind<t1, T2, t> (Func<t1, T2, t>Fun ) {            return(t22, T11) =            {                returnFun (T11, t22);        }; }        Static voidMain () {Func<int,string,string> func = (intXstringY) = = {returnX.tostring () +y;}; varF = Bind (Func,1); varFS = Bind (func,"xx"); varFSS = Bind (func,3,"XXX"); varFsss =bind (func); Console.WriteLine (F ("x")); Console.WriteLine (FS (2));            Console.WriteLine (FSS ()); Console.WriteLine (Fsss ("xxxx",4));        Console.read (); }    }
C # bind

This should be best understood, and the overloaded method of the corresponding bind is listed in C # How to implement it.

In the end, the float (* (*f) (float, float)) (float) is initialized or not, but the corresponding equivalent can be initialized correctly. It is also possible to look at the method and meaning of bind with the bind representation.

    //how to materialize.    float(* (*F) (float,float))(float); Auto FVV= function<function<float(float) > (float,float) >(f); Auto FV= [](floatFfloatd) {                return[](floatc) {returnC;    };        }; using namespacestd::p laceholders; FVV=FV; //f = FV;Auto x = Bind (Bind (FV, _1, _1) (4), _1) (6); Auto XXX= FV (3,4)(2.0f); Auto YYY= FVV (3,4)(2.0f); cout<< x << Endl;
View Code

Ps:stl just started to see, can only say that C + + templates and generics are too powerful, the corresponding template method can be written in a dynamic language (declaring this element, what can be done, and JavaScript), and compile, according to call the corresponding template method to get the correct instance method is equivalent to the running result. So many of the template invocation errors are pointed out during the compile phase.

  

C + + standard bind function usage and C # simple implementation

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.