5using System.Collections.Generic;
15#if !CSX_MAYBE_INTERNAL
29#if !CSX_MAYBE_INTERNAL
32 internal abstract class Maybe<T>
44#region Basic Match Methods
51 value =
Tag == MaybeType.Just ? ((
Just<T>)
this).Value :
default;
72#if !CSX_MAYBE_INTERNAL
84#if !CSX_MAYBE_INTERNAL
104#if !CSX_MAYBE_INTERNAL
107 internal static class Maybe
109#region Value Case Constructors
146 return maybe.MatchJust(out value1) ? func(value1) :
Nothing<T2>();
160 return maybe.MatchJust(out value1) ?
Just(func(value1)) :
Nothing<T2>();
173 if (first.MatchJust(out value1) && second.MatchJust(out value2))
175 return Just(Tuple.Create(value1, value2));
181#if !CSX_REM_EITHER_FUNC
190 return Maybe.Nothing<TRight>();
198#if !CSX_MAYBE_INTERNAL
203#region Alternative Match Methods
212 if (maybe.MatchJust(out value))
225 public static void Match<T1, T2>(
this Maybe<Tuple<T1, T2>> maybe, Action<T1, T2> ifJust, Action ifNothing)
230 if (maybe.MatchJust(out value1, out value2))
232 ifJust(value1, value2);
247 if (maybe.MatchJust(out value))
249 value1 = value.Item1;
250 value2 = value.Item2;
269 return Maybe.Return(value);
277 return Maybe.Bind(maybe, func);
285 return Maybe.Map(maybe, func);
288#region Linq Operators
294 Func<TSource, TResult> selector)
296 return Maybe.Map(maybe, selector);
304 Func<TSource, TValue, TResult> resultSelector)
308 valueSelector(sourceValue)
309 .Map(resultValue => resultSelector(sourceValue, resultValue))
324 if (maybe.MatchJust(out value))
338 if (maybe.MatchJust(out value1, out value2))
340 action(value1, value2);
370 if (maybe.MatchJust(out value))
386 if (maybe.MatchJust(out value))
391 throw exceptionToThrow ??
new ArgumentException(
"Value empty.");
401 return maybe.MatchJust(out value) ? value : noneValue;
411 return maybe.MatchJust(out value1) ? func(value1) : noneValue;
422 return maybe.MatchJust(out value1) ? func(value1) : noneValueFactory();
433 if (maybe.MatchJust(out value))
435 return Enumerable.Empty<T>()
436 .Concat(
new[] { value });
439 return Enumerable.Empty<T>();
Models a CSharpx.Maybe when contains a value.
T Value
The wrapped value.
Provides convenience extension methods for CSharpx.Maybe.
static void Match< T1, T2 >(this Maybe< Tuple< T1, T2 > > maybe, Action< T1, T2 > ifJust, Action ifNothing)
Provides pattern matching using System.Action delegates over maybe with tupled wrapped value.
static T FromJust< T >(this Maybe< T > maybe)
Extracts the element out of a CSharpx.Just<T> and returns a default value if its argument is CSharpx....
static Maybe< TResult > Select< TSource, TResult >(this Maybe< TSource > maybe, Func< TSource, TResult > selector)
Map operation compatible with Linq.
static void Do< T1, T2 >(this Maybe< Tuple< T1, T2 > > maybe, Action< T1, T2 > action)
If contans a value executes an System.Action<T1, T2> delegate over it.
static Maybe< TResult > SelectMany< TSource, TValue, TResult >(this Maybe< TSource > maybe, Func< TSource, Maybe< TValue > > valueSelector, Func< TSource, TValue, TResult > resultSelector)
Bind operation compatible with Linq.
static void Do< T >(this Maybe< T > maybe, Action< T > action)
If contains a value executes an System.Action<T> delegate over it.
static void Match< T >(this Maybe< T > maybe, Action< T > ifJust, Action ifNothing)
Provides pattern matching using System.Action delegates.
static Maybe< T2 > Map< T1, T2 >(this Maybe< T1 > maybe, Func< T1, T2 > func)
Transforms this maybe value by using a specified mapping function.
static T GetValueOrDefault< T >(this Maybe< T > maybe, T noneValue)
If contains a values returns it, otherwise returns noneValue .
static IEnumerable< T > ToEnumerable< T >(this Maybe< T > maybe)
Returns an empty list when given CSharpx.Nothing<T> or a singleton list when given a CSharpx....
static Maybe< T2 > Bind< T1, T2 >(this Maybe< T1 > maybe, Func< T1, Maybe< T2 > > func)
Invokes a function on this maybe value that itself yields a maybe.
static bool IsJust< T >(this Maybe< T > maybe)
Returns true iffits argument is of the form CSharpx.Just<T>.
static bool IsNothing< T >(this Maybe< T > maybe)
Returns true iffits argument is of the form CSharpx.Nothing<T>.
static Maybe< T > ToMaybe< T >(this T value)
Equivalent to monadic CSharpx.Maybe.Return<T> operation. Builds a CSharpx.Just<T> value in case value...
static T2 MapValueOrDefault< T1, T2 >(this Maybe< T1 > maybe, Func< T1, T2 > func, T2 noneValue)
If contains a values executes a mapping function over it, otherwise returns noneValue .
static bool MatchJust< T1, T2 >(this Maybe< Tuple< T1, T2 > > maybe, out T1 value1, out T2 value2)
Matches a value returning true and tupled value itself via two output parameters.
static T FromJustOrFail< T >(this Maybe< T > maybe, Exception exceptionToThrow=null)
Extracts the element out of a CSharpx.Just<T> and throws an error if its argument is CSharpx....
The Maybe type models an optional value. A value of type Maybe a either contains a value of type a (r...
static Maybe< T2 > Bind< T1, T2 >(Maybe< T1 > maybe, Func< T1, Maybe< T2 > > func)
Sequentially compose two actions, passing any value produced by the first as an argument to the secon...
MaybeType Tag
Type discriminator.
static Maybe< T2 > Map< T1, T2 >(Maybe< T1 > maybe, Func< T1, T2 > func)
Transforms an maybe value by using a specified mapping function.
static Maybe< TRight > FromEither< TLeft, TRight >(Either< TLeft, TRight > either)
Maps Either Right value to Maybe Just, otherwise Maybe Nothing.
static Maybe< T > Return< T >(T value)
Inject a value into the monadic CSharpx.Maybe<T> type.
static Just< T > Just< T >(T value)
Builds the case when CSharpx.Maybe contains a value.
bool MatchNothing()
Matches an empty value returning true.
bool MatchJust(out T value)
Matches a value returning true and value itself via output parameter.
static Maybe< Tuple< T1, T2 > > Merge< T1, T2 >(Maybe< T1 > first, Maybe< T2 > second)
If both maybes contain a value, it merges them into a maybe with a tupled value.
static Maybe< T > Nothing< T >()
Builds the empty case of CSharpx.Maybe.
Models a CSharpx.Maybe when in empty state.
MaybeType
Discriminator for CSharpx.Maybe.