public static MaybeNotNull <K> SelectMany <T, K>(this MaybeNotNull <T> source, Func <T, K> maybeSelector) { //Contract.Requires( source != null ); //Contract.Requires( maybeSelector != null ); //Contract.Ensures( //Contract.Result<MaybeNotNull<K>>() != null ); return(source.SelectMany(maybeSelector, (_, x) => x)); }
public static MaybeNotNull <R> SelectMany <T, K, R>(this MaybeNotNull <T> source, Func <T, K> maybeSelector, Func <T, K, R> resultSelector) { //Contract.Requires( source != null ); //Contract.Requires( maybeSelector != null ); //Contract.Requires( resultSelector != null ); return(source.SelectMany(t => Value(maybeSelector(t)), resultSelector)); }
public static MaybeNotNull <T> Catch <T, TException>(this MaybeNotNull <T> source, Func <TException, T> getValue) where TException : Exception { //Contract.Requires( source != null ); //Contract.Ensures( //Contract.Result<MaybeNotNull<T>>() != null ); return(source.Catch((TException ex) => Maybe.Value(getValue(ex)))); }
public static MaybeNotNull <T> Catch <T>(this MaybeNotNull <T> source, MaybeNotNull <T> defaultValue) { //Contract.Requires( source != null ); //Contract.Requires( defaultValue != null ); //Contract.Ensures( //Contract.Result<MaybeNotNull<T>>() != null ); return(source.Kind == MaybeKind.Error ? defaultValue : source); }
public static MaybeNotNull <T> Where <T>(this MaybeNotNull <T> source, Func <T, bool> predicate) { //Contract.Requires( source != null ); //Contract.Requires( predicate != null ); //Contract.Ensures( //Contract.Result<MaybeNotNull<T>>() != null ); return(from t in source from _ in predicate(t) ? "" : null select t); }
public static OpResult Lift(this MaybeNotNull <OpResult> maybe, OpResult nullResult = null) { //Contract.Requires( maybe != null ); //Contract.Ensures( //Contract.Result<OpResult>() != null ); return (maybe.Kind == MaybeKind.Error ? OpResult.FromException(maybe.Exception) : (maybe.ValueOrNull() ?? nullResult ?? OpResult.Failed("Unknown error has occurred"))); }
public static MaybeNotNull <T> OrMaybe <T>(this MaybeNotNull <T> source, Func <MaybeNotNull <T> > whenNull) { //Contract.Requires( source != null ); //Contract.Requires( whenNull != null ); if (source.Kind == MaybeKind.Null) { return(whenNull()); } return(source); }
public static MaybeNotNull <T> Catch <T, TException>(this MaybeNotNull <T> source, Func <TException, MaybeNotNull <T> > getValue) where TException : Exception { //Contract.Requires( source != null ); //Contract.Ensures( //Contract.Result<MaybeNotNull<T>>() != null ); if (source.Kind == MaybeKind.Error) { var e = source.Exception as TException; if (e != null) { return(getValue(e)); } } return(source); }
public override MaybeNotNull <R> SelectMany <K, R>(Func <T, MaybeNotNull <K> > maybeSelector, Func <T, K, R> resultSelector) { try { var k = maybeSelector(this.Value); if (k.Kind == MaybeKind.Value) { var res = resultSelector(this.Value, k.Value); return(isNull(res) ? MaybeNotNull <R> .MakeNull() : MaybeNotNull <R> .MakeValue(res)); } else { return(k.SelectMany <R, R>(null, null)); } } catch (Exception ex) { return(MaybeNotNull <R> .MakeError(ex)); } }
public static T ValueOrDefault <T>(this MaybeNotNull <T> maybe, T defaultValue) { //Contract.Requires( maybe != null ); return(maybe.Kind == MaybeKind.Null ? defaultValue : maybe.Value); }
public static MaybeNotNull <T> Or <T>(this MaybeNotNull <T> source, Func <T> whenNull) { //Contract.Requires( source != null ); //Contract.Requires( whenNull != null ); return(source.OrMaybe(() => Maybe.Value(whenNull()))); }
public static MaybeNotNull <T> Value <T>(T value) { return(value == null?Null <T>() : MaybeNotNull <T> .MakeValue(value)); }
public static T?AsNullable <T>(this MaybeNotNull <T?> source) where T : struct { //Contract.Requires( source != null ); return(source.Kind == MaybeKind.Null ? new T?() : source.Value); }
public static MaybeNotNull <T> Throw <T>(Exception ex) { return(MaybeNotNull <T> .MakeError(ex)); }
public static MaybeNotNull <T> Do <T>(this MaybeNotNull <T> source, Action <T> a) { //Contract.Requires( source != null ); //Contract.Requires( a != null ); return(source.Select(x => { a(x); return x; })); }
public static MaybeNotNull <T> Catch <T>(this MaybeNotNull <T> source, T defaultValue) { //Contract.Requires( source != null ); //Contract.Ensures( //Contract.Result<MaybeNotNull<T>>() != null ); return(source.Catch(Maybe.Value(defaultValue))); }
public static T?ValueOrNull <T>(this MaybeNotNull <T?> maybe) where T : struct { //Contract.Requires( maybe != null ); return(maybe.Kind == MaybeKind.Null ? null : maybe.Value); }
public static T ValueOrDefault <T>(this MaybeNotNull <T> maybe) where T : struct { //Contract.Requires( maybe != null ); return(maybe.Kind == MaybeKind.Null ? default(T) : maybe.Value); }
public static MaybeNotNull <T> Null <T>() { return(MaybeNotNull <T> .MakeNull()); }