/// <summary> /// Averages the specified value selector. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="source">The source.</param> /// <param name="valueSelector">The value selector.</param> /// <param name="emptyValue">The empty value.</param> /// <returns></returns> public static IObservable <double> Avg <T>([NotNull] this IObservable <IAggregateChangeSet <T> > source, [NotNull] Func <T, long> valueSelector, long emptyValue = 0) { return(source.AvgCalc(valueSelector, emptyValue, (current, item) => new Avg <long>(current.Count + 1, current.Sum + item), (current, item) => new Avg <long>(current.Count - 1, current.Sum - item), values => values.Sum / (double)values.Count)); }
/// <summary> /// Continuous calculation of the average of the underlying data source. /// </summary> /// <typeparam name="T">The type to average.</typeparam> /// <param name="source">The source observable.</param> /// <param name="valueSelector">The function which returns the value.</param> /// <param name="emptyValue">The resulting average value when there is no data.</param> /// <returns> /// An observable of averages. /// </returns> public static IObservable <float> Avg <T>(this IObservable <IAggregateChangeSet <T> > source, Func <T, float> valueSelector, float emptyValue = 0) { return(source.AvgCalc(valueSelector, emptyValue, (current, item) => new Avg <float>(current.Count + 1, current.Sum + item), (current, item) => new Avg <float>(current.Count - 1, current.Sum - item), values => values.Sum / values.Count)); }