Linq Zip came to mind in order to map the items in pairs and then apply the predicate.
By skipping the first item in the list and then Zipping the original list against the skipped list will produce item pairs that can then be passed to the provided predicate.
static class MyClass {
public static IEnumerable<T> WherePrevious<T>(this IEnumerable<T> collection, Func<T, T, bool> predicate) {
if (collection == null) throw new ArgumentNullException(nameof(collection));
if (predicate == null) throw new ArgumentNullException(nameof(predicate));
var other = collection.Skip(1);
var blank = default(T);
return collection
.Zip(other, (previous, item) => predicate(previous, item) ? item : blank)
.Where(_ => !object.Equals(_, blank));
}
}
The default value of the item was used as a flag in order to filter out the pairs that did not satisfy the predicate.
The following test was used to demonstrate the example given in the original question.
[TestClass]
public class FilterTest {
[TestMethod]
public void WherePreviousTest() {
//Arrange
var numbers = new[] { 1, 5, 8, 7, 12, 8, 5 };
var expected = new[] { 5, 8, 12 };
//Act
var actual = numbers.WherePrevious((first, second) => second > first).ToArray();
//Assert
actual.ShouldAllBeEquivalentTo(expected);
}
}
The body of the created extension method could have been done on its own, but creating the extension method allowed for a cleaner approach.
While I would say it is easier to read IMO, technically you will be enumerating the list more than once, which makes this not as efficient as your one pass through.
That then lead me to rethink my approach. After reviewing the source code for Enumerable.Zip and realizing your approach had the right idea I refactored the code to use the enumerator of the collection.
public static IEnumerable<T> WherePrevious<T>(this IEnumerable<T> collection, Func<T, T, bool> predicate) {
if (collection == null) throw new ArgumentNullException(nameof(collection));
if (predicate == null) throw new ArgumentNullException(nameof(predicate));
return WherePreviousIterator(collection, predicate);
}
private static IEnumerable<T> WherePreviousIterator<T>(IEnumerable<T> collection, Func<T, T, bool> predicate) {
using (var e = collection.GetEnumerator()) {
if (e.MoveNext()) {
var previous = e.Current;
while (e.MoveNext()) {
var item = e.Current;
if (predicate(previous, item))
yield return item;
previous = item;
}
}
yield break;
}
}
Which actually performs a lot better than the previous suggestion using Zip when run against the same test above.