/// <summary> /// Converts observable sequence to multi-map by creating a key/value pair from each element and storing it inside the structure /// </summary> /// <typeparam name="T">type of sequence element</typeparam> /// <typeparam name="TKey">type of key for multi-map</typeparam> /// <typeparam name="TValue">type of single element for multi-map</typeparam> /// <param name="obj">observable sequence</param> /// <param name="key">key creator</param> /// <param name="val">value creator</param> /// <returns></returns> public static Dictionary <TKey, HashSet <TValue> > ToMultiMap <T, TKey, TValue>(this IObservable <T> obj, Func <T, TKey> key, Func <T, TValue> val) { Dictionary <TKey, HashSet <TValue> > result = new Dictionary <TKey, HashSet <TValue> >(); obj.AutoDo(e => result.Add(key(e), val(e))); return(result); }
/// <summary> /// Converts IObservable<T> to IEnumerable<T>, using an intermediate storage buffer. You may not enumerate before /// the observable sequence has completed. /// </summary> /// <typeparam name="T">type of sequence element</typeparam> /// <param name="obj">observable sequence</param> /// <returns>enumerable sequence</returns> public static IEnumerable <T> ToBufferedEnumerable <T>(this IObservable <T> obj) { List <T> list = new List <T>(); GuardedEnumerable <T> result = new GuardedEnumerable <T>(list); obj.AutoDo( e => list.Add(e), () => result.Status = GuardedEnumerable <T> .EStatus.Completed, e => result.Status = GuardedEnumerable <T> .EStatus.Error); return(result); }