Skip to content

Instantly share code, notes, and snippets.

@he-dev
Created February 9, 2018 05:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save he-dev/bbbfcdbd31aea7994f809ded288dcbb6 to your computer and use it in GitHub Desktop.
Save he-dev/bbbfcdbd31aea7994f809ded288dcbb6 to your computer and use it in GitHub Desktop.
static class MyClass
{
public static IEnumerable<T> WherePrevious<T>(this IEnumerable<T> source, Func<T, T, bool> predicate)
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (predicate == null) throw new ArgumentNullException(nameof(predicate));
return
source
.Zip(source.Skip(1), (previous, current) => (previous, current))
.Where(zip => predicate(zip.previous, zip.current))
.Select(zip => zip.current);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment