Skip to content

Instantly share code, notes, and snippets.

@GeorgeTsiokos
Created February 13, 2017 19:44
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save GeorgeTsiokos/a4985b812c4048c428a981468a965a86 to your computer and use it in GitHub Desktop.
/// <summary>
/// Returns the elements from the source observable sequence only after the predicate returns true (non-inclusive)
/// </summary>
/// <typeparam name="TSource">The type of the elements in the source sequence</typeparam>
/// <param name="source">Source sequence to propagate elements for</param>
/// <param name="predicate">A function to test each source element for a condition</param>
/// <returns>An observable sequence containing the elements of the source sequence starting from the point the predicate is true</returns>
[NotNull]
public static IObservable<TSource> SkipUntil<TSource>([NotNull] this IObservable<TSource> source, [NotNull] Func<TSource, bool> predicate)
{
if (source == null)
throw new ArgumentNullException(nameof(source));
if (predicate == null)
throw new ArgumentNullException(nameof(predicate));
return source.Publish(s => s.SkipUntil(s.Where(predicate)));
}
/// <summary>
/// Returns the elements from the source observable sequence until the predicate returns true (inclusive)
/// </summary>
/// <typeparam name="TSource">The type of the elements in the source sequence</typeparam>
/// <param name="source">Source sequence to propagate elements for</param>
/// <param name="predicate">A function to test each source element for a condition</param>
/// <returns>An observable sequence that contains elements from the input sequence until the condition is met</returns>
[NotNull]
public static IObservable<TSource> TakeUntil<TSource>([NotNull] this IObservable<TSource> source, [NotNull] Func<TSource, bool> predicate)
{
if (source == null)
throw new ArgumentNullException(nameof(source));
if (predicate == null)
throw new ArgumentNullException(nameof(predicate));
return source.Publish(s => s.TakeUntil(s.SkipUntil(predicate)));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment