哪里可以练习Lambda函数?

发布于 2024-07-07 16:11:57 字数 1560 浏览 6 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

要走就滚别墨迹 2024-07-14 16:11:57

LINQPad是学习LINQ的好工具

LINQPad is a good tool for learning LINQ

热情消退 2024-07-14 16:11:57

Lambda 就是这样一种东西,一旦你开始思考,你就会“明白”它。 如果您当前正在使用委托,则可以将其替换为 lambda。 还有 System.Action<...>System.Func<...>System.Predicate<...>< /code> 添加是很好的快捷方式。 一些显示语法的示例会有所帮助(警告:它们很无聊,但我想说明如何交换函数):

public static void Main()
{
    // ToString is shown below for clarification
    Func<int,string,string> intAndString = (x, y) => x.ToString() + y.ToString();
    Func<bool, float, string> boolAndFloat = (x, y) => x.ToString() + y.ToString();

    // with declared
    Combine(13, "dog", intAndString);
    Combine(true, 37.893f, boolAndFloat);

    // inline
    Combine("a string", " with another", (s1, s2) => s1 + s2);
    // and multiline - note inclusion of return
    Combine(new[] { 1, 2, 3 }, new[] { 6, 7, 8 },
        (arr1, arr2) =>
        {
            var ret = "";
            foreach (var i in arr1)
            {
                ret += i.ToString();
            }
            foreach (var n in arr2)
            {
                ret += n.ToString();
            }

            return ret;
        }
    );

    // addition
    PerformOperation(2, 2, (x, y) => 2 + 2);
    // sum, multi-line
    PerformOperation(new[] { 1, 2, 3 }, new[] { 12, 13, 14 },
        (arr1, arr2) =>
        {
            var ret = 0;
            foreach (var i in arr1)
                ret += i;
            foreach (var i in arr2)
                ret += i;
            return ret;
        }
    );

    Console.ReadLine();

}

public static void Combine<TOne, TTwo>(TOne one, TTwo two, Func<TOne, TTwo, string> vd)
{
    Console.WriteLine("Appended: " + vd(one, two));
}

public static void PerformOperation<T,TResult>(T one, T two, Func<T, T, TResult> func)
{
    Console.WriteLine("{0} operation {1} is {2}.", one, two, func(one,two));
}

最让我困惑的是快捷语法,例如当使用 System.Action 只执行不带参数的委托时你可以使用:

var a = new Action(() => Console.WriteLine("Yay!"));

或者你可以这样做:

Action a = () => Console.WriteLine("Yay");

当你有一个带有一个参数的 ActionFuncPredicate 时,你可以省略括号:

var f = new Func<int, bool>(anInt => anInt > 0);

or:

// note: no var here, explicit declaration
Func<int,bool> f = anInt => anInt > 0;

代替:

Func<int,bool> f = (anInt) => anInt > 0;

or 走向极端:

Func<int,bool> f = (anInt) =>
{
    return anInt > 0;
}

如上所示,单行 lambda 不需要 return 语句,但多行 Func lambda 需要。

我认为您会发现学习如何使用 lambda 的最佳方法是大量使用集合,并将 System.Linq 包含在您的 using 命名空间中 - 您将看到大量针对集合的扩展方法,其中大多数方法都允许您锻炼您的 lambda 知识。

Lambdas are just something that, once you get your head around, you "get" it. If you're using a delegate currently, you can replace it with a lambda. Also the System.Action<...>, System.Func<...>, and System.Predicate<...> additions are nice shortcuts. Some examples showing syntax would be helpful though (warning: they are inane but I wanted to illustrate how you can swap functions):

public static void Main()
{
    // ToString is shown below for clarification
    Func<int,string,string> intAndString = (x, y) => x.ToString() + y.ToString();
    Func<bool, float, string> boolAndFloat = (x, y) => x.ToString() + y.ToString();

    // with declared
    Combine(13, "dog", intAndString);
    Combine(true, 37.893f, boolAndFloat);

    // inline
    Combine("a string", " with another", (s1, s2) => s1 + s2);
    // and multiline - note inclusion of return
    Combine(new[] { 1, 2, 3 }, new[] { 6, 7, 8 },
        (arr1, arr2) =>
        {
            var ret = "";
            foreach (var i in arr1)
            {
                ret += i.ToString();
            }
            foreach (var n in arr2)
            {
                ret += n.ToString();
            }

            return ret;
        }
    );

    // addition
    PerformOperation(2, 2, (x, y) => 2 + 2);
    // sum, multi-line
    PerformOperation(new[] { 1, 2, 3 }, new[] { 12, 13, 14 },
        (arr1, arr2) =>
        {
            var ret = 0;
            foreach (var i in arr1)
                ret += i;
            foreach (var i in arr2)
                ret += i;
            return ret;
        }
    );

    Console.ReadLine();

}

public static void Combine<TOne, TTwo>(TOne one, TTwo two, Func<TOne, TTwo, string> vd)
{
    Console.WriteLine("Appended: " + vd(one, two));
}

public static void PerformOperation<T,TResult>(T one, T two, Func<T, T, TResult> func)
{
    Console.WriteLine("{0} operation {1} is {2}.", one, two, func(one,two));
}

Mostly what confused me was the shortcut syntax, for example when using System.Action to just execute a delegate with no parameters you could use:

var a = new Action(() => Console.WriteLine("Yay!"));

Or you could do:

Action a = () => Console.WriteLine("Yay");

When you've got an Action, Func, or Predicate that takes one argument you can omit the parenthesis:

var f = new Func<int, bool>(anInt => anInt > 0);

or:

// note: no var here, explicit declaration
Func<int,bool> f = anInt => anInt > 0;

instead of:

Func<int,bool> f = (anInt) => anInt > 0;

or to go to the extreme:

Func<int,bool> f = (anInt) =>
{
    return anInt > 0;
}

As shown above, single line lambdas do not require the return statement, though multiline Func lambdas do.

I think you will find the best way to learn how to use lambdas is to work a lot with collections and include System.Linq in your using namespaces - you will see a ton of extension methods for your collections and most of these methods allow you to exercise your knowledge of lambdas.

跨年 2024-07-14 16:11:57

目前我发现的最好的就是这个链接。 这是一个让我们练习的测验,但我想要更多的 Lambda,而不是 LINQ。

The best that I have found is this link for the moment. It's a quiz that let met practice, but I would like something more Lambda and less LINQ.

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