需要帮助理解 lambda(柯里化)
我正在阅读 Accelerated C# 我不太理解下面的代码:
public static Func<TArg1, TResult> Bind2nd<TArg1, TArg2, TResult> (
this Func<TArg1, TArg2, TResult> func,
TArg2 constant )
{
return (x) => func( x, constant );
}
最后一行 x 指的是什么?还有另一个:
public static Func<TArg2, Func<TArg1, TResult>> Bind2nd<TArg1, TArg2, TResult>
( this Func<TArg1, TArg2, TResult> func )
{
return (y) => (x) => func( x, y );
}
我如何评价这个? <代码>(y) => (x)=> func( x, y ) 传递了什么......这确实令人困惑。
i am reading Accelerated C# i don't really understand the following code:
public static Func<TArg1, TResult> Bind2nd<TArg1, TArg2, TResult> (
this Func<TArg1, TArg2, TResult> func,
TArg2 constant )
{
return (x) => func( x, constant );
}
in the last line what is x referring to? and there's another:
public static Func<TArg2, Func<TArg1, TResult>> Bind2nd<TArg1, TArg2, TResult>
( this Func<TArg1, TArg2, TResult> func )
{
return (y) => (x) => func( x, y );
}
How do i evaluate this? (y) => (x) => func( x, y )
what is passed where ... it does confusing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
lambda 表达式是匿名方法的简写。与匿名方法一样,lambda 表达式被分配给委托类型。适用于匿名方法的所有条件也适用于 lambda 表达式。
=>称为 lambda 运算符,读作“goes to”。运算符的左侧指定以逗号分隔的输入参数,右侧指定表达式或语句块,称为 lambda 体。 (p1, p2, p3, …pN) =>表达式 如果只有一个参数,则可以跳过括号 p1 =>表达;
我在这里写了一篇解释 lambda 表达式的小博客 Lambda 表达式
A lambda expression is shorthand for anonymous method. Like anonymous method, lambda expression is assigned to delegate types. All conditions that apply for anonymous methods also apply to lambda expressions.
=> is called lambda operator, which is read as “goes to”. The left side of the operator specifies the input parameters separated by comma, and the right side specifies an expression or statement block which is called lambda body. (p1, p2, p3, …pN) => expression If you have only one parameter then you can skip the parenthesis p1 => expression;
I have written a small blog explaining lambda expression here Lambda Expression
变量 x 是未绑定变量。它表示调用
Bind2nd
返回的函数的参数。几个小时的计划会对你有所帮助,但试试这个。
当您调用 Bind2nd 时,返回的结果是一个函数。该函数定义为
现在您已将上述内容分配给变量,比如说 lambda ,您可以通过 lambda 变量调用该函数
xBind2nd
中定义的 code> 只是一个变量,代表将返回给您的函数的参数。The variable x is an unbound variable. That represents an argument to the returned function from calling
Bind2nd
.A few hours with Scheme would help you here, but try this.
When you call
Bind2nd
the returned result is a function. That function is defined asNow that you have the above assigned to a variable, lets say
lambda
, you can call that function via thelambda
variableThe
x
defined inBind2nd
is just a variable that represents an argument to the function that will be returned to you.x 是 lambda 的参数,其类型为 TArg1。
发音 => 会很有帮助。 as“映射到”,如“x 映射到一个新函数,其中 TArg2 类型的常量替换为原始函数委托 func”。
x is the parameter of the lambda, it is of type TArg1.
It can be helpful to pronounce the => as "maps to" as in "x maps to a new function with a constant of type TArg2 substituted into the original function delegate, func."
我们先简化一下代码:
这和:
现在清楚x指的是什么了吗? x 是函数“Magic”的参数。
现在你可以这样使用 B:
有意义吗?看看这里发生了什么?我们采用两个参数的函数并将其中一个参数“固定”为常数。因此它成为一个参数的函数。
第二个例子更进一步。再次简化以消除繁琐,以便您可以更轻松地理解它:
相同
这与所以您说
B 是参数固定器 。 B2为您制作一个参数修复器。
然而,这不是 B2 的要点。 B2 的要点在于:
给出与 B2 相同的结果
将一个两个参数的函数转换为两个各一个参数的函数。
有道理吗?
Let's first simplify the code:
This is just the same as:
Now is it clear what x refers to? x is the parameter to function "Magic".
Now you can use B like this:
Make sense? See what is happening here? We're taking a function of two parameters and "fixing" one of the parameters to a constant. So it becomes a function of one parameter.
The second example takes that one step further. Again, simplify to get rid of the cruft so that you can understand it more easily:
This is the same as
So you say
B is a parameter fixer. B2 makes a parameter fixer for you.
However, that's not the point of B2. The point of B2 is that:
gives the same result as
B2 turns one function of two parameters into two functions of one parameter each.
Make sense?