Proposal: Enumerable.All() overloads for binary aggregation across a sequence #98327
Unanswered
ActivistInvestor
asked this question in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I have in many cases, needed to determine if a given sequence of items is ordered (ascending/descending), for not only testing purposes, but also for optimization purposes. While test frameworks include this functionality, it can be also be useful in runtime scenarios.
I've been using my own simple overload of Enumerable.All() for this purpose,.
It is essentially a conditional take on Enumerable.Aggregate(), that receives a sequence of items and a binary function that returns a bool. The binary function is applied to successive adjacent elements until it returns false. Hence, it can be used to determined if a sequence is ordered using a simple function that uses the > or >= operators to compare the arguments (or by using IComparable).
The following code should be self-explanatory. The issue I've wrangled with in taking a more-generic approach, is the behavior when less than 2 elements appear in the sequence. Insofar as determining if a sequence is ordered (ascending/descending), any sequence containing fewer than 2 elements is considered to be ordered (in any way).
Beta Was this translation helpful? Give feedback.
All reactions