using System;
using System.Collections.Generic;
using System.Linq;
namespace Game.Extensions
{
///
/// Provides additional methods for the
/// interface.
///
public static class EnumerableExtensions
{
///
/// Simultaneously projects each element of a sequence and applies
/// the result of the previous projection.
///
///
/// The type of elements in the source sequence.
///
///
/// The type of elements in the result sequence.
///
///
/// The source sequence.
///
///
/// The seed value for the aggregation component. This value is
/// passed to the first call to .
///
///
/// The projection function. This function is supplied with a value
/// from the source sequence and the result of the projection on the
/// previous value in the source sequence.
///
///
/// The resulting sequence.
///
public static IEnumerable SelectAndAggregate(
this IEnumerable source,
TResult seed,
Func selector)
{
foreach (var element in source)
{
seed = selector(element, seed);
yield return seed;
}
}
///
/// Combines the results of three distinct sequences into a single
/// sequence.
///
///
/// The element type of the first sequence.
///
///
/// The element type of the second sequence.
///
///
/// The element type of the third sequence.
///
///
/// The element type of the resulting sequence.
///
///
/// The first source sequence.
///
///
/// The second source sequence.
///
///
/// The third source sequence.
///
///
/// Function that combines results from each source sequence into a
/// final result.
///
///
/// A sequence of combined values.
///
///
///
/// This function works identically to Enumerable.Zip except that it
/// combines three sequences instead of two.
///
///
/// We have defined this as an extension method for consistency with
/// the similar LINQ methods in the class.
/// However, since there is nothing special about the first sequence,
/// it is often more clear to call this as a regular function. For
/// example:
///
///
/// EnumerableExtensions.Zip(
/// sequence1,
/// sequence2,
/// sequence3,
/// (a, b, c) => GetResult (a, b, c));
///
///
public static IEnumerable Zip(
this IEnumerable first,
IEnumerable second,
IEnumerable third,
Func resultSelector)
{
using var enumerator1 = first.GetEnumerator();
using var enumerator2 = second.GetEnumerator();
using var enumerator3 = third.GetEnumerator();
while (enumerator1.MoveNext() && enumerator2.MoveNext() && enumerator3.MoveNext())
yield return resultSelector(enumerator1.Current, enumerator2.Current, enumerator3.Current);
}
}
}