Method Iterate
- Namespace
- Gamelogic.Extensions.Algorithms
- Assembly
- Gamelogic.Extensions.dll
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
TSourceThe 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, TSource2>(TSource, IGenerator<TSource2>, Func<TSource, TSource2, TSource>)
Applies a function on the last element to produce the next element. The function takes two parameters; the second parameter is supplied by a generator.
public static IGenerator<TSource> Iterate<TSource, TSource2>(TSource initialElement, IGenerator<TSource2> source, Func<TSource, TSource2, TSource> iterator)
Parameters
initialElement
TSourcesource
IGenerator<TSource2>iterator
Func<TSource, TSource2, TSource>
Returns
- IGenerator<TSource>
Type Parameters
TSource
TSource2
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
TSourceThe first element to generate.
initialElement1
TSourceThe 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
TSourceThe first element to generate.
initialElement1
TSourceThe second element to generate.
initialElement2
TSourceThe 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
TSourceThe first element to generate.
initialElement1
TSourceThe second element to generate.
initialElement2
TSourceThe third element to generate.
initialElement3
TSourceThe 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>(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