Table of Contents

Class Generator

Namespace
Gamelogic.Extensions.Algorithms
Assembly
Assembly-CSharp.dll

Contains static methods for creating generators, and extension methods to create generators from existing ones.

[Version(2, 0, 0)]
public static class Generator
Inheritance
Generator
Inherited Members

Remarks

A generator is a class that generates data on demand. In this implementation, generators implement the IGenerator interface, and through an extension method supports a Next method that returns an element every time it is called.

Generators are infinite.

Many methods make methods from existing generators. The existing generators are always cloned, so no derived generator will call the Next<TSource>(IGenerator<TSource>) or MoveNext() methods of an existing generator. Clones are always returned in the restarted state.

Although Generators have almost the same interface as IEnumerator, there are some differences. Generators are always in a "valid" state, so calls to Current will always return a valid result, there is no "before first" and "after last" states.

You can also implement your own generators. There are two ways to do this. The first is to simply use the static methods provided; they allow you to make new generators by manipulating existing ones.

The second way is appropriate for more complicated situations, and that is to define your own class that implements the IGenerator<TResult> interface.

Methods

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> generator, Func<TSource, TSource, TSource> aggregator)

Parameters

generator 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 = generator[0] result1 = aggregator(result0, generator[1]) result2 = aggregator(result1, generator[2])

Exceptions

ArgumentNullException

generator is null.

ArgumentNullException

aggregator is null.

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> generator, Func<TSource, TSource, TSource> aggregator, TSource initialValue)

Parameters

generator IGenerator<TSource>

The source generator.

aggregator Func<TSource, TSource, TSource>

The aggregator.

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, generator[0]) result1 = aggregator(result0, generator[1]) result2 = aggregator(result1, generator[2])

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> generator, Func<TResult, TSource, TResult> aggregator, TResult initialValue)

Parameters

generator IGenerator<TSource>

The source generator.

aggregator Func<TResult, TSource, TResult>

The aggregator function.

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, generator[0]) result1 = aggregator(result0, generator[1]) result2 = aggregator(result1, generator[2])

Apply<TSource>(IGenerator<TSource>, Action<TSource>)

Makes a generator that applies a function on the elements it generates.

public static IGenerator<TSource> Apply<TSource>(this IGenerator<TSource> generator, Action<TSource> action)

Parameters

generator IGenerator<TSource>

The source generator.

action Action<TSource>

The action to apply to generated elements.

Returns

IGenerator<TSource>

A new generator.

Type Parameters

TSource

The type of the elements to generate.

Examples

The following will generate a generator that generates 0 1 2 3 0 1 2 ... and print the values to the console.

var generator = Generator.Count(4).Apply((x) => {Debug.Log(x);});

The following will generate a monster, and sets its properties based on the player's current level:

