Exemplo n.º 1
0
        public static Either <ImmutableList <A>, Tpl <P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, P14, P15, P16, P17, P18, P19, P20, P21> > and <A, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, P14, P15, P16, P17, P18, P19, P20, P21>(
            this Either <ImmutableList <A>, Tpl <P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, P14, P15, P16, P17, P18, P19, P20> > e1,
            Either <ImmutableList <A>, P21> e2
            )
        {
            foreach (var tpl in e1.rightValue)
            {
                foreach (var value in e2.rightValue)
                {
                    return(Either <ImmutableList <A>, Tpl <P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, P14, P15, P16, P17, P18, P19, P20, P21> > .Right(tpl.add(value)));
                }
            }


            var arr = e1.leftValue.fold(ImmutableList <A> .Empty, _ => _);

            foreach (var arr2 in e2.leftValue)
            {
                foreach (var a in arr2)
                {
                    arr = arr.Add(a);
                }
            }
            return(Either <ImmutableList <A>, Tpl <P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, P14, P15, P16, P17, P18, P19, P20, P21> > .Left(arr));
        }
Exemplo n.º 2
0
 public void WhenHasNoErrors()
 {
     new[] {
         Either <ImmutableList <string>, int> .Right(3),
         Either <ImmutableList <string>, int> .Right(4)
     }.sequenceValidations().shouldBeRightEnum(ImmutableList.Create(3, 4));
 }
Exemplo n.º 3
0
        public static Either <ImmutableList <A>, Tpl <P1, P2, P3, P4> > and <A, P1, P2, P3, P4>(
            this Either <ImmutableList <A>, Tpl <P1, P2, P3> > e1,
            Either <ImmutableList <A>, P4> e2
            )
        {
            foreach (var tpl in e1.rightValue)
            {
                foreach (var value in e2.rightValue)
                {
                    return(Either <ImmutableList <A>, Tpl <P1, P2, P3, P4> > .Right(tpl.add(value)));
                }
            }


            var arr = e1.leftValue.fold(ImmutableList <A> .Empty, _ => _);

            foreach (var arr2 in e2.leftValue)
            {
                foreach (var a in arr2)
                {
                    arr = arr.Add(a);
                }
            }
            return(Either <ImmutableList <A>, Tpl <P1, P2, P3, P4> > .Left(arr));
        }
Exemplo n.º 4
0
        public static Either <ImmutableList <A>, ImmutableList <B> > sequence <A, B>(
            this IEnumerable <Either <ImmutableList <A>, B> > eithers
            )
        {
            var errors = ImmutableList <A> .Empty;
            var result = ImmutableList <B> .Empty;

            foreach (var either in eithers)
            {
                foreach (var errs in either.leftValue)
                {
                    errors = errors.AddRange(errs);
                }
                if (errors.IsEmpty)
                {
                    // No point in accumulating result if we have at least one error.
                    foreach (var b in either.rightValue)
                    {
                        result = result.Add(b);
                    }
                }
            }
            return(errors.IsEmpty
        ? Either <ImmutableList <A>, ImmutableList <B> > .Right(result)
        : Either <ImmutableList <A>, ImmutableList <B> > .Left(errors));
        }
Exemplo n.º 5
0
 public static Either <A, Option <B> > extract <A, B>(this Option <Either <A, B> > o)
 {
     foreach (var e in o)
     {
         return(e.fold(Either <A, Option <B> > .Left, b => Either <A, Option <B> > .Right(b.some())));
     }
     return(Either <A, Option <B> > .Right(F.none <B>()));
 }
Exemplo n.º 6
0
        public void WhenHasOneError()
        {
            var l = ImmutableList.Create("error");

            new[] {
                Either <ImmutableList <string>, int> .Left(l),
                Either <ImmutableList <string>, int> .Right(4)
            }.sequenceValidations().shouldBeLeftEnum(l);
        }
Exemplo n.º 7
0
        public void WhenRight()
        {
            var called = 0;

            foreach (var b in Either <string, int> .Right(3))
            {
                b.shouldEqual(3);
                called++;
            }
            called.shouldEqual(1, "it should yield once");
        }
Exemplo n.º 8
0
 [PublicAPI] public static Option <Either <A, B> > flattenRight <A, B>(this Option <Either <A, Option <B> > > o)
 {
     if (o.isSome)
     {
         var e = o.__unsafeGetValue;
         return
             (e.isLeft
     ? Either <A, B> .Left(e.__unsafeGetLeft).some()
     : e.__unsafeGetRight.fold(F.none_, r => Either <A, B> .Right(r).some()));
     }
     return(F.none_);
 }
Exemplo n.º 9
0
 [PublicAPI] public static Either <A, Option <B> > extract <A, B>(this Option <Either <A, B> > o)
 {
     if (o.isSome)
     {
         var e = o.__unsafeGetValue;
         return
             (e.isLeft
     ? Either <A, Option <B> > .Left(e.__unsafeGetLeft)
     : Either <A, Option <B> > .Right(e.__unsafeGetRight.some()));
     }
     return(F.none <B>());
 }
