C# 中柯里化的优点是什么? (实现部分功能)
C# 中柯里化的优点是什么?
在柯里化函数上实现部分函数应用有什么好处?
What is the advantage of Currying in C#?
What is the advantage of achieving partial function application on a curried function?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您的问题是如何在 C# 中实现柯里化,这里有一个示例
柯里化可以用任何支持闭包(lambda)的语言来实现,并且对于部分函数应用程序很有用,例如在 UI 编程中,其中执行函数所需的所有输入未收到,因此传递了一个柯里化函数,并在其中捕获了已接收到的输入。
If your question was how to implement currying in C# , here is an example
Currying can be implemented in any language that supports closures(lambdas), and is useful for partial function application like in UI programming where all the input necessary for the execution of function isnt received, so a curried function is passed around with already received inputs captured in it.
来自 维基百科
From Wikipedia
我发现当我想重用代码时,部分函数应用程序(与柯里化相反)非常有用。
需要明确的是,由于柯里化和偏函数应用的定义似乎变得模糊,所以偏函数应用是指采用具有 N 个参数的函数,并将其转换为具有 N-1 个参数的函数。
特别是,在编写单元测试时它非常方便。由于我将编写数百个单元测试,因此我尝试尽可能重用测试代码。因此,我可能有一个通用的测试方法,它接受我想要测试的方法的委托,以及该方法的一些参数和预期结果。常见的测试方法将使用提供的参数执行被测方法,并将有多个断言将结果与预期结果进行比较。
当我想要测试一个方法,该方法的参数多于传递到通用测试方法的委托时,问题就出现了。我可以编写另一个与第一个相同的通用测试方法,除了采用具有不同签名的委托之外。然而,这似乎是在重复我自己。为了避免编写此类重复代码,我可以使用部分函数应用程序将采用两个参数的委托转换为采用单个参数的委托。现在我可以使用常用的测试方法来测试采用一个或两个参数的方法。
这是我用来修复传入委托的参数之一的辅助方法之一:
I've found partial function application, as opposed to currying, to be useful when I want to reuse code.
To be clear, since the definitions of currying and partial function application seem to get blurred, by partial function application I mean taking a function with N parameters, and converting it into a function with N-1 parameters.
In particular, it's been handy when writing unit tests. Since I'll be writing hundreds of unit tests I try to reuse test code wherever possible. So I may have a common test method that takes a delegate to a method I wish to test, plus some parameters to that method and an expected result. The common test method will execute the method under test with the supplied parameters, and will have several assertions comparing the result to the expected result.
The problem comes when I want to test a method that has more parameters than the delegate being passed into the common test method. I could write another common test method which is identical to the first one, apart from taking a delegate with a different signature. That seems like repeating myself, however. To avoid having to write such duplicate code, I can use partial function application to convert a delegate taking, say, two parameters, into a delegate taking a single parameter. Now I can use my common test method to test methods that take either one or two parameters.
Here's one of the helper methods I use to fix one of the arguments of the delegate that was passed in:
C# 中柯里化的优点是它允许 C# 开发人员以函数式编程风格进行开发。
想想 LINQ。 LINQ 查询允许您传入方法作为参数:
x.someVal == 1
作为函数进行计算,然后Where
在其自身的执行中使用返回值。这是大多数 .NET 3 开发人员都熟悉的示例,但很少有人意识到他们正在涉足函数编程。如果没有 Curry 能力,LINQ 就不可能实现。
...希望这能弥补我自作聪明的评论。
The advantage of Currying in C# is that it allows C# developers to develop in a Functional Programming style.
Think about LINQ. A LINQ query allows you to pass in a method as a parameter:
x.someVal == 1
gets evaluated as a function and thenWhere
uses the return value in its own execution.It's an example that most .NET 3 developers are familiar with, but few realize that they're dabbling in Function Programming. Without the ability to Curry, LINQ wouldn't be possible.
...hopefull that makes up for my smart-ass comment.
一个简单的柯里化将是
A simple Currying will be