Maybe API docs
March 15, 2020 ยท View on GitHub
This is the API documentation for the JFlepp.Maybe library. Please note that the implementations do not show the actual implementation, but rather a reference to look up the behavior.
Type: Maybe
Representation of a maybe value.
Method: Construct
Constructs a some value using the given value.
Parameters
value: The value of the maybe.
Exceptions
System.ArgumentNullException: Thrown value isnull.
Maybe
public Maybe(T value)
{
if (value == null) throw new ArgumentNullException(nameof(value));
(Value, IsSome) = (value, true);
}
Property: IsSome
Gets the type of the Maybe. Returns true if is some, false otherwise.
Property: IsNone
Gets the type of the Maybe. Returns true if is none, false otherwise.
Method: Equals(System.Object)
Indicates whether the current object is equal to another object of the same type.
Parameters
obj: An object to compare with this object.
Returns: true if the current object is equal to the other parameter; otherwise, false.
Maybe implementation
bool Equals(object obj) => obj is Maybe<T> otherMaybe ? Equals(otherMaybe) : false;
Method: GetHashCode()
Serves as the default hash function.
Returns: A hash code for the current object.
Maybe implementation
int GetHashCode()
{
unchecked
{
return -1584136870 + EqualityComparer<T>.Default.GetHashCode(Value);
}
}
Method: Equals(Maybe)
Indicates whether the current maybes value is equal to another maybes value.
Parameters
other: Another maybe to compare with this object.
Returns: true if the current maybe is equal to the other parameter; otherwise, false.
Maybe implementation
bool Equals(Maybe<T> other)
{
if (IsNone) return other.IsNone;
if (other.IsNone) return false;
return Value.Equals(other.Value);
}
Operator: Maybe == Maybe
Checks if two Maybe<T>s are equal.
Parameters
right: The rightMaybe<T>to compare.left: The leftMaybe<T>to compare.
Returns: true if objects are equal, false otherwise.
Maybe implementation
bool operator ==(Maybe<T> left, Maybe<T> right) => left.Equals(right);
Operator: Maybe != Maybe
Checks if two Maybe<T>s are not equal.
Parameters
right: The rightMaybe<T>to compare.left: The leftMaybe<T>to compare.
Returns: true if objects are not equal, false otherwise.
Maybe implementation
bool operator !=(Maybe<T> left, Maybe<T> right) => !(left == right);
Type: Maybe
Provides functions for creating Maybe<T>s.
Method: None()
Creates a new none instance of Maybe<1>
Returns: A new none instance of Maybe<T>
Method: Some(T value)
Creates a new some instance of Maybe<T>.
Parameters
value: The value of theMaybe<T>.
Returns: A new some instance of Maybe<T>.
Method: FromObject(T value)
Convert a potentially null value to an option.
Parameters
value: The input value.
Returns: The result option.
FSharp implementation
let ofObj value = match value with null -> None | _ -> Some value
// val ofObj : value:'a -> 'a option when 'a : null
Maybe implementation
Maybe<T> OfObject<T>(T value) => (value == null) switch
{
true => Some(value),
_ => None<T>(),
};
Type: MaybeExtensions
Provides extension methods for working with the Maybe<T> type.
Method: Aggregate<T, TState>(Maybe input, TState state, Func<TState, T, TState> folder)
Applies the supplied function with the supplied initial state and the value of the Maybe<T> if is Some.
Parameters
input: The inputMaybe<T>.seed: The initial state.func: A function to update the state data when given a value from an option.
Returns: The original state if the Maybe is None, otherwise it returns the updated state with the folder and the Maybe<T> value.
FSharp implementation
let fold{'T,'State} folder (state:'State) (option: option{'T}) = match option with None -> state | Some x -> folder state x
// val fold :
// folder:('State -> 'T -> 'State) ->
// state:'State -> option:'T option -> 'State
Maybe implementation
TState Aggregate<T, TState>(Maybe<T> input, TState state, Func<TState, T, TState> folder) => input.IsNone switch
{
true => folder(state, input.Value),
_ => state,
};
Method: All(Maybe input, Predicate predicate)
Evaluates the equivalent of System.Linq.Enumerable.All for a Maybe<T>.
Parameters
input: The inputMaybe<T>.predicate: A function that evaluates to aboolwhen given a value from theMaybe<T>.
Returns: true if the maybe is None, otherwise it returns the result of applying the predicate to the Maybe<T> value.
FSharp implementation
let forall predicate option = match option with None -> true | Some x -> predicate x
// val forall : predicate:('a -> bool) -> option:'a option -> bool
Maybe implementation
bool All<T>(Maybe<T> input, Predicate<T> predicate) => input.IsNone switch
{
true => predicate(input.Value),
_ => true,
};
Method: Any(Maybe input, Predicate predicate)
Evaluates the equivalent of System.Linq.Enumerable.Any for a Maybe<T>.
Parameters
input: The inputMaybe<T>.predicate: A function that evaluates to a Boolean when given a value from theMaybe<T>.
Returns: Returns false if the Maybe<T> is None, otherwise it returns the result of applying the predicate to the Maybe<T>s value.
FSharp implementation
let exists predicate option = match option with None -> false | Some x -> predicate x
// val exists : predicate:('a -> bool) -> option:'a option -> bool
Maybe implementation
bool Any<T>(Maybe<T> input, Predicate<T> predicate) => input.IsSome switch
{
true => predicate(input.Value),
_ => false,
};
Method: Bind<T, TOut>(Maybe input, Func<T, Maybe> binder)
Invokes a function on a Maybe<T> value that itself yields an option.
Parameters
input: The inputMaybe<T>.binder: A function that takes the value of type T from aMaybe<T>and transforms it into aMaybe<T>containing a value of typeTOut.
Returns: A maybe of the output type of the binder.
FSharp implementation
let bind binder option = match option with None -> None | Some x -> binder x
// val bind : binder:('a -> 'b option) -> option:'a option -> 'b option
Maybe implementation
Maybe<TOut> Bind<T, TOut>(Maybe<T> input, Func<T, Maybe<TOut>> binder) => input.IsSome switch
{
true => binder(input.Value),
_ => None{TOut}(),
};
Method: Contains(Maybe input, T value)
Evaluates to true if input is Some and its value is equal to value.
Parameters
input: The input maybe.value: The value to test for equality.
Returns: true if the Maybe<T> is Some and contains a value equal to value, otherwise false.
FSharp implementation
let inline contains value option = match option with None -> false | Some v -> v = value
// let inline contains value option = match option with None -> false | Some v -> v = value
Maybe implementation
bool Contains<T>(Maybe<T> input, T value) => input.IsSome switch
{
true => input.Value.Equals(value),
_ => false,
};
Method: Count(Maybe input)
Evaluates the equivalent of System.Linq.Enumerable.Count for a Maybe<T>.
Parameters
input: The inputMaybe<T>.
Returns: A zero if the Maybe<T> is None, a one otherwise.
FSharp implementation
count option = match option with None -> 0 | Some _ -> 1
// val count : option:'a option -> int
Maybe implementation
int Count<T>(Maybe<T> input) => input.IsSome switch
{
true => 1,
_ => 0,
};
Method: ForEach(Maybe input, Action action)
Executes a function for a Maybe value if is Some.
Parameters
input: The inputMaybe<T>.action: A function to apply to theMaybevalue.
FSharp implementation
let iter action option = match option with None -> () | Some x -> action x
// val iter : action:('a -> unit) -> option:'a option -> unit
Maybe implementation
void ForEach<T>(Maybe<T> input, Action<T> action)
{
if (input.IsSome) action(input.Value);
}
Method: OrElse(Maybe input, T ifNone)
Returns input if it is Some, otherwise returns ifNone.
Parameters
input: The input option.ifNone: The value to use ifinputisNone.
Returns: The input if the maybe is Some, else the alternate value.
FSharp implementation
let orElse ifNone option = match option with None -> ifNone | Some _ -> option
// val orElse : ifNone:'a option -> option:'a option -> 'a option
Maybe implementation
Maybe<T> OrElse<T>(Maybe<T> input, T ifNone) => input.IsSome switch
{
true => input,
_ => Maybe.Some(ifNone),
};
Method: OrElse(Maybe input, Func ifNoneThunk)
Returns input if it is Some, otherwise evaluates ifNoneThunk and returns the result.
ifNoneThunk is not evaluated unless input is None.
Parameters
input: The input option.ifNoneThunk: A thunk that provides an alternate option when evaluated.
Returns: The input if the maybe is Some, else the alternate value.
FSharp implementation
let orElseWith ifNoneThunk option = match option with None -> ifNoneThunk () | Some _ -> option
// val orElseWith : ifNoneThunk:(unit -> 'a option) -> option:'a option -> 'a option
Maybe implementation
Maybe<T> OrElse<T>(Maybe<T> input, Func<T> ifNoneThunk) => input.IsSome switch
{
true => input,
_ => Maybe.Some(ifNoneThunk()),
};
Method: Select<T, TOut>(Maybe input, Func<T, TOut> mapping)
Transforms a Maybe<T> value by using a specified mapping function.
Parameters
input: The inputMaybe<T>.mapping: A function to apply to theMaybe<T>value.
Returns: A Maybe<T> of the result of applying the mapping function, or None if the Maybe<T> is None.
FSharp implementation
let map mapping option = match option with None -> None | Some x -> Some (mapping x)
// val map : mapping:('a -> 'b) -> option:'a option -> 'b option
Maybe implementation
Maybe<TOut> Select<T, TOut>(Maybe<T> input, Func<T, TOut> mapping) => input.IsSome switch
{
true => Some(mapping(input.Value)),
_ => None{TOut}()
};
Method: SingleOr(Maybe input, T or)
Gets the value of the maybe if the option is Some, otherwise returns the specified default value.
Parameters
input: The input maybe.or: The specified default value.
Returns: The option if the option is Some, else the default value.
FSharp implementation
let defaultValue value option = match option with None -> value | Some v -> v
// val defaultValue : value:'a -> option:'a option -> 'a
Maybe implementation
T SingleOr<T>(Maybe<T> input, T or) => input.IsSome switch
{
true => input.Value,
_ => or,
};
Method: SingleOr(Maybe input, Func or)
Gets the value of the option if the maybe is Some, otherwise evaluates or and returns the result.
Parameters
input: The input maybe.or: A thunk that provides a default value when evaluated.
Returns: The maybe if the maybe is Some, else the result of evaluating or.
FSharp implementation
let defaultWith defThunk option = match option with None -> defThunk () | Some v -> v
// val defaultWith : defThunk:(unit -> 'a) -> option:'a option -> 'a
Maybe implementation
T SingleOr<T>(Maybe<T> input, Func<T> or) => input.IsSome switch
{
true => input.Value,
_ => or.ThrowIfNull(nameof(or))(),
};
Method: ToArray(Maybe input)
Convert the Maybe<T> to an array of length 0 or 1.
Parameters
input: The inputMaybe<T>.
Returns: The result array.
FSharp implementation
let toArray option = match option with None -> [| |] | Some x -> [| x |]
// val toArray : option:'a option -> 'a []
Maybe implementation
T[] ToArray<T>(Maybe<T> input) => input.IsSome switch
{
true => new[] { input.Value },
_ => Array.Empty{T}(),
};
Method: ToList(Maybe input)
Convert the Maybe<T> to a list of length 0 or 1.
Parameters
input: The inputMaybe<T>.
Returns: The result list.
FSharp implementation
let toList option = match option with None -> [ ] | Some x -> [ x ]
// let toList option = match option with None -> [ ] | Some x -> [ x ]
Maybe implementation
List<T> ToList<T>(Maybe<T> input) => input.IsSome switch
{
true => new List{T} { input.Value },
_ => new List{T}(),
};
Method: ToObject(Maybe input)
Convert an Maybe<T> to a potentially null value.
Parameters
inputThe input value.
Returns: The result value, which is null if the input was None.
FSharp implementation
let toObj value = match value with None -> null | Some x -> x
// val toObj : value:'a option -> 'a when 'a : null
Maybe implementation
object ToObject<T>(Maybe<T> input) => input.IsSome switch
{
true => input.Value,
_ => null,
};
Method: Where(Maybe input, Predicate predicate)
Invokes a function on a Maybe<T> that itself yields an option.
Parameters
input: The inputMaybe<T>.predicateA function that evaluates whether the value contained in theMaybe<T>should remain, or be filtered out.
Returns: The input if the predicate evaluates to true; otherwise, None.
FSharp implementation
let filter predicate option = match option with None -> None | Some x -> if predicate x then Some x else None
// val filter : predicate:('a -> bool) -> option:'a option -> 'a option
Maybe implementation
Maybe<T> Where<T>(Maybe<T> input, Predicate<T> predicate) => input.IsSome switch
{
true => predicate(input.Value) ? input : None{T}(),
_ => None{T}(),
};
Type: MaybeStructExtensions
Provides extensions for struct Maybes.
Method: ToNullable(Maybe input)
Convert the Maybe<T> to a Nullable value.
Returns: null if the Maybe<T> is None, the value otherwise.
FSharp implementation
let toNullable option = match option with None -> System.Nullable() | Some v -> System.Nullable(v)
// val toNullable :
// option:'a option -> System.Nullable{'a}
// when 'a : (new : unit -> 'a) and 'a : struct and 'a :> System.ValueType
Maybe implementation
T? ToNullable<T>(Maybe<T> input) where T : struct => input.IsSome switch
{
true => input.Value,
_ => null,
};
Type: MaybeReferenceExtensions
Provides extensions for reference Maybes.
Method: ToNullable(Maybe input)
Convert the Maybe<T> to a Nullable value.
Returns: null if the Maybe<T> is None, the value otherwise.
FSharp implementation
let toNullable option = match option with None -> System.Nullable() | Some v -> System.Nullable(v)
// val toNullable :
// option:'a option -> System.Nullable{'a}
// when 'a : (new : unit -> 'a) and 'a : struct and 'a :> System.ValueType
Maybe implementation
T ToNullable<T>(Maybe<T> input) where T : class => input.IsSome switch
{
true => input.Value,
_ => null,
};
Type: Unsafe.MaybeExtensions
Provides unsafe extensions for Maybe<T>s.
Method: GetValue(Maybe input)
Gets the value of the maybe if is Some.
Parameters
input: TheMaybe<T>to get the value from.
Returns: The value of the Maybe<T> if is Some.
Exceptions
System.NullReferenceException: If the maybe isNone.
FSharp implementation
let get option = match option with None -> invalidArg "option" (SR.GetString(SR.optionValueWasNone)) | Some x -> x
// val get: option:'T option -> 'T
Maybe implementation
T GetValue<T>(Maybe<T> input) => input.IsSome switch
{
true => input.Value,
_ => throw new NullReferenceException(),
};