Table of Contents

Method Aggregate

Namespace
Gamelogic.Extensions.Algorithms
Assembly
Gamelogic.Extensions.dll

Aggregate<TSource, TResult>(IGenerator<TSource>, Func<TResult, TSource, TResult>, TResult)

Makes a generator that generates a running aggregate of the source generator.

public static IGenerator<TResult> Aggregate<TSource, TResult>(this IGenerator<TSource> source, Func<TResult, TSource, TResult> aggregator, TResult initialValue)

Parameters

source IGenerator<TSource>

The source generator.

aggregator Func<TResult, TSource, TResult>

The aggregator function. The two parameters, in order, is the aggregation so far, the next value of source. The result is the new aggregation.

initialValue TResult

The initial value.

Returns

IGenerator<TResult>

A new generator.

Type Parameters

TSource

The type of elements of the source generator.

TResult

The type of elements to generate.

Examples

The following will generate the maximum element generated by source so far:

var generator = source.Aggregate((x, y) = Max(x, y));

For example, if source generates 0 1 2 0 1 2..., then generator will generate 0 1 2 2 2 2...

Remarks

The resulting generator will generate the following sequence:

result0 = aggregator(initialValue, source[0])
result1 = aggregator(result0, source[1])
result2 = aggregator(result1, source[2])

Aggregate<TSource>(IGenerator<TSource>, Func<TSource, TSource, TSource>, TSource)

Makes a generator that generates a running aggregate of the source generator.

public static IGenerator<TSource> Aggregate<TSource>(this IGenerator<TSource> source, Func<TSource, TSource, TSource> aggregator, TSource initialValue)

Parameters

source IGenerator<TSource>

The source generator.

aggregator Func<TSource, TSource, TSource>

The aggregator function. The two parameters, in order, is the aggregation so far, the next value of source. The result is the new aggregation.

initialValue TSource

The initial value.

Returns

IGenerator<TSource>

A new generator.

Type Parameters

TSource

The type of elements of the source generator.

Remarks

The resulting generator will generate the following sequence:

result0 = aggregator(initialValue, source[0])
result1 = aggregator(result0, source[1])
result2 = aggregator(result1, source[2])

Aggregate<TSource>(IGenerator<TSource>, Func<TSource, TSource, TSource>)

Makes a generator that generates a running aggregate of the source generator.

public static IGenerator<TSource> Aggregate<TSource>(this IGenerator<TSource> source, Func<TSource, TSource, TSource> aggregator)

Parameters

source IGenerator<TSource>

The source generator.

aggregator Func<TSource, TSource, TSource>

The aggregator.

Returns

IGenerator<TSource>

A new generator.

Type Parameters

TSource

The type of elements of the source generator.

Remarks

This overload uses the first item as the first element to generate. The resulting generator will generate the following sequence:

result0 = source[0]
result1 = aggregator(result0, source[1])
result2 = aggregator(result1, source[2])

Exceptions

ArgumentNullException

generator is null.

ArgumentNullException

aggregator is null.