Skip to content

Instantly share code, notes, and snippets.

@imgen
Last active September 9, 2019 03:16
Show Gist options
  • Save imgen/00a240946733de45bbc7c140817b0fc9 to your computer and use it in GitHub Desktop.
Save imgen/00a240946733de45bbc7c140817b0fc9 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
public static class EnumeratorExtensions
{
public static IEnumerable<T> TakeNextUntil<T>(this IEnumerator<T> enumerator, Predicate<T> predicate)
{
while(enumerator.MoveNext())
{
var item = enumerator.Current;
if (predicate(item))
break;
yield return item;
}
}
public static T TakeNextItem<T>(this IEnumerator<T> enumerator) =>
enumerator.MoveNext()? enumerator.Current : throw new Exception("Cannot retrieve next item");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment