public static EitherUnsafe <L, A> ToEitherUnsafe <A, L>(this Try <A> self, Func <Exception, L> Fail)
    {
        var res = self.Try();

        return(res.IsFaulted
            ? EitherUnsafe <L, A> .Left(Fail(res.Exception))
            : EitherUnsafe <L, A> .Right(res.Value));
    }
    public static EitherUnsafe <Exception, A> ToEitherUnsafe <A>(this Try <A> self)
    {
        var res = self.Try();

        return(res.IsFaulted
            ? EitherUnsafe <Exception, A> .Left(res.Exception)
            : EitherUnsafe <Exception, A> .Right(res.Value));
    }
Exemple #3
0
 /// <summary>
 /// Filter the Either
 /// This may give unpredictable results for a filtered value.  The Either won't
 /// return true for IsLeft or IsRight.  IsBottom is True if the value is filterd and that
 /// should be checked.
 /// </summary>
 /// <typeparam name="L">Left</typeparam>
 /// <typeparam name="R">Right</typeparam>
 /// <param name="self">Either to filter</param>
 /// <param name="pred">Predicate function</param>
 /// <returns>If the Either is in the Left state it is returned as-is.
 /// If in the Left state the predicate is applied to the Left value.
 /// If the predicate returns True the Either is returned as-is.
 /// If the predicate returns False the Either is returned in a 'Bottom' state.</returns>
 public static EitherUnsafe <L, R> Filter <L, R>(this EitherUnsafe <L, R> self, Func <L, bool> pred) =>
 self.IsBottom
         ? self
         : self.MatchUnsafe(
     Right: (R r) => EitherUnsafe <L, R> .Right(r),
     Left:  (L t) => pred(t)
                                 ? EitherUnsafe <L, R> .Left(t)
                                 : new EitherUnsafe <L, R>(true)
     );
Exemple #4
0
 public static EitherUnsafe <L, VR> SelectMany <L, TR, UR, VR>(this EitherUnsafe <L, TR> self,
                                                               Func <TR, EitherUnsafe <L, UR> > bind,
                                                               Func <TR, UR, VR> project
                                                               ) =>
 matchUnsafe(self,
             Right: t =>
             matchUnsafe(bind(t),
                         Right: u => EitherUnsafe <L, VR> .Right(project(t, u)),
                         Left: l => EitherUnsafe <L, VR> .Left(l)
                         ),
             Left: l => EitherUnsafe <L, VR> .Left(l)
             );
Exemple #5
0
 public static EitherUnsafe <L, UR> Select <L, TR, UR>(this EitherUnsafe <L, TR> self, Func <TR, UR> map) =>
 matchUnsafe(self,
             Right: t => EitherUnsafe <L, UR> .Right(map(t)),
             Left: l => EitherUnsafe <L, UR> .Left(l)
             );
Exemple #6
0
 public EitherUnsafe <L2, R2> BiMap(EitherUnsafe <L, R> ma, Func <L, L2> fa, Func <R, R2> fb) =>
 default(MEitherUnsafe <L, R>).MatchUnsafe(ma,
                                           Left: a => EitherUnsafe <L2, R2> .Left(Check.NullReturn(fa(a))),
                                           Right: b => EitherUnsafe <L2, R2> .Right(Check.NullReturn(fb(b))),
                                           Bottom: () => EitherUnsafe <L2, R2> .Bottom);
Exemple #7
0
 public static Task <EitherUnsafe <L, A> > ToEitherUnsafe <A, L>(this TryAsync <A> self, Func <Exception, L> Fail) =>
 self.Match(
     Succ: v => EitherUnsafe <L, A> .Right(v),
     Fail: x => EitherUnsafe <L, A> .Left(Fail(x)));
Exemple #8
0
 public static Task <EitherUnsafe <Exception, A> > ToEitherUnsafe <A>(this TryAsync <A> self) =>
 self.Match(
     Succ: v => EitherUnsafe <Exception, A> .Right(v),
     Fail: x => EitherUnsafe <Exception, A> .Left(x));
