Cadenza : Cadenza Namespace

ObjectCoda Class

Extension methods on object.

public static class ObjectCoda

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
Just<T> (this T) : Maybe<T>
Create a new Cadenza.Maybe<T> instance initialized to a specified value. The returned value will not be Maybe<T>.Nothing.
static
Match<TSource,TResult> (this TSource, params Func<TSource, Maybe<TResult>>[]) : TResult
Converts the TSource instance self into a TResult.
static
ToMaybe<T> (this T) : Maybe<T>
Create a new Cadenza.Maybe<T> instance initialized to a specified value. The returned value may be Maybe<T>.Nothing.
static
TraverseBreadthFirst<TSource,TResult> (this TSource, Func<TSource, TResult>, Func<TSource, IEnumerable<TSource>>) : IEnumerable<TResult>
Traverse a tree in a breadth-first fashion, converting each encountered node.
static
TraverseBreadthFirstWithParent<TSource,TResult> (this TSource, Func<TSource, TResult>, Func<TSource, IEnumerable<TSource>>) : IEnumerable<KeyValuePair<TSource, TResult>>
Traverse a tree in a breadth-first fashion, converting each encountered node.
static
TraverseDepthFirst<TSource,TResult> (this TSource, Func<TSource, TResult>, Func<TSource, IEnumerable<TSource>>) : IEnumerable<TResult>
Traverse a tree in a depth-first fashion, converting each encountered node.
static
TraverseDepthFirstWithParent<TSource,TResult> (this TSource, Func<TSource, TResult>, Func<TSource, IEnumerable<TSource>>) : IEnumerable<KeyValuePair<TSource, TResult>>
Traverse a tree in a depth-first fashion, converting each encountered node.
static
With<TSource,TResult> (this TSource, Func<TSource, TResult>) : TResult
Supports chaining otherwise temporary values.

Member Details

Just<T> Generic Method

Create a new Cadenza.Maybe<T> instance initialized to a specified value. The returned value will not be Maybe<T>.Nothing.

public static Maybe<T> Just<T> (this T self)

See Also

ObjectCoda.ToMaybe``1(``0)

Type Parameters

T
The type to convert into a Cadenza.Maybe<T>.

Parameters

self
A value of type T to convert into a Cadenza.Maybe<T>.

Returns

A new Cadenza.Maybe<T> instance initialized to a specified value.

The returned value will not be Maybe<T>.Nothing.

Exceptions

Type Reason
ArgumentNullException T is a reference type and self is null.

Remarks

Use this method when you want to ensure that Cadenza.Maybe<T> instance is created in which Maybe<T>.Value will not throw.

Requirements

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

Match<TSource,TResult> Generic Method

Converts the TSource instance self into a TResult.

public static TResult Match<TSource, TResult> (this TSource self, params Func<TSource, Maybe<TResult>>[] matchers)

See Also

Maybe.When``1(bool, ``0)
Maybe.When``1(bool, Func<``0>)

Type Parameters

TSource
The source type.
TResult
The result type.

Parameters

self
A value of type TSource to convert to TResult.
matchers
A Func<TSource, Cadenza.Maybe<TResult>> array containing the conversion routines to try, in order, to convert self into a TResult.

Returns

A value of type TResult, as returned by one of the conversion delegates in matchers.

Exceptions

Type Reason
ArgumentNullException matchers is null.
InvalidOperationException None of the Func<TSource, Cadenza.Maybe<TResult>> delegates within matchers returned a Cadenza.Maybe<TResult> instance where Maybe<TResult>.HasValue was true.

Remarks

Operation

self is converted into a TResult instance by trying each Func<TSource, Cadenza.Maybe<TResult>> within matchers.

This method returns the value of where Maybe<TResult>.Value for the first delegate to return a Cadenza.Maybe<TResult> instance where Maybe<TResult>.HasValue is true.

If no Func<TSource, Cadenza.Maybe<TResult>> returns a Cadenza.Maybe<TResult> instance where Maybe<TResult>.HasValue is true, then an InvalidOperationException is thrown.

C# Example
Assert.AreEqual ("foo",
	"foo".Match (
		s => Maybe.When (s.Length != 3, "bar!"),
		s => s.Just ()));
Assert.AreEqual ("bar!",
	5.Match (
		v => Maybe.When (v != 3, "bar!"),
		v => v.ToString ().Just()));
var m = new Func<string, Maybe<int>>[] {
	v => Maybe.When (v == "bar",    1),
	v => Maybe.When (v.Length == 5, 2),
	v => (-1).Just (),
};
Assert.AreEqual (1, "bar".Match (m));
Assert.AreEqual (2, "12345".Match (m));
Assert.AreEqual (-1, "*default*".Match (m));

