Show / Hide Table of Contents

Class NumericOptionExtensions

Extension methods for the Option<T> type.

Inheritance
object
NumericOptionExtensions
Inherited Members
object.Equals(object)
object.Equals(object, object)
object.GetHashCode()
object.GetType()
object.MemberwiseClone()
object.ReferenceEquals(object, object)
object.ToString()
Namespace: RustyOptions
Assembly: RustyOptions.dll
Syntax
public static class NumericOptionExtensions

Methods

| Edit this page View Source

AndThen<T1, T2>(NumericOption<T1>, Func<T1, NumericOption<T2>>)

Returns None if the option is None, otherwise calls thenFn with the wrapped value and returns the result.

Declaration
public static NumericOption<T2> AndThen<T1, T2>(this NumericOption<T1> self, Func<T1, NumericOption<T2>> thenFn) where T1 : struct, INumber<T1> where T2 : struct, INumber<T2>
Parameters
Type Name Description
NumericOption<T1> self

The first option.

Func<T1, NumericOption<T2>> thenFn

The function to call with the contained value, if there is a contained value.

Returns
Type Description
NumericOption<T2>
Type Parameters
Name Description
T1

The type contained by the first option.

T2

The type contained by the second option.

Exceptions
Type Condition
ArgumentNullException

Thrown if thenFn is null.

| Edit this page View Source

And<T1, T2>(NumericOption<T1>, NumericOption<T2>)

Returns None if self is None, otherwise returns other.

Declaration
public static NumericOption<T2> And<T1, T2>(this NumericOption<T1> self, NumericOption<T2> other) where T1 : struct, INumber<T1> where T2 : struct, INumber<T2>
Parameters
Type Name Description
NumericOption<T1> self

The first option.

NumericOption<T2> other

The second option.

Returns
Type Description
NumericOption<T2>
Type Parameters
Name Description
T1

The type contained by the first option.

T2

The type contained by the second option.

| Edit this page View Source

Expect<T>(NumericOption<T>, string)

Returns the contained Some value, or throws an InvalidOperationException if the value is None.

Declaration
public static T Expect<T>(this NumericOption<T> self, string message) where T : struct, INumber<T>
Parameters
Type Name Description
NumericOption<T> self

The option to unwrap.

string message

The message for the exception that gets thrown if the option has no value.

Returns
Type Description
T

The value contained in the option.

Type Parameters
Name Description
T

The type of the option.

Exceptions
Type Condition
InvalidOperationException

Thrown if the option does not contain a value.

| Edit this page View Source

Filter<T>(NumericOption<T>, Func<T, bool>)

Returns None if the option is None, otherwise calls predicate and returns Some if the predicated returns true, otherwise None.

Declaration
public static NumericOption<T> Filter<T>(this NumericOption<T> self, Func<T, bool> predicate) where T : struct, INumber<T>
Parameters
Type Name Description
NumericOption<T> self

The option to check.

Func<T, bool> predicate

The function that determines if the value in the option is valid to return.

Returns
Type Description
NumericOption<T>

Some if the option is Some and the predicate returns true, otherwise None.

Type Parameters
Name Description
T

The type of the value.

Exceptions
Type Condition
ArgumentNullException

Thrown if predicate is null.

| Edit this page View Source

Flatten<T>(NumericOption<NumericOption<T>>)

Removes one level of nesting from nested options.

Declaration
public static NumericOption<T> Flatten<T>(this NumericOption<NumericOption<T>> self) where T : struct, INumber<T>
Parameters
Type Name Description
NumericOption<NumericOption<T>> self

The nested option to flatten.

Returns
Type Description
NumericOption<T>

The given option with one level of nesting removed.

Type Parameters
Name Description
T

The type of the value.

| Edit this page View Source

MapOrElse<T1, T2>(NumericOption<T1>, Func<T1, T2>, Func<T2>)

Computes a default function result (if None), or applies a different function to the contained value (if Some).

