Show / Hide Table of Contents

Class MaybeExtensions

Extensions for IEnumerable<T>, and for some specific types of Maybe<T>, to support the wider use of Maybe<T>:

  • IEnumerable<T>
  • Maybe<string>
  • Maybe<Guid>
  • Maybe<T[]>
  • Maybe<List<T>>
These methods make it easier to work with Maybe<T> in a variety of contexts.
Inheritance
System.Object
MaybeExtensions
Inherited Members
System.Object.Equals(System.Object)
System.Object.Equals(System.Object, System.Object)
System.Object.GetHashCode()
System.Object.GetType()
System.Object.MemberwiseClone()
System.Object.ReferenceEquals(System.Object, System.Object)
System.Object.ToString()
Namespace: Existential
Assembly: Existential.Net.dll
Syntax
public static class MaybeExtensions

Methods

View Source

GetValueOrEmpty(Maybe<Guid>)

Returns the value, if it exists, or System.Guid.Empty.

Declaration
public static Guid GetValueOrEmpty(this Maybe<Guid> inMaybe)
Parameters
Type Name Description
Maybe<System.Guid> inMaybe

The Maybe<T> (where T is System.Guid) that may or may not contain a value.

Returns
Type Description
System.Guid

The value, if it exists, or System.Guid.Empty.

Remarks

Implemented as an extension method to keep it type-specific to GUIDs.

View Source

GetValueOrEmpty(Maybe<String>)

Returns the value, if it exists, or the empty string.

Declaration
public static string GetValueOrEmpty(this Maybe<string> inMaybe)
Parameters
Type Name Description
Maybe<System.String> inMaybe

The Maybe<T> (where T is System.String) that may or may not contain a value.

Returns
Type Description
System.String

The value, if it exists, or the empty string.

Remarks

Implemented as an extension method to keep it type-specific to strings.

View Source

GetValueOrEmpty<T>(Maybe<T[]>)

Returns the value, if it exists, or an empty array of the same type.

Declaration
public static T[] GetValueOrEmpty<T>(this Maybe<T[]> inMaybe)
Parameters
Type Name Description
Maybe<T[]> inMaybe

The Maybe<T> that may or may not contain an array.

Returns
Type Description
T[]

The value, if it exists, or an empty array of the same type.

Type Parameters
Name Description
T

The type contained by the array.

Remarks

Implemented as an extension method to keep it type-specific to arrays of T.

View Source

GetValueOrEmpty<T>(Maybe<List<T>>)

Returns the value, if it exists, or an empty collection of the same type.

Declaration
public static List<T> GetValueOrEmpty<T>(this Maybe<List<T>> inMaybe)
Parameters
Type Name Description
Maybe<System.Collections.Generic.List<T>> inMaybe

The Maybe<T> that may or may not contain a generic collection.

Returns
Type Description
System.Collections.Generic.List<T>

The value, if it exists, or an empty collection of the same type.

Type Parameters
Name Description
T

The type contained by the collection.

Remarks

Implemented as an extension method to keep it type-specific to generic collections of T.

View Source

MaybeFirst<T>(IEnumerable<T>)

Gets a Maybe<T> containing the first member of the collection if it exists.

Declaration
public static Maybe<T> MaybeFirst<T>(this IEnumerable<T> inCollection)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> inCollection

A collection in which to find the first member.

Returns
Type Description
Maybe<T>

A Maybe<T> containing the first member if it exists.

Type Parameters
Name Description
T

The type of the collection members.

View Source

MaybeFirstThatExists<T>(IEnumerable<T>)

Gets a Maybe<T> containing the first matching member of the collection, if it exists.

Declaration
public static Maybe<T> MaybeFirstThatExists<T>(this IEnumerable<T> inCollection)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> inCollection

A collection in which to find the first matching member.

Returns
Type Description
Maybe<T>

A Maybe<T> containing the first member if it exists.

Type Parameters
Name Description
T

The type of the collection members.

View Source

MaybeFirstWhere<T>(IEnumerable<T>, Func<T, Boolean>)

Gets a Maybe<T> containing the first matching member of the collection, if it exists.

Declaration
public static Maybe<T> MaybeFirstWhere<T>(this IEnumerable<T> inCollection, Func<T, bool> inPredicate)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> inCollection

A collection in which to find the first matching member.

System.Func<T, System.Boolean> inPredicate

The condition that a collection member must satisfy.

Returns
Type Description
Maybe<T>

A Maybe<T> containing the first member if it exists.

Type Parameters
Name Description
T

The type of the collection members.

View Source

MaybeSingle<T>(IEnumerable<T>)

Returns the only element of a sequence, or an empty Maybe<T> if the sequence is empty; this method throws an exception if there is more than one element in the sequence.

Declaration
public static Maybe<T> MaybeSingle<T>(this IEnumerable<T> inCollection)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> inCollection

A collection in which to find a single member.

Returns
Type Description
Maybe<T>

The single element of the input sequence, or an empty Maybe<T> if the sequence contains no elements.

Type Parameters
Name Description
T

The type of the collection members.

View Source

MaybeSingle<T>(IEnumerable<T>, Func<T, Boolean>)

Returns the only element of a sequence that satisfies a specified condition, or an empty Maybe<T> if the sequence is empty; this method throws an exception if more than one such element exists.

Declaration
public static Maybe<T> MaybeSingle<T>(this IEnumerable<T> inCollection, Func<T, bool> inPredicate)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> inCollection

A collection in which to find a single member.

System.Func<T, System.Boolean> inPredicate

A function to test each element for a condition.

Returns
Type Description
Maybe<T>

The single element of the input sequence that satisfies a condition, or an empty Maybe<T> if the sequence contains no elements.

Type Parameters
Name Description
T

The type of the collection members.

View Source

ThatExist<T>(IEnumerable<T>)

Returns a collection containing only the members of the input collection that have values.

Declaration
public static IEnumerable<T> ThatExist<T>(this IEnumerable<T> inCollection)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> inCollection

The collection that may contain values.

Returns
Type Description
System.Collections.Generic.IEnumerable<T>

A collection containing only the members of the input collection that have values.

Type Parameters
Name Description
T

The type of members of the collection.

View Source

ThatExist<T>(IEnumerable<Maybe<T>>)

Returns a collection containing only the members of the input collection that have values.

Declaration
public static IEnumerable<T> ThatExist<T>(this IEnumerable<Maybe<T>> inCollection)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<Maybe<T>> inCollection

The collection that may contain values.

Returns
Type Description
System.Collections.Generic.IEnumerable<T>

A collection containing only the members of the input collection that have values.

Type Parameters
Name Description
T

The type of members of the collection.

Remarks

The implementation of this method looks identical to the more generic version ThatExist<T>(IEnumerable<T>) but it is required to transform a collection of Maybes to a collection of T.

View Source

WhereAllExist<T>(IEnumerable<Maybe<T>>)

Returns a collection only if all the members of the input collection have values.

Declaration
public static Maybe<IEnumerable<T>> WhereAllExist<T>(this IEnumerable<Maybe<T>> inCollection)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<Maybe<T>> inCollection

The collection that may contain values.

Returns
Type Description
Maybe<System.Collections.Generic.IEnumerable<T>>

The input collection if all its members exist; otherwise no collection.

Type Parameters
Name Description
T

The type of members of the collection.

  • View Source
In This Article
Back to top Copyright © 2021 Dr. Gavin T.D. Greig.