Requirements

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

ToMaybe<T> Generic Method

Create a new Cadenza.Maybe<T> instance initialized to a specified value. The returned value may be Maybe<T>.Nothing.

public static Maybe<T> ToMaybe<T> (this T self)

See Also

ObjectCoda.Just``1(``0)

Type Parameters

T
The type to convert into a Cadenza.Maybe<T>.

Parameters

self
A value of type T to convert into a Cadenza.Maybe<T>.

Returns

A new Cadenza.Maybe<T> instance initialized to a specified value.

Note: The value returned may be Maybe<T>.Nothing.

Remarks

Use this method when you want to ensure that a possibly invalid Cadenza.Maybe<T> instance is created. If T is a reference type and self is null, then Maybe<T>.Nothing will be returned; otherwise, a new Cadenza.Maybe<T> will be created containing the value self.

Requirements

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

TraverseBreadthFirst<TSource,TResult> Generic Method

Traverse a tree in a breadth-first fashion, converting each encountered node.

public static IEnumerable<TResult> TraverseBreadthFirst<TSource, TResult> (this TSource self, Func<TSource, TResult> valueSelector, Func<TSource, IEnumerable<TSource>> childrenSelector)

Type Parameters

TSource
The type of the root node and intermediate nodes of the tree.
TResult
The type of the object to return.

Parameters

self
The root of a tree to traverse.
valueSelector
A Func<TSource, TResult> which is used to convert tree nodes into TResult instances.
childrenSelector
A Func<TSource, IEnumerable<TSource>> which returns the child nodes of root and all intermediate non-leaf nodes.

Returns

A IEnumerable<TResult> containing the result of applying valueSelector to all nodes encountered while traversing the tree self in a breadth-first fashion.

Exceptions

Type Reason
ArgumentNullException

self is null.

-or-

childrenSelector is null.

-or-

valueSelector is null.

Remarks

self is the root node of a tree, wherein each node is a data structure containing a value and child nodes. The value is retrieved via valueSelector, and the children are obtained via childrenSelector.

The tree is traversed in a breadth-first fashion, each encountered node is provided to valueSelector, and the values are returned.

Example

Given the TreeNode<T> declaration:

C# Example
class TreeNode<T>
{
	public TreeNode ()
	{
		Children = new TreeNode<T> [0];
	}

	public T Value;
	public IEnumerable<TreeNode<T>> Children;
}

TraverseBreadthFirst() is used as:

C# Example
TreeNode<int> root = new TreeNode<int> {
	Value = 1, Children = new [] {
		new TreeNode<int> { Value = 2 },
		new TreeNode<int> {
			Value = 3, Children = new [] {
				new TreeNode<int> { Value = 5 },
			}
		},
		new TreeNode<int> { Value = 4 },
	}
};
IEnumerable<int> values = root
	.TraverseBreadthFirst (x => x.Value, x => x.Children);
AssertAreSame (new[]{ 1, 2, 3, 4, 5 }, values);

Requirements

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

TraverseBreadthFirstWithParent<TSource,TResult> Generic Method

Traverse a tree in a breadth-first fashion, converting each encountered node.

public static IEnumerable<KeyValuePair<TSource, TResult>> TraverseBreadthFirstWithParent<TSource, TResult> (this TSource self, Func<TSource, TResult> valueSelector, Func<TSource, IEnumerable<TSource>> childrenSelector)

Type Parameters

TSource
The type of the root node and intermediate nodes of the tree.
TResult
The type of the object to return.

Parameters

self
The root of a tree to traverse.
valueSelector
A Func<TSource, TResult> which is used to convert tree nodes into TResult instances.
childrenSelector
A Func<TSource, IEnumerable<TSource>> which returns the child nodes of root and all intermediate non-leaf nodes.

Returns

A IEnumerable<KeyValuePair<TSource, TResult>> in which each returned KeyValuePair<TSource, TResult>.Value is the result of applying valueSelector to a node, and KeyValuePair<TSource, TResult>.Key holds the parent of that node. If the node has no parent (e.g. for self), then KeyValuePair<TSource, TResult>.Key will contain default(TSource).

Returned KeyValuePair<TSource, TResult> values come from traversing self in a breadth-first order.

Exceptions

Type Reason
ArgumentNullException

self is null.

-or-

childrenSelector is null.

-or-

valueSelector is null.

Remarks

self is the root node of a tree, wherein each node is a data structure containing a value and child nodes. The value is retrieved via valueSelector, and the children are obtained via childrenSelector.

The tree is traversed in a breadth-first fashion, each encountered node is provided to valueSelector, and the values are returned.

Requirements

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