Declaration
public static T2 MapOrElse<T1, T2>(this NumericOption<T1> self, Func<T1, T2> mapper, Func<T2> defaultFactory) where T1 : struct, INumber<T1> where T2 : notnull
Parameters
Type Name Description
NumericOption<T1> self

The option to map.

Func<T1, T2> mapper

The function that maps the value contained in the option.

Func<T2> defaultFactory

The function that lazily generates a default value, if required.

Returns
Type Description
T2

The mapped value, or the default value.

Type Parameters
Name Description
T1

The type of the option's value.

T2

The return type after mapping.

Exceptions
Type Condition
ArgumentNullException

Thrown if mapper or defaultFactory is null.

| Edit this page View Source

MapOr<T1, T2>(NumericOption<T1>, Func<T1, T2>, T2)

Returns the provided default result (if None), or applies a function to the contained value (if Some).

Arguments passed to MapOr<T1, T2>(NumericOption<T1>, Func<T1, T2>, T2) are eagerly evaluated; if you are passing the result of a function call, it is recommended to use MapOrElse<T1, T2>(NumericOption<T1>, Func<T1, T2>, Func<T2>), which is lazily evaluated.

Declaration
public static T2 MapOr<T1, T2>(this NumericOption<T1> self, Func<T1, T2> mapper, T2 defaultValue) where T1 : struct, INumber<T1> where T2 : notnull
Parameters
Type Name Description
NumericOption<T1> self

The option to map.

Func<T1, T2> mapper

The function that maps the value contained in the option.

T2 defaultValue

The default value to return if the option is None.

Returns
Type Description
T2

The mapped value, or the default value.

Type Parameters
Name Description
T1

The type of the option's value.

T2

The return type after mapping.

Exceptions
Type Condition
ArgumentNullException

Thrown if mapper is null.

| Edit this page View Source

Map<T1, T2>(NumericOption<T1>, Func<T1, T2>)

If the option has a value, passes that option to the mapper function and returns that value as a Some.

Declaration
public static NumericOption<T2> Map<T1, T2>(this NumericOption<T1> self, Func<T1, T2> mapper) where T1 : struct, INumber<T1> where T2 : struct, INumber<T2>
Parameters
Type Name Description
NumericOption<T1> self

The option to map.

Func<T1, T2> mapper

The function that maps the value contained in the option.

Returns
Type Description
NumericOption<T2>

The mapped value as Some, or None.

Type Parameters
Name Description
T1

The type of the option.

T2

The type returned by the mapper functions.

Exceptions
Type Condition
ArgumentNullException

Thrown if mapper is null.

| Edit this page View Source

NoneNumeric<T>(T)

Returns None option typed to match value. The value is not used except to determine the type of the option.

Declaration
public static NumericOption<T> NoneNumeric<T>(this T value) where T : struct, INumber<T>
Parameters
Type Name Description
T value

The value used to determine the type of the returned None option. The value itself is not used.

Returns
Type Description
NumericOption<T>

A None option.

Type Parameters
Name Description
T

The type used in the returned NumericOption<T>.

| Edit this page View Source

OrElse<T>(NumericOption<T>, Func<NumericOption<T>>)

Returns self if it contains a value, otherwise calls elseFn and returns the result.

Declaration
public static NumericOption<T> OrElse<T>(this NumericOption<T> self, Func<NumericOption<T>> elseFn) where T : struct, INumber<T>
Parameters
Type Name Description
NumericOption<T> self

The option.

Func<NumericOption<T>> elseFn

The function that creates the alternate value if the option is None.

Returns
Type Description
NumericOption<T>
Type Parameters
Name Description
T

The type contained by the option.

Exceptions
Type Condition
ArgumentNullException

Thrown if elseFn is null.

| Edit this page View Source

Or<T>(NumericOption<T>, NumericOption<T>)

Returns self if it contains a value, otherwise returns other.

Arguments passed to or are eagerly evaluated; if you are passing the result of a function call, it is recommended to use OrElse<T>(NumericOption<T>, Func<NumericOption<T>>), which is lazily evaluated.

Declaration
public static NumericOption<T> Or<T>(this NumericOption<T> self, NumericOption<T> other) where T : struct, INumber<T>
Parameters
Type Name Description
NumericOption<T> self

