BotSharp/BotSharp.Algorithm/Extensions/Reduce.cs

24 lines
770 B
C#
Raw Normal View History

2018-09-09 01:47:53 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BotSharp.Algorithm.Extensions
{
public static partial class IListExtensions
{
/// <summary>
/// equivalent reduce function in Python
/// https://docs.python.org/3/library/functools.html?highlight=reduce#functools.reduce
/// </summary>
/// <typeparam name="TAccumulate"></typeparam>
/// <param name="source"></param>
/// <param name="func"></param>
/// <returns></returns>
public static TAccumulate Reduce<TAccumulate>(this IList<TAccumulate> source, Func<TAccumulate, TAccumulate, TAccumulate> func)
{
return source.Skip(1).Aggregate(source[0], func);
}
}
}