TraverseDepthFirst<TSource,TResult> Generic Method

Traverse a tree in a depth-first fashion, converting each encountered node.

public static IEnumerable<TResult> TraverseDepthFirst<TSource, TResult> (this TSource self, Func<TSource, TResult> valueSelector, Func<TSource, IEnumerable<TSource>> childrenSelector)

Type Parameters

TSource
The type of the root node and intermediate nodes of the tree.
TResult
The type of the object to return.

Parameters

self
The root of a tree to traverse.
valueSelector
A Func<TSource, TResult> which is used to convert tree nodes into TResult instances.
childrenSelector
A Func<TSource, IEnumerable<TSource>> which returns the child nodes of root and all intermediate non-leaf nodes.

Returns

A IEnumerable<TResult> containing the result of applying valueSelector to all nodes encountered while traversing the tree self in a depth-first fashion.

Exceptions

Type Reason
ArgumentNullException

self is null.

-or-

childrenSelector is null.

-or-

valueSelector is null.

Remarks

self is the root node of a tree, wherein each node is a data structure containing a value and child nodes. The value is retrieved via valueSelector, and the children are obtained via childrenSelector.

The tree is traversed in a depth-first fashion, each encountered node is provided to valueSelector, and the values are returned.

Example

Given the TreeNode<T> declaration:

C# Example
class TreeNode<T>
{
	public TreeNode ()
	{
		Children = new TreeNode<T> [0];
	}

	public T Value;
	public IEnumerable<TreeNode<T>> Children;
}

TraverseDepthFirst() is used as:

C# Example
TreeNode<int> root = new TreeNode<int> {
	Value = 1, Children = new [] {
		new TreeNode<int> { Value = 2 },
		new TreeNode<int> {
			Value = 3, Children = new [] {
				new TreeNode<int> { Value = 5 },
			}
		},
		new TreeNode<int> { Value = 4 },
	}
};
IEnumerable<int> values = root
	.TraverseDepthFirst (x => x.Value, x => x.Children);
AssertAreSame (new[]{ 1, 2, 3, 5, 4 }, values);

Requirements

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

TraverseDepthFirstWithParent<TSource,TResult> Generic Method

Traverse a tree in a depth-first fashion, converting each encountered node.

public static IEnumerable<KeyValuePair<TSource, TResult>> TraverseDepthFirstWithParent<TSource, TResult> (this TSource self, Func<TSource, TResult> valueSelector, Func<TSource, IEnumerable<TSource>> childrenSelector)

Type Parameters

TSource
The type of the root node and intermediate nodes of the tree.
TResult
The type of the object to return.

Parameters

self
The root of a tree to traverse.
valueSelector
A Func<TSource, TResult> which is used to convert tree nodes into TResult instances.
childrenSelector
A Func<TSource, IEnumerable<TSource>> which returns the child nodes of root and all intermediate non-leaf nodes.

Returns

A IEnumerable<KeyValuePair<TSource, TResult>> in which each returned KeyValuePair<TSource, TResult>.Value is the result of applying valueSelector to a node, and KeyValuePair<TSource, TResult>.Key holds the parent of that node. If the node has no parent (e.g. for self), then KeyValuePair<TSource, TResult>.Key will contain default(TSource).

Returned KeyValuePair<TSource, TResult> values come from traversing self in a breadth-first order.

Exceptions

Type Reason
ArgumentNullException

self is null.

-or-

childrenSelector is null.

-or-

valueSelector is null.

Remarks

self is the root node of a tree, wherein each node is a data structure containing a value and child nodes. The value is retrieved via valueSelector, and the children are obtained via childrenSelector.

The tree is traversed in a depth-first fashion, each encountered node is provided to valueSelector, and the values are returned.

Requirements

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

With<TSource,TResult> Generic Method

Supports chaining otherwise temporary values.

public static TResult With<TSource, TResult> (this TSource self, Func<TSource, TResult> selector)

Type Parameters

TSource
The type to operate on.
TResult
The type to return.

Parameters

self
A TSource containing the value to manipulate. This value may be null (unlike most other extension methods).
selector
A Func<TSource, TResult> which will be invoked with self as a parameter.

Returns

The value of type TResult returned by selector.

Exceptions

Type Reason
ArgumentNullException selector is null.

Remarks

With is useful for easily using an intermediate value within an expression "chain" without requiring an explicit variable declaration (which is useful for reducing in-scope variables, as no variable is explicitly declared).

C# Example
// sorts the array, then returns the 
// element in the middle of the array.
Assert.AreEqual (3,
	new[]{5, 4, 3, 2, 1}.Sort ()
	.With (c => c.ElementAt (c.Count()/2)));

Requirements

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