Lambda 表达式查找 3 个数字中最大的

发布于 2024-11-01 02:26:06 字数 1080 浏览 0 评论 0原文

背景:我正在尝试编写一个最短的 lambda 表达式来查找 3 个数字中最大的一个。 当然,Ternery 运算符是有能力做到的。

Func<int, int, int, int> greatestNumber2 = (x, y, z) => (x > y) ? ((x > z) ? x : z) : ((y > z) ? y : z);

但我的目的是实现如下功能。

greatest (x, y, z) = if (greater (x, y)>z) then greater(x, y) else z;

我可以通过两种方式做到这一点。

            Func<int, int, int> greaterNumber = null;
            Func<int, int, int, int> greatestNumber = null;

            //Expression 1
            greaterNumber = (x, y) => x > y ? x : y;
            greatestNumber = (x, y, z) => greaterNumber(x, y) > z ? greaterNumber(x, y) : z;

            //Expression 2
            greatestNumber = (x, y, z) => {
                Func<int, int, int> greater = (i, j) => i > j ? i : j;
                return greater(x, y) > z ? greater(x, y) : z;
            };

在表达式 2 中,我能够以某种方式实现我想要的目标,即定义函数来查找同一表达式本身的两个数字中较大的一个。然而,它是一个 lambda 语句。

有没有办法编写一个单行 lambda 来定义和使用 max 本身表达式中的 2 个数字中的较大者。

Background: I am trying to write a shortest possible lambda expression to find the greatest of 3 numbers.
Of course ternery operator is capable of doing it.

Func<int, int, int, int> greatestNumber2 = (x, y, z) => (x > y) ? ((x > z) ? x : z) : ((y > z) ? y : z);

But my intention is to achieve a function such as the one below.

greatest (x, y, z) = if (greater (x, y)>z) then greater(x, y) else z;

I was able to do this in two ways.

            Func<int, int, int> greaterNumber = null;
            Func<int, int, int, int> greatestNumber = null;

            //Expression 1
            greaterNumber = (x, y) => x > y ? x : y;
            greatestNumber = (x, y, z) => greaterNumber(x, y) > z ? greaterNumber(x, y) : z;

            //Expression 2
            greatestNumber = (x, y, z) => {
                Func<int, int, int> greater = (i, j) => i > j ? i : j;
                return greater(x, y) > z ? greater(x, y) : z;
            };

In the Expression 2, I was able to somehow achieve the what I wanted i.e define the function to find the greater among two numbers from the same expression itself. However, it is a statment lambda.

Is there a way to write a single lined lambda to define and use the greater of 2 numbers from within the expression of greatest itself.

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

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

发布评论

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

评论(2

飘然心甜 2024-11-08 02:26:06

类似以下的内容怎么样:

Func<int, int, int, int> greatest = (x, y, z) => Math.Max(x, Math.Max(y, z));

示例测试应用程序:

using System;

class Program
{
    static void Main(string[] args)
    {
        Func<int, int, int, int> greatest = (x, y, z) => Math.Max(x, Math.Max(y, z));

        Console.WriteLine(greatest(1, 2, 3));
        Console.WriteLine(greatest(1, 3, 2));
        Console.WriteLine(greatest(2, 1, 3));
        Console.WriteLine(greatest(2, 3, 1));
        Console.WriteLine(greatest(3, 1, 2));
        Console.WriteLine(greatest(3, 2, 1));
    }
}

How about something along the lines of:

Func<int, int, int, int> greatest = (x, y, z) => Math.Max(x, Math.Max(y, z));

Sample test application:

using System;

class Program
{
    static void Main(string[] args)
    {
        Func<int, int, int, int> greatest = (x, y, z) => Math.Max(x, Math.Max(y, z));

        Console.WriteLine(greatest(1, 2, 3));
        Console.WriteLine(greatest(1, 3, 2));
        Console.WriteLine(greatest(2, 1, 3));
        Console.WriteLine(greatest(2, 3, 1));
        Console.WriteLine(greatest(3, 1, 2));
        Console.WriteLine(greatest(3, 2, 1));
    }
}
因为看清所以看轻 2024-11-08 02:26:06

构建一个数组并使用 LINQ 怎么样?可能比计算出具有多个输入的所有逻辑路径(未经测试)更容易扩展:

Func<int, int, int, int> f = (x, y, z) => new[] { x, y, z }.Max();

编辑:只需重新阅读问题,看起来您想仅使用一个 lambda 定义来递归 greatestNumber lambda 。我认为您不能这样做,因为 greatestNumber(x,y) 的签名与 greatestNumber(x,y,z) 不同。您可能可以使用带有 params 参数的普通函数做一些更通用的事情,但这不涉及 lambda。

编辑2:正如@Anthony所说,创建一个数组可能有点矫枉过正,尽管它很短而且一行字。您可以稍微简化一下表达式 2:

Func<int, int, int> greaterOf = (x, y) => x > y ? x : y;
Func<int, int, int, int> greaterOf = (x, y, z) => greaterOf(x, greaterOf(y, z));

How about building an array and using LINQ? Probably a bit easier to extend than working out all the logic paths with multiple inputs (untested):

Func<int, int, int, int> f = (x, y, z) => new[] { x, y, z }.Max();

EDIT: Just reread the question, it looks like you want to recurse the greatestNumber lambda, with just one lambda definition. I don't think you could do that, as greatestNumber(x,y)'s signature is different to greatestNumber(x,y,z). You could probably do something more generalised with a normal function that takes a params argument, but that doesn't involve lambdas.

EDIT 2: As @Anthony says, creating an array is probably overkill, although it is short and a one-liner. You could simplify your Expression 2 a bit:

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