Cadenza : Cadenza Namespace

Sequence Class

Provides a set of static methods for creating sequences.

public static class Sequence

Thread Safety

This type is thread safe.

Remarks

Requirements

Namespace: Cadenza
Assembly: Cadenza (in Cadenza.dll)
Assembly Versions: 0.1.0.0

Members

See Also: Inherited members from object.

Public Methods

static
Expand (object) : IEnumerable
Depth-first object graph traversal.
static
Expand (object, IEnumerable<Type>) : IEnumerable
Depth-first object graph traversal.
static
Expand (object, params Type[]) : IEnumerable
Depth-first object graph traversal.
static
GenerateReverse<TSource,TResult> (TSource, Func<TSource, Maybe<Tuple<TResult, TSource>>>) : IEnumerable<TResult>
Creates a IEnumerable<TResult> containing the values returned by selector.
static
Iterate<TSource> (TSource, Func<TSource, TSource>) : IEnumerable<TSource>
Creates an infinite IEnumerable<TSource> sequence determined by value and selector.
static
Repeat<TSource> (TSource) : IEnumerable<TSource>
Creates an infinite IEnumerable<TSource> seqeuence where each element value is value.

Member Details

Expand Method

Depth-first object graph traversal.

public static IEnumerable Expand (object o)

Parameters

o
A object containing the object graph to traverse.

Returns

An IEnumerable containing all non-string, non-IEnumerable values found while traversing o

Remarks

This is to simplify supporting System.Xml.Linq.XContainer.Add(object)-style enumerable traversal.

If o does not implement IEnumerable, then an enumerator which will yield o is returned.

If o implements IEnumerable, then it is enumerated over, and if an enumerated element implements IEnumerable, it too is enumerated, repeatedly, until the enumerated element doesn't implement IEnumerable or the element is a string.

C# Example
Assert.AreEqual (
	"1,2,3,4",
	Sequence.Expand (new object[]{
		Enumerable.Range (1, 2),
		"3",
		4
	}).Cast<object>().Implode (","));

Requirements

Namespace: Cadenza
Assembly: Cadenza (in Cadenza.dll)
Assembly Versions: 0.1.0.0

Expand Method

Depth-first object graph traversal.

public static IEnumerable Expand (object o, IEnumerable<Type> except)

Parameters

o
A object containing the object graph to traverse.
except
A IEnumerable<Type> containing the types that, while implementing IEnumerable, should not be traversed. An inheritance match is used.

Returns

An IEnumerable containing only values with types present within except or values with types that do not implement IEnumerable.

Exceptions

Type Reason
ArgumentNullException except is null.

Remarks

This is to simplify supporting System.Xml.Linq.XContainer.Add(object)-style enumerable traversal.

If o does not implement IEnumerable, then an enumerator which will yield o is returned.

If o implements IEnumerable, then it is enumerated over, and if an enumerated element implements IEnumerable, it too is enumerated, repeatedly, until either the enumerated element doesn't implement IEnumerable or the element's type is present within except. except type matching is based on Type.IsAssignableFrom(Type); consequently, exact type matching is not performed, and inheritance relationships are taken into consideration.

For example, if except contains typeof(ICollection), then if o were a List<int> (which implements ICollection), then the embedded list will not be further enumerated.

C# Example
IEnumerable seq = Sequence.Expand (new List<int> { 1, 2, 3 }, typeof (ICollection));
foreach (object v in seq)
	Assert.IsTrue (v is List<int>);

Requirements

Namespace: Cadenza
Assembly: Cadenza (in Cadenza.dll)
Assembly Versions: 0.1.0.0

Expand Method

Depth-first object graph traversal.

public static IEnumerable Expand (object o, params Type[] except)

Parameters

o
A object containing the object graph to traverse.
except
A Type array containing the types that, while implementing IEnumerable, should not be traversed. An inheritance match is used.

Returns

An IEnumerable containing only values with types present within except or values with types that do not implement IEnumerable.

Exceptions

Type Reason
ArgumentNullException except is null.

Remarks

This is to simplify supporting System.Xml.Linq.XContainer.Add(object)-style enumerable traversal.

If o does not implement IEnumerable, then an enumerator which will yield o is returned.

If o implements IEnumerable, then it is enumerated over, and if an enumerated element implements IEnumerable, it too is enumerated, repeatedly, until either the enumerated element doesn't implement IEnumerable or the element's type is present within except. except type matching is based on Type.IsAssignableFrom(Type); consequently, exact type matching is not performed, and inheritance relationships are taken into consideration.

For example, if except contains typeof(ICollection), then if o were a List<int> (which implements ICollection), then the embedded list will not be further enumerated.

