public static IReactiveSet <T> Except <T>(this IReactiveSet <T> first, IReactiveSet <T> second) { if (first == null) { throw new ArgumentNullException(nameof(first)); } if (second == null) { throw new ArgumentNullException(nameof(second)); } var obs1 = first.AsObservable().Select(change => new ReactiveSetChange <T>(change.ChangeReason, change.Items.Where(x => !second.Contains(x)))); var obs2 = second.AsObservable() .Select(change => { if (change.ChangeReason == ReactiveSetChangeReason.Add) { return(new ReactiveSetChange <T>(ReactiveSetChangeReason.Remove, change.Items)); } else { return(new ReactiveSetChange <T>(ReactiveSetChangeReason.Add, change.Items.Where(first.Contains))); } }); return(obs1.Merge(obs2).ToReactiveSet(x => first.Contains(x) && !second.Contains(x))); }
public static IReactiveSet <T> ObserveOnDispatcher <T>(this IReactiveSet <T> set) { if (set == null) { throw new ArgumentNullException(nameof(set)); } return(set.AsObservable().ObserveOnDispatcher().ToSet(set.Contains)); }
public static ICollectedReactiveSet <T> Collect <T>(this IReactiveSet <T> set) { if (set == null) { throw new ArgumentNullException(nameof(set)); } return(new ReactiveSet <T>(set.AsObservable())); }
public static IObservable <int> Count <T>(this IReactiveSet <T> set) { if (set == null) { throw new ArgumentNullException(nameof(set)); } return(set.Aggregate(0, (x, _) => x + 1, (x, _) => x - 1)); }
public static IObservable <int> Product(this IReactiveSet <int> set) { if (set == null) { throw new ArgumentNullException(nameof(set)); } return(set.Aggregate(1, (x, y) => x * y, (x, y) => x / y)); }
public static IObservable <T> ElementsAdded <T>(this IReactiveSet <T> set) { if (set == null) { throw new ArgumentNullException(nameof(set)); } return(set .AsObservable() .Where(x => x.ChangeReason == ReactiveSetChangeReason.Add) .SelectMany(x => x.Items)); }
private static IObservable <IImmutableSet <T> > ToImmutableSets <T>(this IReactiveSet <T> set, Func <IImmutableSet <T> > setFactory) { if (set == null) { throw new ArgumentNullException(nameof(set)); } if (setFactory == null) { throw new ArgumentNullException(nameof(setFactory)); } return(set.Aggregate(ImmutableHashSet <T> .Empty, (x, y) => x.Add(y), (x, y) => x.Remove(y))); }
public static IReactiveSet <T> SymmetricExcept <T>(this IReactiveSet <T> first, IReactiveSet <T> second) { if (first == null) { throw new ArgumentNullException(nameof(first)); } if (second == null) { throw new ArgumentNullException(nameof(second)); } return(first.Union(second).Except(first.Intersection(second))); }
public static ICollectedReactiveSet <TResult> Select <T, TResult>(this IReactiveSet <T> set, Func <T, TResult> filter) { if (set == null) { throw new ArgumentNullException(nameof(set)); } if (filter == null) { throw new ArgumentNullException(nameof(filter)); } return(new SetSelectImplementation <T, TResult>(set.AsObservable(), filter)); }
public static IObservable <TResult> Aggregate <T, TResult>(this IReactiveSet <T> set, TResult seed, Func <TResult, T, TResult> addFunc, Func <TResult, T, TResult> removeFunc) { if (set == null) { throw new ArgumentNullException(nameof(set)); } if (addFunc == null) { throw new ArgumentNullException(nameof(addFunc)); } if (removeFunc == null) { throw new ArgumentNullException(nameof(removeFunc)); } return(Observable.Create <TResult>(observer => { var current = seed; observer.OnNext(current); IDisposable sub = null; sub = set .AsObservable() .Subscribe( x => { try { if (x.ChangeReason == ReactiveSetChangeReason.Add) { current = x.Items.Aggregate(current, addFunc); } else { current = x.Items.Aggregate(current, removeFunc); } observer.OnNext(current); } catch (Exception ex) { observer.OnError(ex); sub?.Dispose(); return; } }, observer.OnError, observer.OnCompleted ); return sub; }) .DistinctUntilChanged()); }
public static IReactiveSet <T> Where <T>(this IReactiveSet <T> set, Func <T, bool> filter) { if (set == null) { throw new ArgumentNullException(nameof(set)); } if (filter == null) { throw new ArgumentNullException(nameof(filter)); } return(set .AsObservable() .Select(change => new ReactiveSetChange <T>(change.ChangeReason, change.Items.Where(filter))) .ToReactiveSet(x => filter(x) && set.Contains(x))); }
public static IReactiveSet <T> Intersection <T>(this IReactiveSet <T> first, IReactiveSet <T> second) { if (first == null) { throw new ArgumentNullException(nameof(first)); } if (second == null) { throw new ArgumentNullException(nameof(second)); } var obs1 = first.AsObservable().Select(change => new ReactiveSetChange <T>(change.ChangeReason, change.Items.Where(x => second.Contains(x)))); var obs2 = second.AsObservable().Select(change => new ReactiveSetChange <T>(change.ChangeReason, change.Items.Where(x => first.Contains(x)))); return(obs1.Merge(obs2).ToReactiveSet(x => first.Contains(x) && second.Contains(x))); }
public static IOrderedReactiveSet <T> OrderBy <T, TCompare>(this IReactiveSet <T> set, Func <T, TCompare> compareFunc) { int comparison(T first, T second) { int comp = Comparer <TCompare> .Default.Compare(compareFunc(first), compareFunc(second)); if (comp != 0) { return(comp); } comp = Comparer <T> .Default.Compare(first, second); if (comp != 0) { return(comp); } return(1); } return(new OrderedReactiveSet <T>(set.AsObservable(), Comparer <T> .Create(comparison))); }
public static T FirstOrDefault <T>(this IReactiveSet <T> set) { return(set.AsObservable().Select(x => x.Items.FirstOrDefault()).FirstAsync().Wait()); }
public static T[] ToArray <T>(this IReactiveSet <T> set) { return(set.AsObservable().FirstAsync().Select(x => x.Items.ToArray()).Wait()); }
public static IObservable <IImmutableSet <T> > ToImmutableSets <T>(this IReactiveSet <T> set) => set.ToImmutableSets(() => ImmutableHashSet <T> .Empty);
public static ICollectedReactiveSet <T> Buffer <T>(this IReactiveSet <T> set, TimeSpan time) { if (set == null) { throw new ArgumentNullException(nameof(set)); } var obs = Observable.Create <ReactiveSetChange <T> >(observer => { var isFirst = true; return(set.AsObservable() .Where(x => { if (isFirst) { isFirst = false; observer.OnNext(x); return false; } else { return true; } }) .Buffer(time) .Subscribe( x => { var added = new HashSet <T>(); var removed = new HashSet <T>(); foreach (var change in x) { if (change.ChangeReason == ReactiveSetChangeReason.Add) { foreach (var item in change.Items) { if (removed.Contains(item)) { removed.Remove(item); } else { added.Add(item); } } } else { foreach (var item in change.Items) { if (added.Contains(item)) { added.Remove(item); } else { removed.Add(item); } } } } if (added.Any()) { observer.OnNext(new ReactiveSetChange <T>(ReactiveSetChangeReason.Add, added)); } if (removed.Any()) { observer.OnNext(new ReactiveSetChange <T>(ReactiveSetChangeReason.Remove, removed)); } }, observer.OnError, observer.OnCompleted )); }); return(obs.ToReactiveSet()); }
public static IObservable <ImmutableSortedSet <T> > ToImmutableSortedSets <T>(this IReactiveSet <T> set) { return(set.Aggregate(ImmutableSortedSet <T> .Empty, (x, y) => x.Add(y), (x, y) => x.Remove(y))); }
public static T FirstOrDefault <T>(this IReactiveSet <T> set, Func <T, bool> predicate) { return(set.AsObservable().Select(x => x.Items.FirstOrDefault(predicate)).FirstAsync().Wait()); }