Exemplo n.º 10
0
 [PublicAPI] public static Option <Either <A, B> > flattenLeft <A, B>(this Option <Either <Option <A>, B> > o)
 {
     if (o.isSome)
     {
         var e = o.__unsafeGetValue;
         return
             (e.isRight
     ? Either <A, B> .Right(e.__unsafeGetRight).some()
     : e.__unsafeGetLeft.fold(F.none_, l => Either <A, B> .Left(l).some()));
     }
     return(F.none_);
 }
Exemplo n.º 11
0
 public void WhenNotEqual()
 {
     ImmutableList.Create(
         Either <int, string> .Left(0),
         Either <int, string> .Left(1),
         Either <int, string> .Right("0"),
         Either <int, string> .Right("1")
         ).shouldTestInequalityAgainst(ImmutableList.Create(
                                           Either <int, string> .Left(10),
                                           Either <int, string> .Left(11),
                                           Either <int, string> .Right("10"),
                                           Either <int, string> .Right("11")
                                           ));
 }
Exemplo n.º 12
0
        public static Either <ImmutableList <A>, Tpl <P1, P2> > and <A, P1, P2>(
            this Either <ImmutableList <A>, P1> e1, Either <A, P2> e2
            )
        {
            foreach (var b in e1.rightValue)
            {
                foreach (var c in e2.rightValue)
                {
                    return(Either <ImmutableList <A>, Tpl <P1, P2> > .Right(F.t(b, c)));
                }
            }


            var arr = e1.leftValue.fold(ImmutableList <A> .Empty, _ => _);

            foreach (var a in e2.leftValue)
            {
                arr = arr.Add(a);
            }
            return(Either <ImmutableList <A>, Tpl <P1, P2> > .Left(arr));
        }
Exemplo n.º 13
0
   public static Either <A, B> opt <A, B>(bool condition, A ifFalse, B ifTrue) =>
   condition
 ? Either <A, B> .Right(ifTrue)
 : Either <A, B> .Left(ifFalse);
Exemplo n.º 14
0
 public Either <A, B> toLeft <B>(Fn <B> right)
 {
     return(isSome ? Either <A, B> .Left(value) : Either <A, B> .Right(right()));
 }
Exemplo n.º 15
0
 public Either <B, A> toRight <B>(Fn <B> left)
 {
     return(isSome ? Either <B, A> .Right(value) : Either <B, A> .Left(left()));
 }
Exemplo n.º 16
0
 [Test] public void WhenRight() => Either <string, int> .Right(3).swap.shouldBeLeft(3);
Exemplo n.º 17
0
 [Test] public void WhenRight() =>
 Either <int, string> .Right("foo")
 .toTry(onLeft)
 .shouldBeSuccess("foo");
Exemplo n.º 18
0
 [Test] public void WhenRight() => test(Either <int, string> .Right("foo"), 'f');
Exemplo n.º 19
0
 [PublicAPI] public Either <A, B> toLeft <B>(B right) =>
 isSome ? Either <A, B> .Left(__unsafeGetValue) : Either <A, B> .Right(right);
Exemplo n.º 20
0
   public static Either <A, B> opt <A, B>(bool condition, Fn <A> ifFalse, Fn <B> ifTrue) =>
   condition
 ? Either <A, B> .Right(ifTrue())
 : Either <A, B> .Left(ifFalse());
Exemplo n.º 21
0
 public Either <B, A> toRight <B>(Fn <B> left) =>
 isSome ? Either <B, A> .Right(__unsafeGetValue) : Either <B, A> .Left(left());
Exemplo n.º 22
0
 public Either <A, B> toLeft <B>(Fn <B> right) =>
 isSome ? Either <A, B> .Left(__unsafeGetValue) : Either <A, B> .Right(right());
Exemplo n.º 23
0
 [PublicAPI] public Either <B, A> toRight <B>(B left) =>
 isSome ? Either <B, A> .Right(__unsafeGetValue) : Either <B, A> .Left(left);
Exemplo n.º 24
0
   public static Either <ImmutableList <A>, B> asValidationErrors <A, B>(
       this ImmutableList <A> errors, Fn <B> b
       ) =>
   errors.IsEmpty
 ? Either <ImmutableList <A>, B> .Right(b())
 : Either <ImmutableList <A>, B> .Left(errors);
Exemplo n.º 25
0
        public void WhenRightEquals()
        {
            Either <int, string> .Right("0").shouldEqual(Either <int, string> .Right("0"));

            Either <int, string> .Right("10").shouldEqual(Either <int, string> .Right("10"));
        }
Exemplo n.º 26
0
 public static Either <ImmutableList <A>, B> validationSuccess <A, B>(this B b) =>
 Either <ImmutableList <A>, B> .Right(b);
Exemplo n.º 27
0
 [Test] public void WhenRight() => Either <int, string> .Right("foo").fold(folder, folder).shouldEqual('f');