Say I have elements (X, Y, and Z) in a list, I have a function, that generates a percentage, of how much two objects resemble each other.
What I want to do, is run X against Y and Z using my compareElements, so:
compareElements(X,Y); // equals 55
compareElements(X,Z); // equals 60
Then Y against X and Z
compareElements(Y,X); // equals 55
compareElements(Y,Z); // equals 62
Then Z against Y and X
compareElements(Z,X); // equals 60
compareElements(Z,Y); // equals 62
Then, I return the highest value, which is 62.
Obviously, there’s some repetition there, I don’t need the repetition, but I’m not sure how to eliminate it.
How do I structure my LINQ query, or a function/algorithm to do this comparison on every element, without the repetition?
I’d prefer to use LINQ if I can, as I’m being passed an enumerable and the function returns before the list is actually enumerated, so we can save the cost of performing the compare, until the list is enumerated.
All I need is that highest value, of the compare functions, 62.
Note: My actual result set I’m working with averages between 3 and 10 elements in the list, that need to be ran through this compare function.
For the sake of readability, I would write an iterator block to generate the comparisons in a non-repetitive manner:
Then you can do the following:
(That said I prefer Smelch’s suggestion for situations where there’s no practical reason to use LINQ or iterators, such as having a need for composable routines.)