/// <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) );
public Unit Left(Action <L> leftHandler) => either.MatchUnsafe(rightHandler, leftHandler);
public Ret Left(Func <L, Ret> left) => either.MatchUnsafe(rightHandler, left);
public Func <Unit, Task <S> > FoldBackAsync <S>(EitherUnsafe <L, R> fa, S state, Func <S, R, Task <S> > f) => _ => fa.MatchUnsafe( Right: r => f(state, r), Left: l => Task.FromResult(state), Bottom: () => Task.FromResult(state));
public Func <Unit, S> FoldBack <S>(EitherUnsafe <L, R> foldable, S state, Func <S, R, S> f) => u => foldable.MatchUnsafe( Left: _ => state, Right: _ => f(state, foldable.RightValue), Bottom: () => state);