C# Example
IEnumerable seq = Sequence.Expand (new List<int> { 1, 2, 3 }, typeof (ICollection));
foreach (object v in seq)
	Assert.IsTrue (v is List<int>);

Requirements

Namespace: Cadenza
Assembly: Cadenza (in Cadenza.dll)
Assembly Versions: 0.1.0.0

GenerateReverse<TSource,TResult> Generic Method

Creates a IEnumerable<TResult> containing the values returned by selector.

public static IEnumerable<TResult> GenerateReverse<TSource, TResult> (TSource value, Func<TSource, Maybe<Tuple<TResult, TSource>>> selector)

See Also

Cadenza.Collections.IEnumerableCoda.AggregateReverse``2(IEnumerable<``0>, ``1, Func<``1, ``0, ``1>)
Maybe.When``1(bool, ``0)
Maybe.When``1(bool, Func<``0>)
Tuple<TResult, TSource>

Type Parameters

TSource
The type of the seed value.
TResult
The type to create a sequence of.

Parameters

value
A TSource containing the initial value to pass to selector.
selector
A Func<TSource, Cadenza.Maybe<Cadenza.Tuple<TResult, TSource>>> which is invoked to generate the sequence.

Returns

A IEnumerable<TResult> containing the values returned by selector.

Exceptions

Type Reason
ArgumentNullException selector is null.

Remarks

GenerateReverse acts as a "dual" to Cadenza.Collections.IEnumerableCoda.AggregateReverse``2(IEnumerable<``0>, ``1, Func<``1, ``0, ``1>): while AggregateReverse reduces a list to a summary value (evaluating the list starting from the end), GenerateReverse builds a list from a seed value.

Operation

selector is first invoked with value, and returns a Cadenza.Maybe<Cadenza.Tuple<TResult, TSource>>. If the returned Cadenza.Maybe<Cadenza.Tuple<TResult, TSource>>. contains a value (Maybe<Tuple<TResult, TSource>>.HasValue is true), then Tuple<TResult, TSource>._1 will be returned from the generator, and Tuple<TResult, TSource>._2 will be used as the value for subsequent selector invocations.

When selector returns Maybe<Tuple<TResult, TSource>>.Nothing, the sequence is terminated.

C# Example
Assert.AreEqual ("10,9,8,7,6,5,4,3,2,1",
	Sequence.GenerateReverse (10, 
		b => Maybe.When (b > 0, Tuple.Create (b, b-1)))
	.Implode (","));

Requirements

Namespace: Cadenza
Assembly: Cadenza (in Cadenza.dll)
Assembly Versions: 0.1.0.0

Iterate<TSource> Generic Method

Creates an infinite IEnumerable<TSource> sequence determined by value and selector.

public static IEnumerable<TSource> Iterate<TSource> (TSource value, Func<TSource, TSource> selector)

Type Parameters

TSource
The type of the seed value value and the element type of the generated sequence.

Parameters

value
The initial value to provide to selector.
selector
A Func<TSource, TSource> which is used to generate the next value in the sequence.

Returns

An infinite IEnumerable<TSource> sequence determined by value and selector.

Exceptions

Type Reason
ArgumentNullException selector is null.

Remarks

Operation

On the first selector invocation, value is provided as the parameter value.

For all subsequent selector invocations, the parameter value is the result of the previous selector invocation.

Thus, the generated sequence is equivalent to: new[]{ selector(value), selector(selector(value)), selector(selector(selector(value))), ... }.

C# Example
Assert.AreEqual ("16,8,4,2,1",
		Sequence.Iterate (16, v => v / 2).Take (5).Implode (","));
Assert.AreEqual ("1,2,3,4,5",
		Sequence.Iterate (1, v => v+1).Take (5).Implode (","));

Requirements

Namespace: Cadenza
Assembly: Cadenza (in Cadenza.dll)
Assembly Versions: 0.1.0.0

Repeat<TSource> Generic Method

Creates an infinite IEnumerable<TSource> seqeuence where each element value is value.

public static IEnumerable<TSource> Repeat<TSource> (TSource value)

Type Parameters

TSource
The type of the value to repeat.

Parameters

value
A TSource containing the value to repeat.

Returns

An infinite IEnumerable<TSource> seqeuence where each element value is value.

Remarks

C# Example
Assert.AreEqual ("1,1,1,1,1",
		Sequence.Repeat (1).Take (5).Implode (","));

Requirements

Namespace: Cadenza
Assembly: Cadenza (in Cadenza.dll)
Assembly Versions: 0.1.0.0