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)); }
public void WhenHasNoErrors() { new[] { Either <ImmutableList <string>, int> .Right(3), Either <ImmutableList <string>, int> .Right(4) }.sequenceValidations().shouldBeRightEnum(ImmutableList.Create(3, 4)); }
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)); }
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)); }
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>())); }
public void WhenHasOneError() { var l = ImmutableList.Create("error"); new[] { Either <ImmutableList <string>, int> .Left(l), Either <ImmutableList <string>, int> .Right(4) }.sequenceValidations().shouldBeLeftEnum(l); }
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"); }
[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_); }
[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>()); }
[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_); }
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") )); }
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)); }
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);
public Either <A, B> toLeft <B>(Fn <B> right) { return(isSome ? Either <A, B> .Left(value) : Either <A, B> .Right(right())); }
public Either <B, A> toRight <B>(Fn <B> left) { return(isSome ? Either <B, A> .Right(value) : Either <B, A> .Left(left())); }
[Test] public void WhenRight() => Either <string, int> .Right(3).swap.shouldBeLeft(3);
[Test] public void WhenRight() => Either <int, string> .Right("foo") .toTry(onLeft) .shouldBeSuccess("foo");
[Test] public void WhenRight() => test(Either <int, string> .Right("foo"), 'f');
[PublicAPI] public Either <A, B> toLeft <B>(B right) => isSome ? Either <A, B> .Left(__unsafeGetValue) : Either <A, B> .Right(right);
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());
public Either <B, A> toRight <B>(Fn <B> left) => isSome ? Either <B, A> .Right(__unsafeGetValue) : Either <B, A> .Left(left());
public Either <A, B> toLeft <B>(Fn <B> right) => isSome ? Either <A, B> .Left(__unsafeGetValue) : Either <A, B> .Right(right());
[PublicAPI] public Either <B, A> toRight <B>(B left) => isSome ? Either <B, A> .Right(__unsafeGetValue) : Either <B, A> .Left(left);
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);
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")); }
public static Either <ImmutableList <A>, B> validationSuccess <A, B>(this B b) => Either <ImmutableList <A>, B> .Right(b);
[Test] public void WhenRight() => Either <int, string> .Right("foo").fold(folder, folder).shouldEqual('f');