public void Test1_either() { var somethingOrError = from userID in FSharpOption.ParseInt(req_userID).ToFSharpChoice("Invalid User ID") from id in FSharpOption.ParseInt(req_otherID).ToFSharpChoice("Invalid ID") select doSomething(userID, id); somethingOrError.Match(Console.WriteLine, setError); }
public void ChoiceToOption() { object a = 40; const string b = "60"; var r = from i in FSharpOption.ParseInt(b) from j in FSharpChoice.Cast <int>(a).ToFSharpOption() select i + j; Assert.AreEqual(100.Some(), r); }
public void SelectSecond_Error() { object a = 40; const string b = "xx"; var r = from i in FSharpOption.ParseInt(b).ToFSharpChoice("Invalid value b") from j in FSharpChoice.Cast <int>(a).SelectSecond(_ => "Invalid value a") select i + j; r.Match(i => Assert.Fail("should not have succeeded with value {0}", i), e => Assert.AreEqual("Invalid value b", e)); }
public void SelectSecond_OK() { object a = 40; const string b = "60"; var r = from i in FSharpOption.ParseInt(b).ToFSharpChoice("Invalid value b") from j in FSharpChoice.Cast <int>(a).SelectSecond(_ => "Invalid value a") select i + j; r.Match(i => Assert.AreEqual(100, i), Assert.Fail); }
public void OptionToChoice() { object a = 40; const string b = "60"; var r = from i in FSharpOption.ParseInt(b).ToFSharpChoice(new Exception()) from j in FSharpChoice.Cast <int>(a) select i + j; r.Match(i => Assert.AreEqual(100, i), e => Assert.Fail(e.Message)); }
public void Test2_either_LINQ() { var userID = FSharpOption.ParseInt(req_userID) .ToFSharpChoice(NonEmptyList.Singleton("Invalid User ID")); var id = FSharpOption.ParseInt(req_otherID) .ToFSharpChoice(NonEmptyList.Singleton("Invalid ID")); var result = from a in userID join b in id on 1 equals 1 select doSomething(a, b); result.Match(Console.WriteLine, setErrors); }
public void Test2_either() { var userID = FSharpOption.ParseInt(req_userID) .ToFSharpChoice(NonEmptyList.Singleton("Invalid User ID")); var id = FSharpOption.ParseInt(req_otherID) .ToFSharpChoice(NonEmptyList.Singleton("Invalid ID")); var doSomethingFunc = L.F((int a, int b) => doSomething(a, b)); var curriedDoSomething = doSomethingFunc.Curry(); var result = curriedDoSomething.ReturnValidation() .ApValidation(userID) .ApValidation(id); //var result = L.F((int a, int b) => doSomething(a,b)) // .Curry().PureValidate() // .ApV(userID) // .ApV(id); result.Match(Console.WriteLine, setErrors); }
public void Test1_option() { var userID = FSharpOption.ParseInt(req_userID); if (!userID.HasValue()) { setError("Invalid User ID"); } else { var otherID = FSharpOption.ParseInt(req_otherID); if (!otherID.HasValue()) { setError("Invalid ID"); } else { Console.WriteLine(doSomething(userID.Value, otherID.Value)); } } }
public void TryParseInt() { var a = FSharpOption.ParseInt("123"); Assert.AreEqual(123, a.Value); }