Exemple #9
0
 /// <summary>
 /// Filter the Either
 /// This may give unpredictable results for a filtered value.  The Either won't
 /// return true for IsLeft or IsRight.  IsBottom is True if the value is filterd and that
 /// should be checked.
 /// </summary>
 /// <typeparam name="L">Left</typeparam>
 /// <typeparam name="R">Right</typeparam>
 /// <param name="self">Either to filter</param>
 /// <param name="pred">Predicate function</param>
 /// <returns>If the Either is in the Left state it is returned as-is.
 /// If in the Right state the predicate is applied to the Right value.
 /// If the predicate returns True the Either is returned as-is.
 /// If the predicate returns False the Either is returned in a 'Bottom' state.  IsLeft will return True, but the value
 /// of Left = default(L)</returns>
 public static EitherUnsafe <L, R> Filter <L, R>(this EitherUnsafe <L, R> self, Func <R, bool> pred) =>
 self.IsBottom
         ? self
         : matchUnsafe(self,
                       Right: t => pred(t) ? EitherUnsafe <L, R> .Right(t) : new EitherUnsafe <L, R>(true),
                       Left: l => EitherUnsafe <L, R> .Left(l));
Exemple #10
0
 /// <summary>
 /// Monadic bind function
 /// https://en.wikipedia.org/wiki/Monad_(functional_programming)
 /// </summary>
 /// <typeparam name="L">Left</typeparam>
 /// <typeparam name="R">Right</typeparam>
 /// <typeparam name="Ret"></typeparam>
 /// <param name="self"></param>
 /// <param name="binder"></param>
 /// <returns>Bound Either</returns>
 public static EitherUnsafe <Ret, R> Bind <L, R, Ret>(this EitherUnsafe <L, R> self, Func <L, EitherUnsafe <Ret, R> > binder) =>
 self.IsBottom
         ? new EitherUnsafe <Ret, R>(true)
         : self.IsLeft
             ? binder(self.LeftValue)
             : EitherUnsafe <Ret, R> .Right(self.RightValue);
Exemple #11
0
 /// <summary>
 /// Bi-filter the Either
 /// </summary>
 /// <remarks>
 /// This may give unpredictable results for a filtered value.  The Either won't
 /// return true for IsLeft or IsRight.  IsBottom is True if the value is filtered and that
 /// should be checked for.
 /// </remarks>
 /// <typeparam name="L">Left</typeparam>
 /// <typeparam name="R">Right</typeparam>
 /// <param name="self">Either to filter</param>
 /// <param name="pred">Predicate function</param>
 /// <returns>
 /// If the Either is in the Left state then the Left predicate is run against it.
 /// If the Either is in the Right state then the Right predicate is run against it.
 /// If the predicate returns False the Either is returned in a 'Bottom' state.</returns>
 public static EitherUnsafe <L, R> Filter <L, R>(this EitherUnsafe <L, R> self, Func <R, bool> Right, Func <L, bool> Left) =>
 self.IsBottom
         ? self
         : matchUnsafe(self,
                       Right: r => Right(r) ? EitherUnsafe <L, R> .Right(r) : new EitherUnsafe <L, R>(true),
                       Left: l => Left(l) ? EitherUnsafe <L, R> .Left(l) : new EitherUnsafe <L, R>(true));
Exemple #12
0
 public EitherUnsafe <L, R2> Map(EitherUnsafe <L, R> ma, Func <R, R2> f) =>
 default(MEitherUnsafe <L, R>).MatchUnsafe(ma,
                                           Left: EitherUnsafe <L, R2> .Left,
                                           Right: b => EitherUnsafe <L, R2> .Right(f(b)),
                                           Bottom: () => EitherUnsafe <L, R2> .Bottom);