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.
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
greatestNumberlambda, 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 aparamsargument, 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: