using System; using System.Collections.Generic; using System.Linq; namespace InABox.WPF { public abstract class AbstractCalculatorFunction where TType : struct, IComparable { protected abstract TValue Calculate(TValue current, TValue next, TType type) ; public TValue Calculate(TValue[] value, TType type) { if (!value.Any()) return default; var result = value.First(); result = value.Skip(1).Aggregate(result, (current, next) => Calculate(current, next, type)); return result; } } public abstract class AbstractCalculator : AbstractMultiConverter where TFunction : AbstractCalculatorFunction, new() where TType : struct, IComparable { public TValue Default { get; private set; } public TType Type { get; set; } public TFunction Function { get; private set; } protected AbstractCalculator() { Default = default; Function = new TFunction(); } protected override TValue Convert(IEnumerable value, object parameter = null) { var enumerable = value as TValue[] ?? value?.ToArray() ?? new TValue[] { }; if (Function == null || !enumerable.Any()) return Default; var result = Function.Calculate(enumerable, Type); return result; } } }