Rx Challenge #10 (Solution): DistinctUntilChanged with expiration period

2016/09/02

Rx Challenge #10 (Solution): DistinctUntilChanged with expiration period

The solution for the DistinctUntilChanged challenge is:

Code Snippet
  1. public static IObservable<TSource> DistinctUntilChanged<TSource, TKey, TPeriod>(
  2.     this IObservable<TSource> source,
  3.     Func<TSource, TKey> keySelector,
  4.     IEqualityComparer<TKey> comparer,
  5.     IObservable<TPeriod> distinctPeriod)
  6. {
  7.     var result = source.Publish(hot =>
  8.     {
  9.         var distinct = hot.TakeUntil(distinctPeriod)
  10.                           .DistinctUntilChanged(keySelector, comparer)
  11.                           .Repeat();
  12.         return distinct;
  13.     });
  14.     return result;
  15. }

 

The idea is to cancel the district stream after a period and resume the subscription (repeat).

 

You can use the following convenient overloads:

Code Snippet
  1. public static IObservable<TSource> DistinctUntilChanged<TSource>(
  2.     this IObservable<TSource> source,
  3.     TimeSpan distinctPeriod,
  4.     IScheduler scheduler = null)
  5. {
  6.     var result = DistinctUntilChanged(source, x => x, EqualityComparer<TSource>.Default, distinctPeriod, scheduler);
  7.     return result;
  8. }
  9.  
  10. public static IObservable<TSource> DistinctUntilChanged<TSource, TPeriod>(
  11.     this IObservable<TSource> source,
  12.     IObservable<TPeriod> distinctPeriod)
  13. {
  14.     var result = DistinctUntilChanged(source, x => x, EqualityComparer<TSource>.Default, distinctPeriod);
  15.     return result;
  16. }
  17.  
  18. public static IObservable<TSource> DistinctUntilChanged<TSource>(
  19.     this IObservable<TSource> source,
  20.     IEqualityComparer<TSource> comparer,
  21.     TimeSpan distinctPeriod,
  22.     IScheduler scheduler = null)
  23. {
  24.     var result = DistinctUntilChanged(source, x => x, comparer, distinctPeriod, scheduler);
  25.     return result;
  26. }
  27.  
  28. public static IObservable<TSource> DistinctUntilChanged<TSource, TPeriod>(
  29.     this IObservable<TSource> source,
  30.     IEqualityComparer<TSource> comparer,
  31.     IObservable<TPeriod> distinctPeriod)
  32. {
  33.     var result = DistinctUntilChanged(source, x => x, comparer, distinctPeriod);
  34.     return result;
  35. }
  36.  
  37. public static IObservable<TSource> DistinctUntilChanged<TSource, TKey>(
  38.     this IObservable<TSource> source,
  39.     Func<TSource, TKey> keySelector,
  40.     TimeSpan distinctPeriod,
  41.     IScheduler scheduler = null)
  42. {
  43.     var result = DistinctUntilChanged(source, keySelector, EqualityComparer<TKey>.Default, distinctPeriod, scheduler);
  44.     return result;
  45. }
  46.  
  47. public static IObservable<TSource> DistinctUntilChanged<TSource, TKey, TPeriod>(
  48.     this IObservable<TSource> source,
  49.     Func<TSource, TKey> keySelector,
  50.     IObservable<TPeriod> distinctPeriod)
  51. {
  52.     var result = DistinctUntilChanged(source, keySelector, EqualityComparer<TKey>.Default, distinctPeriod);
  53.     return result;
  54. }
  55.  
  56. public static IObservable<TSource> DistinctUntilChanged<TSource, TKey>(
  57.     this IObservable<TSource> source,
  58.     Func<TSource, TKey> keySelector,
  59.     IEqualityComparer<TKey> comparer,
  60.     TimeSpan distinctPeriod,
  61.     IScheduler scheduler = null)
  62. {
  63.     scheduler = scheduler ?? Scheduler.Default;
  64.     var period = Observable.Timer(distinctPeriod, scheduler);
  65.     var result = DistinctUntilChanged(source, keySelector, comparer, period);
  66.     return result;
  67. }

 

All challenges available here.

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*