Lambda 表达式查找 3 个数字中最大的
背景:我正在尝试编写一个最短的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
类似以下的内容怎么样:
示例测试应用程序:
How about something along the lines of:
Sample test application:
构建一个数组并使用 LINQ 怎么样?可能比计算出具有多个输入的所有逻辑路径(未经测试)更容易扩展:
编辑:只需重新阅读问题,看起来您想仅使用一个 lambda 定义来递归
greatestNumber
lambda 。我认为您不能这样做,因为greatestNumber(x,y)
的签名与greatestNumber(x,y,z)
不同。您可能可以使用带有params
参数的普通函数做一些更通用的事情,但这不涉及 lambda。编辑2:正如@Anthony所说,创建一个数组可能有点矫枉过正,尽管它很短而且一行字。您可以稍微简化一下表达式 2:
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):
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, asgreatestNumber(x,y)
's signature is different togreatestNumber(x,y,z)
. You could probably do something more generalised with a normal function that takes aparams
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: