需要帮助理解 lambda(柯里化)

发布于 2024-09-17 07:53:04 字数 589 浏览 3 评论 0原文

我正在阅读 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

无需解释 2024-09-24 07:53:48

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

慵挽 2024-09-24 07:53:41

变量 x 是未绑定变量。它表示调用 Bind2nd 返回的函数的参数。

几个小时的计划会对你有所帮助,但试试这个。

当您调用 Bind2nd 时,返回的结果是一个函数。该函数定义为

(x) => func (x, constant)

现在您已将上述内容分配给变量,比如说 lambda ,您可以通过 lambda 变量调用该函数

lambda(x);

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 as

(x) => func (x, constant)

Now that you have the above assigned to a variable, lets say lambda, you can call that function via the lambda variable

lambda(x);

The x defined in Bind2nd is just a variable that represents an argument to the function that will be returned to you.

我是男神闪亮亮 2024-09-24 07:53:34

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."

雪若未夕 2024-09-24 07:53:27

我们先简化一下代码:

Func<int, int> B(Func<int, int, int> f, int c)
{
    return x=>f(x, c);
}

这和:

class Locals
{
    public int c;
    public Func<int, int, int> f;
    public int Magic(int x) { return f(x, c); }
}
Func<int, int> B(Func<int, int, int> f, int c)
{
    Locals locals = new Locals();
    locals.f = f;
    locals.c = c;
    return locals.Magic;
}

现在清楚x指的是什么了吗? x 是函数“Magic”的参数。

现在你可以这样使用 B:

Func<int, int, int> adder = (a, b)=>a+b;
Func<int, int> addTen = B(adder, 10);
int thirty = addTen(20);

有意义吗?看看这里发生了什么?我们采用两个参数的函数并将其中一个参数“固定”为常数。因此它成为一个参数的函数。

第二个例子更进一步。再次简化以消除繁琐,以便您可以更轻松地理解它:

Func<int, Func<int, int>> B2(Func<int, int, int> f) 
{
    return y=>x=>f(x,y);
}

相同

class Locals3
{
    public int y;
    public int Magic3(int x)
    {
       return x + this.y;
    }
}
class Locals2
{
    public Func<int, int, int> f;
    public Func<int, int> Magic2(int y)
    {
        Locals3 locals = new Locals3;
        locals.y = y;
        return locals.Magic3;
    }
}

Func<int, Func<int, int>> B2(Func<int, int, int> f) 
{
    Locals2 locals = new Locals2();
    locals.f = f;   
    return locals.Magic2;
}

这与所以您说

Func<int, int, int> adder = (a, b)=>a+b;
Func<int, Func<int, int>> makeFixedAdder = B2(adder);
Func<int, int> add10 = makeFixedAdder(10);
int thirty = add10(20);

B 是参数固定器 。 B2为您制作一个参数修复器

然而,这不是 B2 的要点。 B2 的要点在于:

adder(20, 10);

给出与 B2 相同的结果

B2(adder)(20)(10)

将一个两个参数的函数转换为两个各一个参数的函数

有道理吗?

Let's first simplify the code:

Func<int, int> B(Func<int, int, int> f, int c)
{
    return x=>f(x, c);
}

This is just the same as:

class Locals
{
    public int c;
    public Func<int, int, int> f;
    public int Magic(int x) { return f(x, c); }
}
Func<int, int> B(Func<int, int, int> f, int c)
{
    Locals locals = new Locals();
    locals.f = f;
    locals.c = c;
    return locals.Magic;
}

Now is it clear what x refers to? x is the parameter to function "Magic".

Now you can use B like this:

Func<int, int, int> adder = (a, b)=>a+b;
Func<int, int> addTen = B(adder, 10);
int thirty = addTen(20);

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:

Func<int, Func<int, int>> B2(Func<int, int, int> f) 
{
    return y=>x=>f(x,y);
}

This is the same as

class Locals3
{
    public int y;
    public int Magic3(int x)
    {
       return x + this.y;
    }
}
class Locals2
{
    public Func<int, int, int> f;
    public Func<int, int> Magic2(int y)
    {
        Locals3 locals = new Locals3;
        locals.y = y;
        return locals.Magic3;
    }
}

Func<int, Func<int, int>> B2(Func<int, int, int> f) 
{
    Locals2 locals = new Locals2();
    locals.f = f;   
    return locals.Magic2;
}

So you say

Func<int, int, int> adder = (a, b)=>a+b;
Func<int, Func<int, int>> makeFixedAdder = B2(adder);
Func<int, int> add10 = makeFixedAdder(10);
int thirty = add10(20);

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:

adder(20, 10);

gives the same result as

B2(adder)(20)(10)

B2 turns one function of two parameters into two functions of one parameter each.

Make sense?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文