Exemplo n.º 1
0
    public static OptionUnsafe <A> ToOptionUnsafe <A>(this TryOption <A> self)
    {
        var res = TryOptionExtensions.Try(self);

        return(res.IsFaulted
            ? None
            : res.Value.ToOptionUnsafe());
    }
Exemplo n.º 2
0
    public static async Task <R> MatchAsync <A, R>(this TryOption <A> self, Func <A, R> Some, Func <Task <R> > Fail)
    {
        var res = TryOptionExtensions.Try(self);

        return(await(res.IsFaulted || res.Value.IsNone
            ? Fail()
            : Task.FromResult(Some(res.Value.Value))));
    }
Exemplo n.º 3
0
    public static R Match <A, R>(this TryOption <A> self, Func <A, R> Some, Func <R> None, Func <Exception, R> Fail)
    {
        var res = TryOptionExtensions.Try(self);

        return(res.IsFaulted ? Fail(res.Exception)
             : res.Value.IsSome ? Some(res.Value.Value)
             : None());
    }
Exemplo n.º 4
0
    public static Seq <A> ToSeq <A>(this TryOption <A> self)
    {
        var res = TryOptionExtensions.Try(self);

        return(res.Value.IsSome
            ? res.Value.Value.Cons(Empty)
            : Empty);
    }
Exemplo n.º 5
0
    public static R Match <A, R>(this TryOption <A> self, Func <A, R> Some, Func <R> Fail)
    {
        var res = TryOptionExtensions.Try(self);

        return(res.IsFaulted || res.Value.IsNone
            ? Fail()
            : Some(res.Value.Value));
    }
Exemplo n.º 6
0
    public static S BiFold <A, S>(this TryOption <A> self, S state, Func <S, A, S> Some, Func <S, S> Fail)
    {
        var res = TryOptionExtensions.Try(self);

        return(res.IsFaulted || res.Value.IsNone
            ? Fail(state)
            : Some(state, res.Value.Value));
    }
Exemplo n.º 7
0
    public static bool Exists <A>(this TryOption <A> self, Func <A, bool> pred)
    {
        var res = TryOptionExtensions.Try(self);

        return(res.IsFaulted || res.Value.IsNone
            ? false
            : pred(res.Value.Value));
    }
Exemplo n.º 8
0
    public static S Fold <A, S>(this TryOption <A> self, S state, Func <S, A, S> folder)
    {
        var res = TryOptionExtensions.Try(self);

        return(res.IsFaulted || res.Value.IsNone
            ? state
            : folder(state, res.Value.Value));
    }
Exemplo n.º 9
0
    public static int Count <A>(this TryOption <A> self)
    {
        var res = TryOptionExtensions.Try(self);

        return(res.IsFaulted || res.Value.IsNone
            ? 0
            : 1);
    }
Exemplo n.º 10
0
    public static Either <L, Option <A> > ToEither <A, L>(this TryOption <A> self, Func <Exception, L> Fail)
    {
        var res = TryOptionExtensions.Try(self);

        return(res.IsFaulted
            ? Left <L, Option <A> >(Fail(res.Exception))
            : Right <L, Option <A> >(res.Value));
    }
Exemplo n.º 11
0
    public static EitherUnsafe <Exception, Option <A> > ToEitherUnsafe <A>(this TryOption <A> self)
    {
        var res = TryOptionExtensions.Try(self);

        return(res.IsFaulted
            ? LeftUnsafe <Exception, Option <A> >(res.Exception)
            : RightUnsafe <Exception, Option <A> >(res.Value));
    }
Exemplo n.º 12
0
    public static IObservable <R> MatchObservable <A, R>(this TryOption <A> self, Func <A, R> Some, Func <IObservable <R> > Fail)
    {
        var res = TryOptionExtensions.Try(self);

        return(res.IsFaulted || res.Value.IsNone
            ? Fail()
            : Observable.Return(Some(res.Value.Value)));
    }
Exemplo n.º 13
0
    public static IEnumerable <A> AsEnumerable <A>(this TryOption <A> self)
    {
        var res = TryOptionExtensions.Try(self);

        if (res.Value.IsSome)
        {
            yield return(res.Value.Value);
        }
    }
Exemplo n.º 14
0
    /// <summary>
    /// Invoke a delegate if the Try returns a value successfully
    /// </summary>
    /// <param name="Some">Delegate to invoke if successful</param>
    public static Unit IfSome <A>(this TryOption <A> self, Action <A> Some)
    {
        var res = TryOptionExtensions.Try(self);

        if (!res.IsFaulted && res.Value.IsSome)
        {
            Some(res.Value.Value);
        }
        return(unit);
    }
Exemplo n.º 15
0
    public static IObservable <R> MatchObservable <A, R>(this TryOption <A> self, Func <A, R> Some, Func <IObservable <R> > None, Func <Exception, IObservable <R> > Fail)
    {
        var res = TryOptionExtensions.Try(self);

        return(res.IsFaulted
            ? Fail(res.Exception)
            : res.Value.IsSome
                ? Observable.Return(Some(res.Value.Value))
                : None());
    }