var monsterGenerator = Generator
	.Constant(monsterTemplate)
	.Apply(m => m.SetProperties(GetPlayerLevel());

Exceptions

ArgumentNullException

generator is $(null).

ArgumentNullException

action is $(null).

Average(IGenerator<int>)

Makes a generator the will generate the average of elements generated by another generator.

public static IGenerator<int> Average(this IGenerator<int> generator)

Parameters

generator IGenerator<int>

The generator.

Returns

IGenerator<int>

A new generator.

Examples

The following generator will generate 4, 4, 4, 4, ....

var generator = Generator.Constant(4).Average()

Exceptions

ArgumentNullException

source is null

Average(IGenerator<float>)

Makes a generator the will generate the average of elements generated by another generator.

public static IGenerator<float> Average(this IGenerator<float> generator)

Parameters

generator IGenerator<float>

The generator.

Returns

IGenerator<float>

A new generator.

Examples

The following generator will generate 0, 0.5f, 0.33f, 0.5f, 0.4f, ...

var generator = Generator.Count(2).Average()

Exceptions

ArgumentNullException

source is null

Cast<TResult>(IGenerator)

Makes a generator that will generate elements by casting the elements of a source generator.

public static IGenerator<TResult> Cast<TResult>(this IGenerator generator)

Parameters

generator IGenerator

The source generator.

Returns

IGenerator<TResult>

A new generator.

Type Parameters

TResult

The type of elements of the source generator.

ChooseUniformRandom<TSource>(params IGenerator<TSource>[])

Chooses the from the given generators selected uniform randomly.

[Obsolete("Use an appropriate overload instead.")]
public static IGenerator<TSource> ChooseUniformRandom<TSource>(params IGenerator<TSource>[] list)

Parameters

list IGenerator<TSource>[]

Returns

IGenerator<TSource>

IGenerator<TSource>.

Type Parameters

TSource

ChooseUniformRandom<TSource>(IList<IGenerator<TSource>>)

Makes a generator that selects a random generator from a given element to generate an element from.

public static IGenerator<TSource> ChooseUniformRandom<TSource>(IList<IGenerator<TSource>> list)

Parameters

list IList<IGenerator<TSource>>

The list of generators to choose from.

Returns

IGenerator<TSource>

A new generator.

Type Parameters

TSource

The type of elements to generate.

Exceptions

ArgumentNullException

list is $(null).

ArgumentException

list is empty or has null elements.

ChooseUniformRandom<TSource>(IList<IGenerator<TSource>>, int)

Chooses the random.

public static IGenerator<TSource> ChooseUniformRandom<TSource>(IList<IGenerator<TSource>> list, int seed)

Parameters

list IList<IGenerator<TSource>>

The list.

seed int

The seed.

Returns

IGenerator<TSource>

IGenerator<TSource>.

Type Parameters

TSource

The type of the t source.

Exceptions

ArgumentNullException

list is $(null).

ArgumentException

list is empty or has null elements.

ChooseUniformRandom<TSource>(IList<TSource>)

Make a generator that randomly generates elements from a list.

public static IGenerator<TSource> ChooseUniformRandom<TSource>(IList<TSource> list)

Parameters

list IList<TSource>

The source list.

Returns

IGenerator<TSource>

A new generator.

Type Parameters

TSource

The type of elements to generate.

Exceptions

ArgumentNullException

list is null

ArgumentException

listis empty.

ChooseUniformRandom<TSource>(IList<TSource>, int)

Make a generator that randomly generates elements from a list. Can be seeded.

public static IGenerator<TSource> ChooseUniformRandom<TSource>(IList<TSource> list, int seed)

Parameters

list IList<TSource>

The source list.

seed int

The seed of the random number generator to use.

Returns

IGenerator<TSource>

A new generator.

Type Parameters

TSource

The type of elements to generate.

Exceptions

ArgumentNullException

list is null

ArgumentException

listis empty.

Choose<TSource>(IList<IGenerator<TSource>>, IGenerator<int>)

Makes a generator that uses an index generator to choose a generator to generate an element from.

public static IGenerator<TSource> Choose<TSource>(IList<IGenerator<TSource>> generators, IGenerator<int> indexGenerator)

Parameters

generators IList<IGenerator<TSource>>

The source generators.

indexGenerator IGenerator<int>

The index generator.

Returns

IGenerator<TSource>

A new generator.

Type Parameters

TSource

The type of element to generate.

Examples

The following generator will interleave the given generators:

//generators is a IEnumerable of generators
var generator = Generator.Choose(generators, Generator.Count(generators.Count());

Exceptions

ArgumentNullException

generators or indexGenerator

ArgumentException

Cannot contain null elements;generators

Choose<TSource>(IList<TSource>, IGenerator<int>)

Makes a generator that chooses elements from a list using an index generator.

public static IGenerator<TSource> Choose<TSource>(IList<TSource> source, IGenerator<int> indexGenerator)

Parameters

source IList<TSource>

The source list.

indexGenerator IGenerator<int>

The index generator.

Returns

IGenerator<TSource>

A new generator.

Type Parameters

TSource

The type of elements to generate.

Examples

The following will generate "cat" "dog" "cat" "dog":

var generator = Generator.Choose(new[] {"cat", "dog"}, Generator.Count(2));

Exceptions

ArgumentNullException

list or indexGenerator

ArgumentException

List must have at least one element;list

ClosedSawTooth(int)

Makes a generator that produces evenly spaced floats from 0 to 1, both limits included, and repeats the result.

public static IGenerator<float> ClosedSawTooth(int sampleCount)

Parameters

sampleCount int

The number of samples per cycle.

Returns

IGenerator<float>

A new generator.

Exceptions

ArgumentOutOfRangeException

sampleCountis not bigger than 1.

Combine<TSource, TResult>(IEnumerable<IGenerator<TSource>>, Func<IList<TSource>, TResult>)

Makes a generator that combines the elements of specified generators.

public static IGenerator<TResult> Combine<TSource, TResult>(IEnumerable<IGenerator<TSource>> generators, Func<IList<TSource>, TResult> resultSelector)

Parameters

generators IEnumerable<IGenerator<TSource>>

The generators.

resultSelector Func<IList<TSource>, TResult>

The result selector.

Returns

IGenerator<TResult>

A new generator.

Type Parameters

TSource

The type of elements of the source generators.

TResult

The type of elements this generator will generate.

Exceptions

ArgumentNullException

generators

ArgumentException

Cannot contain null elements;generators

Combine<T1, T2, TResult>(IGenerator<T1>, IGenerator<T2>, Func<T1, T2, TResult>)

Combines the specified generators by applying a result selector function to the elements of each generator.

public static IGenerator<TResult> Combine<T1, T2, TResult>(IGenerator<T1> generator1, IGenerator<T2> generator2, Func<T1, T2, TResult> resultSelector)

Parameters

generator1 IGenerator<T1>

The first generator.

generator2 IGenerator<T2>

The second generator.

resultSelector Func<T1, T2, TResult>

The result selector function.

Returns

IGenerator<TResult>

A new generator

Type Parameters

T1

The type of elements of the first generator.

T2

The type of elements of the second generator.

TResult

The type of the elements of the result generator.

Examples

The following will code will add the elements of two generators to form its elements.

var generator1 = Generator.Count(3); //generates 0 1 2 0 1 2 0 1 2...
var generator2 = Generator.Count(2); //generates 0 1 0 1 0 1 0 1 0...
var generator = Generator.Combine(generator1, generator2, (x, y) => x + y);
	//generates 0 2 2 1 1 3 0 2 2...</code></pre>

Exceptions

ArgumentNullException

generator1 or generator2 or resultSelector

ArgumentNullException

generator1 is null.

ArgumentNullException

generator2 is null.

ArgumentNullException

resultSelector is null.

Combine<T1, T2, T3, TResult>(IGenerator<T1>, IGenerator<T2>, IGenerator<T3>, Func<T1, T2, T3, TResult>)

Combines the specified generators by applying a result selector function to the elements of each generator.

public static IGenerator<TResult> Combine<T1, T2, T3, TResult>(IGenerator<T1> generator1, IGenerator<T2> generator2, IGenerator<T3> generator3, Func<T1, T2, T3, TResult> resultSelector)

Parameters

generator1 IGenerator<T1>

The first generator.

generator2 IGenerator<T2>

The second generator.

generator3 IGenerator<T3>

The third generator.

resultSelector Func<T1, T2, T3, TResult>

The result selector function.

Returns

IGenerator<TResult>

A new generator

Type Parameters

T1

The type of elements of the first generator.

T2

The type of elements of the second generator.

T3

The type of elements of the third generator.

TResult

The type of the elements of the result generator.

Exceptions

ArgumentNullException

generator1 is null.

ArgumentNullException

generator2 is null.

ArgumentNullException

generator3 is null.

ArgumentNullException

resultSelector is null.

Combine<T1, T2, T3, T4, TResult>(IGenerator<T1>, IGenerator<T2>, IGenerator<T3>, IGenerator<T4>, Func<T1, T2, T3, T4, TResult>)

Combines the specified generators by applying a result selector function to the elements of each generator.

public static IGenerator<TResult> Combine<T1, T2, T3, T4, TResult>(IGenerator<T1> generator1, IGenerator<T2> generator2, IGenerator<T3> generator3, IGenerator<T4> generator4, Func<T1, T2, T3, T4, TResult> resultSelector)

Parameters

generator1 IGenerator<T1>

The first generator.

generator2 IGenerator<T2>

The second generator.

generator3 IGenerator<T3>

The third generator.

generator4 IGenerator<T4>

The fourth generator.

resultSelector Func<T1, T2, T3, T4, TResult>

The result selector function.

Returns

IGenerator<TResult>

A new generator

Type Parameters

T1

The type of elements of the first generator.

T2

The type of elements of the second generator.

T3

The type of elements of the third generator.

T4

The type of elements of the fourth generator.

TResult

The type of the elements of the result generator.

Exceptions

ArgumentNullException

generator1 is null.

ArgumentNullException

generator2 is null.

ArgumentNullException

generator3 is null.

ArgumentNullException

generator4 is null.

ArgumentNullException

resultSelector is null.

Constant<TSource>(TSource)

Makes a generator that generates a the same item each time.

public static IGenerator<TSource> Constant<TSource>(TSource item)

Parameters

item TSource

The item to generate.

Returns

IGenerator<TSource>

Type Parameters

TSource

The type of the item to generate.

Count(int)

Makes a generator that generates consecutive integers starting from zero up to a limit, and repeats the cycle.

public static IGenerator<int> Count(int upperLimitExcluded)

Parameters

upperLimitExcluded int

The upper limit (excluded).

Returns

IGenerator<int>

A new generator

Exceptions

ArgumentOutOfRangeException

upperLimitExcluded is not positive.

Dither(IGenerator<float>, int, IEnumerable<float>)

Takes the source generator's output, and generate a dithered sequence of integers in the range 0 to levels - 1. Uses error diffusion.

public static IGenerator<int> Dither(this IGenerator<float> source, int levels, IEnumerable<float> errorFactors)

Parameters

source IGenerator<float>

The source.

levels int

The levels.

errorFactors IEnumerable<float>

The error factors.

Returns

IGenerator<int>

IGenerator<System.Int32>.

FrequencyRandomInt(IEnumerable<float>)

Generates random integers at relative frequencies provided.

public static IGenerator<int> FrequencyRandomInt(IEnumerable<float> frequencies)

Parameters

frequencies IEnumerable<float>

The relative frequencies of each integer. If the array of frequencies has three elements, then the integers produced will be 0, 1, 2 (provided all frequencies are positive). All frequencies must be non-negative, and at least one must be positive.

Returns

IGenerator<int>

A new generator.

FrequencyRandomInt(IEnumerable<float>, int)

Generates random integers at relative frequencies provided.

public static IGenerator<int> FrequencyRandomInt(IEnumerable<float> frequencies, int seed)

Parameters

frequencies IEnumerable<float>

The relative frequencies of each integer. If the array of frequencies has three elements, then the integers produced will be 0, 1, 2 (provided all frequencies are positive). All frequencies must be non-negative, and at least one must be positive.

seed int

A seed to use for the random number generator.

Returns

IGenerator<int>

A new generator.

FromFunc<TResult>(Func<TResult>)

Makes a new generator from a generator function.

public static IGenerator<TResult> FromFunc<TResult>(Func<TResult> generator)

Parameters

generator Func<TResult>

The generator function that will be called to generate new elements.

Returns

IGenerator<TResult>

Type Parameters

TResult

The type of elements to generate.

Exceptions

ArgumentNullException

generator

GaussianRandomFloat(float, float)

Makes a generator that generates floats with a Gaussian distribution.

public static IGenerator<float> GaussianRandomFloat(float mean, float standardDeviation)

Parameters

mean float

The mean of the distribution. Values generated will be centered around this point.

standardDeviation float

The standard deviation of the distribution. The bigger this value, the flatter the distribution curve will be.

Returns

IGenerator<float>

A new generator.

Group<TSource>(IGenerator<TSource>, IGenerator<int>)

Makes a generator that generates groups of items from the source generator.

public static IGenerator<IList<TSource>> Group<TSource>(this IGenerator<TSource> generator, IGenerator<int> groupSizeGenerator)

Parameters

generator IGenerator<TSource>

The source generator.

groupSizeGenerator IGenerator<int>

The generator used to determine the size of the groups to return.

Returns

IGenerator<IList<TSource>>

IGenerator<IEnumerable<TSource>>.

Type Parameters

TSource

The type of the source generator.

Exceptions

ArgumentNullException

generator

ArgumentOutOfRangeException

groupSizeGenerator;Argument must be positive.

Group<TSource>(IGenerator<TSource>, int)

Makes a generator that returns groups of elements from the source generator.

public static IGenerator<IList<TSource>> Group<TSource>(this IGenerator<TSource> generator, int groupSize)

Parameters

generator IGenerator<TSource>

The source generator.

groupSize int

The size of the groups.

Returns

IGenerator<IList<TSource>>

IGenerator<IEnumerable<TSource>>.

Type Parameters

TSource

The type of the source generator.

Remarks

For example, if the source generator generates 0, 1, 2, 3, ... and the group size is 2 then the result generator will return groups (0, 1), (2, 3), ...

Exceptions

ArgumentNullException

generator

ArgumentOutOfRangeException

groupSize;Argument must be positive.

Interleave<TSource>(IGenerator<TSource>, params IGenerator<TSource>[])

Makes a generator that interleaves the elements of the specified generators.

public static IGenerator<TSource> Interleave<TSource>(IGenerator<TSource> generator, params IGenerator<TSource>[] generators)

Parameters

generator IGenerator<TSource>

The first generator.

generators IGenerator<TSource>[]

The other generators.

Returns

IGenerator<TSource>

A new generator.

Type Parameters

TSource

The type of elements to generate.

Exceptions

ArgumentNullException

generator is $(null)

ArgumentException

Any of generators is null.

Interleave<TSource>(IList<IGenerator<TSource>>)

Makes a generator that interleaves the elements of the specified generators.

public static IGenerator<TSource> Interleave<TSource>(IList<IGenerator<TSource>> generators)

Parameters

generators IList<IGenerator<TSource>>

The source generators.

Returns

IGenerator<TSource>

A new generator.

Type Parameters

TSource

The type of elements to generate.

Exceptions

ArgumentNullException

generators is null.

ArgumentException

generators is empty.

ArgumentException

Any of generators is null.

InterpolateDither<TSource>(IGenerator<TSource>, IGenerator<int>)

Interpolates a sequence, but applies dithering.

public static IGenerator<TSource> InterpolateDither<TSource>(this IGenerator<TSource> generator, IGenerator<int> sampleCount)

Parameters

generator IGenerator<TSource>
sampleCount IGenerator<int>

Returns

IGenerator<TSource>

Type Parameters

TSource

Remarks

For example, if the sequence is a binary sequence 1 0 1..., the result is a dithered sequence, something like 1 1 1 1 0 1 1 0 1 0 1 0 0 1 0 0 0 0

Interpolate<TSource>(IGenerator<TSource>, IGenerator<int>, Func<TSource, TSource, float, TSource>)

Makes a generator that interpolates between values of a given generator.

public static IGenerator<TSource> Interpolate<TSource>(this IGenerator<TSource> generator, IGenerator<int> sampleCount, Func<TSource, TSource, float, TSource> interpolater)

Parameters

generator IGenerator<TSource>
sampleCount IGenerator<int>
interpolater Func<TSource, TSource, float, TSource>

Returns

IGenerator<TSource>

Type Parameters

TSource

Interpolate<TSource>(IGenerator<TSource>, int, Func<TSource, TSource, float, TSource>)

Makes a generator that interpolates between values of a given generator.

public static IGenerator<TSource> Interpolate<TSource>(this IGenerator<TSource> generator, int sampleCount, Func<TSource, TSource, float, TSource> interpolater)

Parameters

generator IGenerator<TSource>

The source generator.

sampleCount int

The sample count per cycle.

interpolater Func<TSource, TSource, float, TSource>

The interpolater function. This function takes three arguments: the first two are the left and right endpoints, and the third is a fraction between 0 and 1. For typical interpolation, the function must return the left endpoint if this fraction is 0, and the right endpoint if the fraction is 1.

Returns

IGenerator<TSource>

A new generator.

Type Parameters

TSource

The type of elements to generate.

Examples

var baseGenerator = Generator.Count(2); //Generates 0 1 0 1 0 1...
var interpolatedGenerator = 
	baseGenerator.Interpolate(2, (x, y, t) => x*(1 - t) + y*t); //Generates 0 0.5f 1 0.5f 0 0.5f....

Remarks

The expression (x, y, t) => x*(1 - t) + y*t) is standard linear interpolation.

Exceptions

ArgumentNullException

generator is $(null).

ArgumentOutOfRangeException

sampleCount not positive.

ArgumentNullException

interpolater is $(null).

Iterate<TSource>(IEnumerable<TSource>, Func<IList<TSource>, TSource>)

Makes a generator that uses an iterator function to generate elements.

public static IGenerator<TSource> Iterate<TSource>(IEnumerable<TSource> initialElements, Func<IList<TSource>, TSource> iterator)

Parameters

initialElements IEnumerable<TSource>

The initial elements.

iterator Func<IList<TSource>, TSource>

The iterator function.

Returns

IGenerator<TSource>

Type Parameters

TSource

The type of elements to generate.

Remarks

Uses the last n elements to generate the next one, where n is the same number of elements as is provided initially.

Exceptions

ArgumentNullException

iterator

Iterate<TSource>(TSource, Func<TSource, TSource>)

Makes a generator that returns iterations of the specified initial element.

public static IGenerator<TSource> Iterate<TSource>(TSource initialElement, Func<TSource, TSource> iterator)

Parameters

initialElement TSource

The initial element.

iterator Func<TSource, TSource>

The iteration function.

Returns

IGenerator<TSource>

IGenerator<TSource>.

Type Parameters

TSource

The type of the source generator.

Remarks

If the iteration function is f and the initial element is x, then the result generator will generate x, f(x), f(f(x)), f(f(f(x))), ...

Exceptions

ArgumentNullException

iterator

Iterate<TSource>(TSource, TSource, Func<TSource, TSource, TSource>)

Makes a generator that uses an iterator function to generate elements.

public static IGenerator<TSource> Iterate<TSource>(TSource initialElement0, TSource initialElement1, Func<TSource, TSource, TSource> iterator)

Parameters

initialElement0 TSource

The first element to generate.

initialElement1 TSource

The second element to generate.

iterator Func<TSource, TSource, TSource>

The iterator function.

Returns

IGenerator<TSource>

Type Parameters

TSource

The type of the t source.

Remarks

The iterator function is applied the last two items generated to produce the next one. The first two elements are provided by the caller. For example,

Iterate(0, 1, (x, y) => x + y);
make a generator that produces the Fibonacci numbers.

Exceptions

ArgumentNullException

iterator

Iterate<TSource>(TSource, TSource, TSource, Func<TSource, TSource, TSource, TSource>)

Makes a generator that uses an iterator function to generate elements.

public static IGenerator<TSource> Iterate<TSource>(TSource initialElement0, TSource initialElement1, TSource initialElement2, Func<TSource, TSource, TSource, TSource> iterator)

Parameters

initialElement0 TSource

The first element to generate.

initialElement1 TSource

The second element to generate.

initialElement2 TSource

The third element to generate.

iterator Func<TSource, TSource, TSource, TSource>

The iterator function.

Returns

IGenerator<TSource>

Type Parameters

TSource

The type of the t source.

Remarks

The iterator function is applied the last three items generated to produce the next one. The first three elements are provided by the caller. For example,

Iterate(0, 0, 1, (x, y, z) => x + y + z);
make a generator that produces the Tribonacci numbers.

Exceptions

ArgumentNullException

iterator

Iterate<TSource>(TSource, TSource, TSource, TSource, Func<TSource, TSource, TSource, TSource, TSource>)

Makes a generator that uses an iterator function to generate elements.

public static IGenerator<TSource> Iterate<TSource>(TSource initialElement0, TSource initialElement1, TSource initialElement2, TSource initialElement3, Func<TSource, TSource, TSource, TSource, TSource> iterator)

Parameters

initialElement0 TSource

The first element to generate.

initialElement1 TSource

The second element to generate.

initialElement2 TSource

The third element to generate.

initialElement3 TSource

The fourth element to generate.

iterator Func<TSource, TSource, TSource, TSource, TSource>

The iterator function.

Returns

IGenerator<TSource>

Type Parameters

TSource

The type of the t source.

Remarks

The iterator function is applied the last four items generated to produce the next one. The first four elements are provided by the caller. For example,

Iterate(0, 0, 0, 1, (x, y, z, w) => x + y + w + z);
make a generator that produces the Tetranacci numbers.

Exceptions

ArgumentNullException

iterator

Iterate<TSource, TSource2>(TSource, IGenerator<TSource2>, Func<TSource, TSource2, TSource>)

Applies a function on the last element to produce the next element. The function takes twp parameters; the second parameter is supplied by a generator.

public static IGenerator<TSource> Iterate<TSource, TSource2>(TSource initialElement, IGenerator<TSource2> parm2, Func<TSource, TSource2, TSource> iterator)

Parameters

initialElement TSource
parm2 IGenerator<TSource2>
iterator Func<TSource, TSource2, TSource>

Returns

IGenerator<TSource>

Type Parameters

TSource
TSource2

Log<TSource>(IGenerator<TSource>)

A generator that generates the elements of the source generator, but sends the generated element to the Unity Console.

public static IGenerator<TSource> Log<TSource>(this IGenerator<TSource> generator)

Parameters

generator IGenerator<TSource>

The base generator.

Returns

IGenerator<TSource>

Type Parameters

TSource

The type of the generator's items.

Log<TSource>(IGenerator<TSource>, Action<TSource>)

A generator that generates the elements of the source generator, but applies a log function to each element as it is generated.

public static IGenerator<TSource> Log<TSource>(this IGenerator<TSource> generator, Action<TSource> log)

Parameters

generator IGenerator<TSource>

The generator.

log Action<TSource>

The log.

Returns

IGenerator<TSource>

IGenerator<TSource>.

Type Parameters

TSource

The type of the source generator.

Examples

var generator = Generator.Count(4).Log(x => Debug.Log(x.ToString())).Select(x => 2*x);

Remarks

This generator is useful for debugging, to inspect the results of a internal generator. It should not be used for other purposes. (Generators should generally not have side effects).

MarkovRandomInt(float[,])

Generates a Markov chain of integers from a transition table.

public static IGenerator<int> MarkovRandomInt(float[,] transitionTable)

Parameters

transitionTable float[,]

The transition table to use. The value in the table at [i][j] is the relative probability that i will be followed by j. Note that values in a row need not add to 1, the values are normalized per row. In each row, there must be at least one positive value. All values must be non-negative.

Returns

IGenerator<int>

A new generator.

MarkovRandomInt(float[,], int)

Generates a Markov chain of integers from a transition table.

public static IGenerator<int> MarkovRandomInt(float[,] transitionTable, int seed)

Parameters

transitionTable float[,]

The transition table to use. The value in the table at [i][j] is the relative probability that i will be followed by j. Note that values in a row need not add to 1, the values are normalized per row. In each row, there must be at least one positive value. All values must be non-negative.

seed int

The seed to use for the random number generator.

Returns

IGenerator<int>

A new generator.

MarkovRandomIntStartsWith(float[,], int)

Makes a Markov generator that starts with the given value.

public static IGenerator<int> MarkovRandomIntStartsWith(float[,] transitionTable, int initialValue)

Parameters

transitionTable float[,]
initialValue int

Returns

IGenerator<int>

MarkovRandomIntStartsWith(float[,], int, int)

Makes a Markov generator that starts with the given value.

public static IGenerator<int> MarkovRandomIntStartsWith(float[,] transitionTable, int initialValue, int seed)

Parameters

transitionTable float[,]
initialValue int
seed int

Returns

IGenerator<int>

MoveNext<TSource>(IGenerator<TSource>, int)

Moves the generator by a specified amount forward.

public static void MoveNext<TSource>(this IGenerator<TSource> generator, int count)

Parameters

generator IGenerator<TSource>

The generator.

count int

The number of times to move the generator forward.

Type Parameters

TSource

The type of the t source.

Exceptions

ArgumentNullException

generator

ArgumentOutOfRangeException

count;Argument cannot be smaller than 0.

NextWhile<TSource>(IGenerator<TSource>, Func<TSource, bool>)

Generates the elements from the generator while the predicate applied to elements hold an return them in an enumerable. After calling this method, the next element returned by Next (or the current value of Current) will not satisfy the predicate.

public static IEnumerable<TSource> NextWhile<TSource>(this IGenerator<TSource> source, Func<TSource, bool> predicate)

Parameters

source IGenerator<TSource>

The source generator.

predicate Func<TSource, bool>

The predicate.

Returns

IEnumerable<TSource>

A new enumerator.

Type Parameters

TSource

The type of the source generator.

Examples

The enumerable in the following will contain the elements 0, 1, 2, 3:

var list = Generator.Count(100).NextWhile(x => x < 4);

Next<TSource>(IGenerator<TSource>)

Returns the next element of the specified generator.

public static TSource Next<TSource>(this IGenerator<TSource> generator)

Parameters

generator IGenerator<TSource>

The generator.

Returns

TSource

TSource.

Type Parameters

TSource

The type of the source generator.

Exceptions

ArgumentNullException

generator

Next<TSource>(IGenerator<TSource>, int)

Returns a list of the next n items from the generator.

public static IEnumerable<TSource> Next<TSource>(this IGenerator<TSource> generator, int count)

Parameters

generator IGenerator<TSource>

The generator.

count int

How many items to return.

Returns

IEnumerable<TSource>

IEnumerable<TResult>.

Type Parameters

TSource

The type of the source.

Exceptions

ArgumentNullException

generator

ArgumentOutOfRangeException

count;Argument must be positive.

OfType<TResult>(IGenerator)

Makes a generator that will generate elements of a source generator that is of the given type.

public static IGenerator<TResult> OfType<TResult>(this IGenerator generator) where TResult : class

Parameters

generator IGenerator

The source generator.

Returns

IGenerator<TResult>

A new generator.

Type Parameters

TResult

The type of the elements the generator must generate.

OpenSawTooth(IGenerator<int>)

Makes a generator that produces evenly spaced floats from 0 (included) to 1 (excluded), and repeats the result (but with the number of samples each time given by a generator).

public static IGenerator<float> OpenSawTooth(IGenerator<int> sampleCount)

Parameters

sampleCount IGenerator<int>

The number of samples per cycle.

Returns

IGenerator<float>

A new generator.

Exceptions

ArgumentOutOfRangeException

sampleCountis not positive.

OpenSawTooth(int)

Makes a generator that produces evenly spaced floats from 0 (included) to 1 (excluded), and repeats the result.

public static IGenerator<float> OpenSawTooth(int sampleCount)

Parameters

sampleCount int

The number of samples per cycle.

Returns

IGenerator<float>

A new generator.

Exceptions

ArgumentOutOfRangeException

sampleCountis not positive.

Pad<TSource>(IGenerator<TSource>, IEnumerable<TSource>)

Pads the specified generator with elements from a given list.

public static IGenerator<TSource> Pad<TSource>(this IGenerator<TSource> generator, IEnumerable<TSource> padding)

Parameters

generator IGenerator<TSource>

The source generator.

padding IEnumerable<TSource>

The padding.

Returns

IGenerator<TSource>

IGenerator<TSource>.

Type Parameters

TSource

The type of elements to generate.

Examples

var paddedGenerator = Generator.Count(4).Pad(new List(){7, 8});
//will generate 7 8 0 1 2 3 0 1 2 3...

Exceptions

ArgumentNullException

generator is null.

ArgumentNullException

padding is null.

Pad<TSource>(IGenerator<TSource>, TSource, int)

Pads the specified generator with a constant element repeated a specified number of times.

public static IGenerator<TSource> Pad<TSource>(this IGenerator<TSource> generator, TSource padding, int padCount)

Parameters

generator IGenerator<TSource>

The source generator.

padding TSource

The padding value.

padCount int

The number of values to pad.

Returns

IGenerator<TSource>

A new generator.

Type Parameters

TSource

The type of elements to generate.

Examples

var generator = Generator.Count(4).Pad(0, 3);
//will generate 0 0 0 0 1 2 0 1 2 ...

Poisson(int, int)

A boolean generator that returns true at intervals uniformly distributed between minRadius and maxRadius.

public static IGenerator<bool> Poisson(int minRadius, int maxRadius)

Parameters

minRadius int

The minimum radius.

maxRadius int

The maximum radius.

Returns

IGenerator<bool>

RandomBoolGenerator(float)

Makes a generator that returns random boolean values, true with the specified probability.

public static IGenerator<bool> RandomBoolGenerator(float probability)

Parameters

probability float

The probability of generating $(true).

Returns

IGenerator<bool>

A new generator.

Exceptions

ArgumentException

probability does not lie between 0 and 1 (inclusive)

RandomBoolGenerator(float, int)

Makes a generator that returns random boolean values, true with the specified probability.

public static IGenerator<bool> RandomBoolGenerator(float probability, int seed)

Parameters

probability float

The probability of generating $(true).

seed int

The seed to use for the random num ber generator.

Returns

IGenerator<bool>

A new generator.

Exceptions

ArgumentException

probability does not lie between 0 and 1 (inclusive)

RepeatEach<TSource>(IGenerator<TSource>, IGenerator<int>)

Makes a new generator that will repeat each of the given generators elements a number of times.

public static IGenerator<TSource> RepeatEach<TSource>(this IGenerator<TSource> generator, IGenerator<int> repeatCountGenerator)

Parameters

generator IGenerator<TSource>

The source generator.

repeatCountGenerator IGenerator<int>

A generator used to get the number of times each element will be generated.

Returns

IGenerator<TSource>

A new generator.

Type Parameters

TSource

The type of the elements of the source generator.

Exceptions

ArgumentNullException

generator

ArgumentNullException

repeatCountGenerator is null.

RepeatEach<TSource>(IGenerator<TSource>, int)

Makes a new generator that will repeat each of the given generators elements a number of times.

public static IGenerator<TSource> RepeatEach<TSource>(this IGenerator<TSource> generator, int repeatCount)

Parameters

generator IGenerator<TSource>

The source generator.

repeatCount int

The number of times each element will be generated.

Returns

IGenerator<TSource>

A new generator.

Type Parameters

TSource

The type of the elements of the source generator.

Exceptions

ArgumentNullException

generator is null

ArgumentOutOfRangeException

repeatCount is not positive.

Repeat<TSource>(IEnumerable<TSource>)

Makes a generator that repeats elements of the given list over and over.

public static IGenerator<TSource> Repeat<TSource>(IEnumerable<TSource> list)

Parameters

list IEnumerable<TSource>

The list from which elements are generated.

Returns

IGenerator<TSource>

A new generator.

Type Parameters

TSource

The type of elements in the given list and the type of elements that will be generated.

Exceptions

ArgumentNullException

list

SelectMany<TSource, TResult>(IGenerator<TSource>, Func<TSource, IEnumerable<TResult>>)

For each item in the source generator, a list of items is generated, but the items are generated one by one (and not as a list of items).

public static IGenerator<TResult> SelectMany<TSource, TResult>(this IGenerator<TSource> generator, Func<TSource, IEnumerable<TResult>> selector)

Parameters

generator IGenerator<TSource>

The generator.

selector Func<TSource, IEnumerable<TResult>>

The function that transform elements of the source generator to a list of items.

Returns

IGenerator<TResult>

IGenerator<TResult>.

Type Parameters

TSource

The type of the source generator.

TResult

The type of the result generator.

Exceptions

ArgumentNullException

generator or selector

Select<TResult>(IGenerator<float>, ResponseCurveBase<TResult>)

Makes a new generator by transforming the elements of a given float generator using a response curve.

public static IGenerator<TResult> Select<TResult>(this IGenerator<float> generator, ResponseCurveBase<TResult> selector)

Parameters

generator IGenerator<float>

The source generator.

selector ResponseCurveBase<TResult>

The selector function, used to transform floats to elements.

Returns

IGenerator<TResult>

IGenerator<TResult>.

Type Parameters

TResult

The type of elements this generator will generate.

Exceptions

ArgumentNullException

generator or selector is null

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

Makes a generator which generates items that are transformed, generated from a given generator.

public static IGenerator<TResult> Select<TSource, TResult>(this IGenerator<TSource> generator, Func<TSource, TResult> selector)

Parameters

generator IGenerator<TSource>

The generator.

selector Func<TSource, TResult>

The function used to transform elements of the source generator.

Returns

IGenerator<TResult>

IGenerator<TResult>.

Type Parameters

TSource

The type of the source generator.

TResult

The type of the result generator.

Exceptions

ArgumentNullException

generator or selector

SkipAndTake<TSource>(IGenerator<TSource>, int, int)

Makes a generator that repeatedly skips over and takes elements from a given generator.

public static IGenerator<TSource> SkipAndTake<TSource>(this IGenerator<TSource> generator, int skipCount, int takeCount)

Parameters

generator IGenerator<TSource>

The source generator.

skipCount int

The number of elements to skip each cycle.

takeCount int

The number of elements to take each cycle.

Returns

IGenerator<TSource>

A new generator.

Type Parameters

TSource

The type of elements of the source generator.

Examples

The following generator will generate 0 1 2 4 5 6 8 9 10...

var generator = Generator
   	.Count(100)
   	.TakeAndSkip(3, 1);

Exceptions

ArgumentNullException

generator is null.

ArgumentOutOfRangeException

takeCount is not larger than 0.

ArgumentOutOfRangeException

skipCount is negative.

Skip<TSource>(IGenerator<TSource>, int)

Makes a generator that skips over the specified number of elements from the source generator.

public static IGenerator<TSource> Skip<TSource>(this IGenerator<TSource> generator, int count)

Parameters

generator IGenerator<TSource>

The source generator.

count int

The number of elements to skip.

Returns

IGenerator<TSource>

A new generator.

Type Parameters

TSource

The type of elements of the source generator.

Examples

The following generator will generate 3 4 0 1 2 3 4 0 ...

var generator = Generator.Count(5).Skip(3);

Exceptions

ArgumentNullException

generator is null.

ArgumentOutOfRangeException

count is negative.

Sum(IGenerator<int>)

Makes a generator that will generate partial sums of a given generator.

public static IGenerator<int> Sum(this IGenerator<int> generator)

Parameters

generator IGenerator<int>

The generator.

Returns

IGenerator<int>

A new generator.

Examples

The following generator will generate 0, 1, 2, 3, 4, 5...

var generator = Generator.Constant(1).Sum();

Exceptions

ArgumentNullException

source is null

Sum(IGenerator<float>)

Makes a generator that will generate partial sums of a given generator.

public static IGenerator<float> Sum(this IGenerator<float> generator)

Parameters

generator IGenerator<float>

The generator.

Returns

IGenerator<float>

A new generator.

Examples

The following generator will generate 0f, 1f, 2f, 3f, 4f, 5f...

var generator = Generator.Constant(1f).Sum();

Exceptions

ArgumentNullException

source is null

SwitchAfter<TSource>(IGenerator<TSource>, int, IGenerator<TSource>)

Generates elements from a source generator for the given number of steps, then switches to a second generator.

public static IGenerator<TSource> SwitchAfter<TSource>(this IGenerator<TSource> source, int steps, IGenerator<TSource> newGenerator)

Parameters

source IGenerator<TSource>

The source generator that will be used before the switch.

steps int

The number of steps before switching.

newGenerator IGenerator<TSource>

The new generator that will be used after the switch.

Returns

IGenerator<TSource>

A new generator.

Type Parameters

TSource

The type of the source generator.

Exceptions

ArgumentNullException

source is null

ArgumentNullException

newGenerator is null

SwitchWhen<TSource>(IGenerator<TSource>, Func<TSource, bool>, IGenerator<TSource>)

Generates elements from the source generator until a condition is met, then generate elements from a second generator.

public static IGenerator<TSource> SwitchWhen<TSource>(this IGenerator<TSource> source, Func<TSource, bool> predicate, IGenerator<TSource> newGenerator)

Parameters

source IGenerator<TSource>

The source.

predicate Func<TSource, bool>

The predicate.

newGenerator IGenerator<TSource>

The new generator.

Returns

IGenerator<TSource>

A new generator

Type Parameters

TSource

The type of the source generator.

Exceptions

ArgumentNullException

source is null

ArgumentNullException

predicate is null

ArgumentNullException

newGenerator is null

TakeAndSkip<TSource>(IGenerator<TSource>, int, int)

Makes a generator that repeatedly takes and skips over elements from a given generator.

public static IGenerator<TSource> TakeAndSkip<TSource>(this IGenerator<TSource> generator, int takeCount, int skipCount)

Parameters

generator IGenerator<TSource>

The source generator.

takeCount int

The number of elements to take each cycle.

skipCount int

The number of elements to skip each cycle.

Returns

IGenerator<TSource>

A new generator.

Type Parameters

TSource

The type of elements of the source generator.

Examples

The following generator will generate 0 1 2 4 5 6 8 9 10...

var generator = Generator
   	.Count(100)
   	.TakeAndSkip(3, 1);

Exceptions

ArgumentNullException

generator is null.

ArgumentOutOfRangeException

takeCount is not larger than 0.

ArgumentOutOfRangeException

skipCount is negative.

UniformRandomFloat()

Makes a generator hat generates floats uniformly between 0 and 1.

public static IGenerator<float> UniformRandomFloat()

Returns

IGenerator<float>

A new generator.

UniformRandomFloat(int)

Makes a generator that generates floats uniformly between 0 and 1.

public static IGenerator<float> UniformRandomFloat(int seed)

Parameters

seed int

Returns

IGenerator<float>

A new generator.

UniformRandomInt(int)

Makes a generator that generates integers uniformly distributed between 0 (included) and the specified limit (excluded).

public static IGenerator<int> UniformRandomInt(int upperLimitExcluded)

Parameters

upperLimitExcluded int

The upper limit (excluded).

Returns

IGenerator<int>

IGenerator<System.Int32>.

Exceptions

ArgumentOutOfRangeException

upperLimitExcludedis not positive.

UniformRandomInt(int, int)

Makes a generator hat generates integers uniformly distributed between 0 (included) and the specified limit (excluded).

public static IGenerator<int> UniformRandomInt(int upperLimitExcluded, int seed)

Parameters

upperLimitExcluded int

The upper limit (excluded).

seed int

The seed to use for the random number generator.

Returns

IGenerator<int>

A new generator.

Exceptions

ArgumentOutOfRangeException

upperLimitExcludedis not positive.

UniformVector2InCircle(float)

Generates vectors uniformly distributed in a given circle.

public static IGenerator<Vector2> UniformVector2InCircle(float radius)

Parameters

radius float

Returns

IGenerator<Vector2>

UniformVector2InRect(Vector2)

Generates vectors uniformly distributed in a given rectangle.

public static IGenerator<Vector2> UniformVector2InRect(Vector2 dimensions)

Parameters

dimensions Vector2

The dimensions of the rectangle.

Returns

IGenerator<Vector2>

WhereWindow<TSource>(IGenerator<TSource>, int, Func<TSource[], bool>)

Only generates an item if the window of the item passes the predicate.

public static IGenerator<TSource> WhereWindow<TSource>(this IGenerator<TSource> generator, int windowSize, Func<TSource[], bool> predicate)

Parameters

generator IGenerator<TSource>

The generator.

windowSize int

Size of the window.

predicate Func<TSource[], bool>

The predicate.

Returns

IGenerator<TSource>

IGenerator<TSource>.

Type Parameters

TSource

The type of the source generator.

Remarks

The window of an item is the element and a number of elements after the element (together, the number of elements is windowSize).

Exceptions

ArgumentNullException

generator or predicate

ArgumentOutOfRangeException

windowSize;Argument must be positive.

Where<TSource>(IGenerator<TSource>, IGenerator<bool>)

Makes a generator that will only generate elements that pass the predicate generated by the predicate generator.

public static IGenerator<TSource> Where<TSource>(this IGenerator<TSource> generator, IGenerator<bool> predicateGenerator)

Parameters

generator IGenerator<TSource>
predicateGenerator IGenerator<bool>

Returns

IGenerator<TSource>

Type Parameters

TSource

Where<T>(IGenerator<T>, IGenerator<bool>, int)

Makes a generator that will only generate elements that pass the predicate generated by the predicate generator. If the source elements does not provide elements that pass the predicate for the given number of maximum iterations, an exception is thrown. This is to prevent a stalling the generator forever.

public static IGenerator<T> Where<T>(this IGenerator<T> generator, IGenerator<bool> predicateGenerator, int maxIterations)

Parameters

generator IGenerator<T>
predicateGenerator IGenerator<bool>
maxIterations int

Returns

IGenerator<T>

Type Parameters

T

Where<TSource>(IGenerator<TSource>, Func<TSource, bool>)

Makes a generator that will only generate elements that pass the predicate.

public static IGenerator<TSource> Where<TSource>(this IGenerator<TSource> generator, Func<TSource, bool> predicate)

Parameters

generator IGenerator<TSource>

The generator.

predicate Func<TSource, bool>

The predicate.

Returns

IGenerator<TSource>

IGenerator<TSource>.

Type Parameters

TSource

The type of the source generator.

Where<T>(IGenerator<T>, Func<T, bool>, int)

Makes a generator that will only generate elements that pass the predicate. If the source elements does not provide elements that pass the predicate for the given number of maximum iterations, an exception is thrown. This is to prevent a stalling the generator forever.

public static IGenerator<T> Where<T>(this IGenerator<T> generator, Func<T, bool> predicate, int maxIterations)

Parameters

generator IGenerator<T>
predicate Func<T, bool>
maxIterations int

Returns

IGenerator<T>

Type Parameters

T

Window<TSource>(IGenerator<TSource>, int)

Makes a generator that generates a moving window of elements over a given generator.

public static IGenerator<TSource[]> Window<TSource>(this IGenerator<TSource> source, int windowSize)

Parameters

source IGenerator<TSource>

The source generator.

windowSize int

Size of the window.

Returns

IGenerator<TSource[]>

A new generator.

Type Parameters

TSource

The type of the source generator.

Examples

In the following example,

var generator = Generator.Count(4).Window(2);

the generator will generate (0 1) (1 2) (2 3) (3 0) (0 1)... The window of size 2 move one element (of the original generator) at a time.

The following is an implementation of a box blur on the given sequence:

public static IGenerator<float> BoxBlur(IGenerator<float> generator)
{
	return generator.Window(3).Select(w => (w[0] + w[1] + w[2])/3f);
}

Exceptions

ArgumentOutOfRangeException

windowSize;Argument must be positive.