C# lambda - curry usecases

后端 未结 6 1728
小蘑菇
小蘑菇 2021-01-31 12:04

I read This article and i found it interesting.

To sum it up for those who don\'t want to read the entire post. The author implements a higher order function named Curry

6条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-31 12:29

    One example: You have a function compare(criteria1, criteria2, option1, option2, left, right). But when you want to supply the function compare to some method with sorts a list, then compare() must only take two arguments, compare(left, right). With curry you then bind the criteria arguments as you need it for sorting this list, and then finally this highly configurable function presents to the sort algorithm as any other plain compare(left,right).

    Detail: .NET delegates employ implicit currying. Each non-static member function of a class has an implicit this reference, still, when you write delegates, you do not need to manually use some currying to bind this to the function. Instead C# cares for the syntactic sugar, automatically binds this, and returns a function which only requires the arguments left.

    In C++ boost::bind et al. are used for the same. And as always, in C++ everything is a little bit more explicit (for instance, if you want to pass a instance-member function as a callback, you need to explicitly bind this).

提交回复
热议问题