The first option.

NumericOption<T> other

The replacement option to use if the first option is None.

Returns
Type Description
NumericOption<T>
Type Parameters
Name Description
T

The type contained by the option.

| Edit this page View Source

SomeNumeric<T>(T)

Wraps the given number in a NumericOption<T>.

Declaration
public static NumericOption<T> SomeNumeric<T>(this T value) where T : struct, INumber<T>
Parameters
Type Name Description
T value

The value that will be contained in the NumericOption.

Returns
Type Description
NumericOption<T>

The value wrapped in an NumericOption<T>

Type Parameters
Name Description
T

The type used in the returned NumericOption<T>.

| Edit this page View Source

UnwrapOrElse<T>(NumericOption<T>, Func<T>)

Returns the contained Some value or computes a default using the provided defaultFactory.

Declaration
public static T UnwrapOrElse<T>(this NumericOption<T> self, Func<T> defaultFactory) where T : struct, INumber<T>
Parameters
Type Name Description
NumericOption<T> self

The option to unwrap.

Func<T> defaultFactory

A function that returns a default value to use if the option is None.

Returns
Type Description
T
Type Parameters
Name Description
T

The type of the option.

Exceptions
Type Condition
ArgumentNullException

Thrown if defaultFactory is null.

| Edit this page View Source

UnwrapOr<T>(NumericOption<T>, T)

Returns the contained Some value or a provided default.

Declaration
public static T UnwrapOr<T>(this NumericOption<T> self, T defaultValue) where T : struct, INumber<T>
Parameters
Type Name Description
NumericOption<T> self

The option to bind.

T defaultValue

The default value to return if the option is None.

Returns
Type Description
T
Type Parameters
Name Description
T

The type of the option.

| Edit this page View Source

Values<T>(IEnumerable<NumericOption<T>>)

Flattens a sequence of NumericOption<T> into a sequence containing all inner values. Empty elements are discarded.

Declaration
public static IEnumerable<T> Values<T>(this IEnumerable<NumericOption<T>> self) where T : struct, INumber<T>
Parameters
Type Name Description
IEnumerable<NumericOption<T>> self

The sequence of options.

Returns
Type Description
IEnumerable<T>

A flattened sequence of values.

Type Parameters
Name Description
T
| Edit this page View Source

Xor<T>(NumericOption<T>, NumericOption<T>)

Returns Some if exactly one of self, other is Some, otherwise returns None.

Declaration
public static NumericOption<T> Xor<T>(this NumericOption<T> self, NumericOption<T> other) where T : struct, INumber<T>
Parameters
Type Name Description
NumericOption<T> self

The option.

NumericOption<T> other

The other option.

Returns
Type Description
NumericOption<T>
Type Parameters
Name Description
T

The type contained by the option.

| Edit this page View Source

ZipWith<T1, T2, T3>(NumericOption<T1>, NumericOption<T2>, Func<T1, T2, T3>)

Zips this Option and another Option with function zipper.

If this option is Some(s) and other is Some(o), this method returns Some(zipper(s, o)). Otherwise, None is returned.

Declaration
public static NumericOption<T3> ZipWith<T1, T2, T3>(this NumericOption<T1> self, NumericOption<T2> other, Func<T1, T2, T3> zipper) where T1 : struct, INumber<T1> where T2 : struct, INumber<T2> where T3 : struct, INumber<T3>
Parameters
Type Name Description
NumericOption<T1> self

The first option.

NumericOption<T2> other

The second option.

Func<T1, T2, T3> zipper

A functon that combines values from the two options into a new type.

Returns
Type Description
NumericOption<T3>

An option contianing the result of passing both values to the zipper function, or None.

Type Parameters
Name Description
T1

The type contained by the first option.

T2

The type contained by the second option.

T3

The type returned by the zipper function.

Exceptions
Type Condition
ArgumentNullException

Thrown if zipper is null.

  • Edit this page
  • View Source
In this article
Back to top Generated by DocFX