Exemplo n.º 16
0
    public static S TriFold <A, S>(this TryOption <A> self, S state, Func <S, A, S> Some, Func <S, S> None, Func <S, Exception, S> Fail)
    {
        var res = TryOptionExtensions.Try(self);

        return(res.IsFaulted
            ? Fail(state, res.Exception)
            : res.Value.IsSome
                ? Some(state, res.Value.Value)
                : None(state));
    }
Exemplo n.º 17
0
    /// <summary>
    /// Invoke a delegate if the Try is in a Fail or None state
    /// </summary>
    /// <param name="None">Delegate to invoke if successful</param>
    public static Unit IfNoneOrFail <A>(this TryOption <A> self, Action None)
    {
        var res = TryOptionExtensions.Try(self);

        if (res.IsFaulted || res.Value.IsNone)
        {
            None();
        }
        return(unit);
    }
Exemplo n.º 18
0
    public static async Task <R> MatchAsync <A, R>(this TryOption <A> self, Func <A, R> Some, Func <Task <R> > None, Func <Exception, Task <R> > Fail)
    {
        var res = TryOptionExtensions.Try(self);

        return(await(res.IsFaulted
            ? Fail(res.Exception)
            : res.Value.IsSome
                ? Task.FromResult(Some(res.Value.Value))
                : None()));
    }
Exemplo n.º 19
0
    public static R Match <A, R>(this TryOption <A> self, Func <A, R> Some, R Fail)
    {
        if (isnull(Fail))
        {
            throw new ArgumentNullException(nameof(Fail));
        }

        var res = TryOptionExtensions.Try(self);

        return(res.IsFaulted || res.Value.IsNone
            ? Fail
            : Some(res.Value.Value));
    }
Exemplo n.º 20
0
    public static ExceptionMatch <Option <A> > IfFail <A>(this TryOption <A> self)
    {
        var res = TryOptionExtensions.Try(self);

        if (res.IsFaulted)
        {
            return(res.Exception.Match <Option <A> >());
        }
        else
        {
            return(new ExceptionMatch <Option <A> >(res.Value));
        }
    }
Exemplo n.º 21
0
    public static A IfNoneOrFail <A>(this TryOption <A> self, Func <A> None)
    {
        var res = TryOptionExtensions.Try(self);

        if (res.IsFaulted || res.Value.IsNone)
        {
            return(None());
        }
        else
        {
            return(res.Value.Value);
        }
    }
Exemplo n.º 22
0
    public static ExceptionMatch <Option <A> > IfFail <A>(this TryOption <A> self)
    {
        var res = TryOptionExtensions.Try(self);

        // BUG/TODO what about IsBottom (res.Exception might be null!)
        if (res.IsFaulted)
        {
            return(res.Exception.Match <Option <A> >());
        }
        else
        {
            return(new ExceptionMatch <Option <A> >(res.Value));
        }
    }
Exemplo n.º 23
0
    /// <summary>
    /// Pattern matches the two possible states of the Try computation
    /// </summary>
    /// <param name="Some">Delegate to invoke if the Try computation completes successfully</param>
    /// <param name="Fail">Delegate to invoke if the Try computation fails</param>
    public static Unit Match <A>(this TryOption <A> self, Action <A> Some, Action Fail)
    {
        var res = TryOptionExtensions.Try(self);

        if (res.IsFaulted || res.Value.IsNone)
        {
            Fail();
        }
        else
        {
            Some(res.Value.Value);
        }

        return(Unit.Default);
    }
Exemplo n.º 24
0
    public static A IfNoneOrFail <A>(this TryOption <A> self, A defaultValue)
    {
        if (isnull(defaultValue))
        {
            throw new ArgumentNullException(nameof(defaultValue));
        }

        var res = TryOptionExtensions.Try(self);

        if (res.IsFaulted || res.Value.IsNone)
        {
            return(defaultValue);
        }
        else
        {
            return(res.Value.Value);
        }
    }
Exemplo n.º 25
0
    public static A IfNoneOrFail <A>(
        this TryOption <A> self,
        Func <A> None,
        Func <Exception, A> Fail)
    {
        var res = TryOptionExtensions.Try(self);

        if (res.IsBottom)
        {
            return(Fail(res.Exception ?? new BottomException()));
        }
        else if (res.IsFaulted)
        {
            return(Fail(res.Exception));
        }
        else if (res.Value.IsNone)
        {
            return(None());
        }
        else
        {
            return(res.Value.Value);
        }
    }
Exemplo n.º 26
0
 public static TryOption <T> TryOption <T>(Func <Option <T> > f) =>
 TryOptionExtensions.Memo <T>(() => f());
Exemplo n.º 27
0
 public static TryOption <T> TryOption <T>(Func <T> f) =>
 TryOptionExtensions.Memo <T>(() => Optional(f()));