コード例 #1
0
ファイル: ChoiceTests.cs プロジェクト: gusty/fsharpx
        public void Select2()
        {
            var a = FSharpChoice.New2Of2 <int, string>("hello");
            var b = a.Select(i => i + 2);

            b.Match(_ => Assert.Fail("is int"), s => Assert.AreEqual("hello", s));
        }
コード例 #2
0
ファイル: Person.cs プロジェクト: slavah/fsharpx
 private static FSharpChoice <string, Errors> Mandatory(string s)
 {
     return(FSharpChoice.Validator <string>(x => !string.IsNullOrEmpty(x), "Mandatory field")(s));
     //if (string.IsNullOrEmpty(s))
     //    return FSharpChoice.Error<string>("Mandatory field");
     //return FSharpChoice.Ok(s);
 }
コード例 #3
0
        internal static Tuple <RankedTree <a>, FSharpList <RankedTree <a> > > removeMinTree <a>(IComparer <a> comparer, FSharpList <RankedTree <a> > heap)
        {
            FSharpList <RankedTree <a> > fsharpList1 = heap;

            if (fsharpList1.get_TailOrNull() == null)
            {
                throw new InvalidOperationException("The heap is empty.");
            }
            FSharpList <RankedTree <a> > fsharpList2 = fsharpList1;

            if (fsharpList2.get_TailOrNull().get_TailOrNull() == null)
            {
                return(new Tuple <RankedTree <a>, FSharpList <RankedTree <a> > >(fsharpList2.get_HeadOrDefault(), FSharpList <RankedTree <a> > .get_Empty()));
            }
            FSharpList <RankedTree <a> > tailOrNull = fsharpList2.get_TailOrNull();
            RankedTree <a> headOrDefault            = fsharpList2.get_HeadOrDefault();
            Tuple <RankedTree <a>, FSharpList <RankedTree <a> > > tuple = BinomialHeap.removeMinTree <a>(comparer, tailOrNull);
            FSharpList <RankedTree <a> > fsharpList3 = tuple.Item2;
            RankedTree <a> rankedTree = tuple.Item1;
            FSharpChoice <Unit, Unit, Unit> fsharpChoice = BinomialHeap.LTGTEQ(comparer.Compare(BinomialHeap.root <a>(headOrDefault), BinomialHeap.root <a>(rankedTree)));

            if (fsharpChoice is FSharpChoice <Unit, Unit, Unit> .Choice1Of3 || fsharpChoice is FSharpChoice <Unit, Unit, Unit> .Choice3Of3)
            {
                return(new Tuple <RankedTree <a>, FSharpList <RankedTree <a> > >(headOrDefault, tailOrNull));
            }
            return(new Tuple <RankedTree <a>, FSharpList <RankedTree <a> > >(rankedTree, FSharpList <RankedTree <a> > .Cons(headOrDefault, fsharpList3)));
        }
コード例 #4
0
ファイル: ChoiceTests.cs プロジェクト: gusty/fsharpx
        public void Select()
        {
            var a = FSharpChoice.New1Of2 <int, string>(5);
            var b = a.Select(i => i + 2);

            b.Match(i => Assert.AreEqual(7, i), _ => Assert.Fail("is string"));
        }
コード例 #5
0
ファイル: Person.cs プロジェクト: slavah/fsharpx
 private static FSharpChoice <int, Errors> Positive(int a)
 {
     if (a <= 0)
     {
         return(FSharpChoice.Error <int>("Field must be positive"));
     }
     return(FSharpChoice.Ok(a));
 }
コード例 #6
0
ファイル: ChoiceTests.cs プロジェクト: gusty/fsharpx
        public void Match()
        {
            var a = FSharpChoice <int, string> .NewChoice1Of2(1);

            var c = a.Match(i => 0, _ => 1);

            Assert.AreEqual(0, c);
        }
コード例 #7
0
ファイル: ChoiceTests.cs プロジェクト: gusty/fsharpx
        public void Cast_OK()
        {
            object a = 40;

            FSharpChoice.Cast <int>(a)
            .Match(i => Assert.AreEqual(40, i),
                   e => Assert.Fail(e.Message));
        }
コード例 #8
0
 static FSharpChoice <T?, Errors> GreaterThan <T>(T?value, T?other, string err) where T : struct, IComparable <T>
 {
     if (!value.HasValue && !other.HasValue || value.HasValue != other.HasValue || value.Value.CompareTo(other.Value) > 0)
     {
         return(FSharpChoice.Ok(value));
     }
     return(FSharpChoice.Error <T?>(err));
 }
コード例 #9
0
ファイル: ChoiceTests.cs プロジェクト: gusty/fsharpx
        public void Cast_Exception()
        {
            object a = "hello";

            FSharpChoice.Cast <int>(a)
            .Match(i => Assert.Fail("should not have succeeded with value {0}", i),
                   e => {});
        }
コード例 #10
0
 static FSharpChoice <Address, Errors> ValidateAddressLines(Address a)
 {
     if (a.Line1 != null || a.Line2 == null)
     {
         return(FSharpChoice.Ok(a));
     }
     return(FSharpChoice.Error <Address>("Line1 is empty but Line2 is not"));
 }
コード例 #11
0
ファイル: ChoiceTests.cs プロジェクト: gusty/fsharpx
        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);
        }
コード例 #12
0
 /// <summary>
 /// Same as FSharpChoice.Validator, for demo purposes only
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="pred"></param>
 /// <param name="err"></param>
 /// <returns></returns>
 static Func <T, FSharpChoice <T, Errors> > Validator <T>(Predicate <T> pred, string err)
 {
     return(x => {
         if (pred(x))
         {
             return FSharpChoice.Ok(x);
         }
         return FSharpChoice.Error <T>(err);
     });
 }
コード例 #13
0
        public void Part3()
        {
            var person = new Person(
                gender: Gender.Male,
                age: 59,
                clothes: FSharpSet.Create("Jeans"),
                sobriety: Sobriety.Paralytic);
            var cost = GayBar.CostToEnter(person);

            Assert.AreEqual(FSharpChoice.Errors <decimal>("Too old!", "Smarten up!", "Sober up!"), cost);
        }
コード例 #14
0
ファイル: ChoiceTests.cs プロジェクト: gusty/fsharpx
        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));
        }
コード例 #15
0
        static FSharpChoice <T, Errors> GreaterThan <T>(T value, T other, string err) where T : IComparable <T>
        {
            var valueNull = Equals(null, value);
            var otherNull = Equals(null, other);

            if (valueNull && otherNull || valueNull != otherNull || value.CompareTo(other) > 0)
            {
                return(FSharpChoice.Ok(value));
            }
            return(FSharpChoice.Error <T>(err));
        }
コード例 #16
0
 private void SaveAndClose(bool isNewName)
 {
     if (isNewName)
     {
         Name = FSharpChoice <string, string> .NewChoice1Of2(SaveWithNewName);
     }
     else
     {
         Name = FSharpChoice <string, string> .NewChoice2Of2(RewriteExistScheduleName);
     }
 }
コード例 #17
0
ファイル: ChoiceTests.cs プロジェクト: gusty/fsharpx
        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));
        }
コード例 #18
0
ファイル: ChoiceTests.cs プロジェクト: gusty/fsharpx
        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);
        }
コード例 #19
0
        public static TResult Match <T1, T2, TResult>(this FSharpChoice <T1, T2> choice, Func <T1, TResult> func1, Func <T2, TResult> func2)
        {
            switch (choice.Tag)
            {
            case FSharpChoice <T1, T2> .Tags.Choice1Of2:
                return(func1((choice.CastTo <FSharpChoice <T1, T2> .Choice1Of2>()).Item));

            case FSharpChoice <T1, T2> .Tags.Choice2Of2:
                return(func2((choice.CastTo <FSharpChoice <T1, T2> .Choice2Of2>()).Item));
            }
            throw new InvalidOperationException();
        }
コード例 #20
0
        public static FSharpChoice <TR1, TR2> Transform <T1, T2, TR1, TR2>(this FSharpChoice <T1, T2> choice, Func <T1, TR1> func1, Func <T2, TR2> func2)
        {
            switch (choice.Tag)
            {
            case FSharpChoice <T1, T2> .Tags.Choice1Of2:
                return(FSharpChoice <TR1, TR2> .NewChoice1Of2(func1((choice.CastTo <FSharpChoice <T1, T2> .Choice1Of2>()).Item)));

            case FSharpChoice <T1, T2> .Tags.Choice2Of2:
                return(FSharpChoice <TR1, TR2> .NewChoice2Of2(func2((choice.CastTo <FSharpChoice <T1, T2> .Choice2Of2>()).Item)));
            }
            throw new InvalidOperationException();
        }
コード例 #21
0
 public static FSharpChoice <Unit, Unit, Unit> LTGTEQ(int n)
 {
     if (n < 0)
     {
         return(FSharpChoice <Unit, Unit, Unit> .NewChoice1Of3((Unit)null));
     }
     if (n > 0)
     {
         return(FSharpChoice <Unit, Unit, Unit> .NewChoice2Of3((Unit)null));
     }
     return(FSharpChoice <Unit, Unit, Unit> .NewChoice3Of3((Unit)null));
 }
コード例 #22
0
ファイル: ChoiceTests.cs プロジェクト: gusty/fsharpx
        public void New()
        {
            var a = FSharpChoice.New1Of2 <int, string>(1);
            var b = FSharpChoice <int, string> .NewChoice1Of2(1);

            Assert.AreEqual(a, b);

            var c = FSharpChoice.New2Of2 <int, string>("a");
            var d = FSharpChoice <int, string> .NewChoice2Of2("a");

            Assert.AreEqual(c, d);
        }
コード例 #23
0
        public void Example()
        {
            var urls = FSharpList.Create(
                "http://www.google.com"
                , "http://www.bing.com"
                , "http://www.yahoo.com"
                , "http://www.microsoft.com"
                );
            var realJoinWebPages = JoinWebPages(url => Get(url).Protect());
            var testJoinWebPages = JoinWebPages(_ => FSharpAsyncEx.Return(FSharpChoice.New1Of2 <string, Exception>("hello")));
            var result           = testJoinWebPages(urls);

            Assert.AreEqual("hellohellohellohello", result);
        }
コード例 #24
0
        Tuple <IIndex <K>, VectorConstruction> IIndexBuilder.GetRange <K>(Tuple <IIndex <K>, VectorConstruction> _arg12, Tuple <FSharpOption <Tuple <K, BoundaryBehavior> >, FSharpOption <Tuple <K, BoundaryBehavior> > > _arg13)
        {
            Tuple <IIndex <K>, VectorConstruction> tuple1 = _arg12;
            VectorConstruction vectorConstruction         = tuple1.Item2;
            IIndex <K>         index = tuple1.Item1;
            Tuple <FSharpOption <Tuple <K, BoundaryBehavior> >, FSharpOption <Tuple <K, BoundaryBehavior> > > tuple2 = _arg13;
            FSharpOption <Tuple <K, BoundaryBehavior> > fsharpOption1 = tuple2.Item1;
            FSharpOption <Tuple <K, BoundaryBehavior> > fsharpOption2 = tuple2.Item2;
            Tuple <long, long> tuple3 = new Tuple <long, long>(0L, index.KeyCount - 1L);
            long num1 = tuple3.Item1;
            long num2 = tuple3.Item2;
            FSharpOption <Tuple <K, BoundaryBehavior> > fsharpOption3 = fsharpOption1;
            long num3;

            if (fsharpOption3 != null)
            {
                FSharpOption <Tuple <K, BoundaryBehavior> > fsharpOption4 = fsharpOption3;
                K      key    = fsharpOption4.get_Value().Item1;
                Lookup lookup = !fsharpOption4.get_Value().Item2.Equals((object)BoundaryBehavior.get_Exclusive(), LanguagePrimitives.get_GenericEqualityComparer()) ? Lookup.ExactOrGreater : Lookup.Greater;
                FSharpChoice <Unit, Tuple <K, long> > fsharpChoice = OptionalValueModule.MissingPresent <Tuple <K, long> >(index.Lookup(key, lookup, (Func <long, bool>) new LinearIndex.loBound()));
                num3 = !(fsharpChoice is FSharpChoice <Unit, Tuple <K, long> > .Choice1Of2) ? ((FSharpChoice <Unit, Tuple <K, long> > .Choice2Of2)fsharpChoice).get_Item().Item2 : num2 + 1L;
            }
            else
            {
                num3 = num1;
            }
            long startAddress = num3;
            FSharpOption <Tuple <K, BoundaryBehavior> > fsharpOption5 = fsharpOption2;
            long num4;

            if (fsharpOption5 != null)
            {
                FSharpOption <Tuple <K, BoundaryBehavior> > fsharpOption4 = fsharpOption5;
                K      key    = fsharpOption4.get_Value().Item1;
                Lookup lookup = !fsharpOption4.get_Value().Item2.Equals((object)BoundaryBehavior.get_Exclusive(), LanguagePrimitives.get_GenericEqualityComparer()) ? Lookup.ExactOrSmaller : Lookup.Smaller;
                FSharpChoice <Unit, Tuple <K, long> > fsharpChoice = OptionalValueModule.MissingPresent <Tuple <K, long> >(index.Lookup(key, lookup, (Func <long, bool>) new LinearIndex.hiBound()));
                num4 = !(fsharpChoice is FSharpChoice <Unit, Tuple <K, long> > .Choice1Of2) ? ((FSharpChoice <Unit, Tuple <K, long> > .Choice2Of2)fsharpChoice).get_Item().Item2 : num1 - 1L;
            }
            else
            {
                num4 = num2;
            }
            long endAddress = num4;

            if (endAddress < startAddress)
            {
                return(new Tuple <IIndex <K>, VectorConstruction>((IIndex <K>) new LinearIndex <K>(System.Array.AsReadOnly <K>(new K[0]), (IIndexBuilder)this, FSharpOption <bool> .Some(true)), VectorConstruction.NewEmpty(0L)));
            }
            return(new Tuple <IIndex <K>, VectorConstruction>((IIndex <K>) new LinearRangeIndex <K>(index, startAddress, endAddress), VectorConstruction.NewGetRange(vectorConstruction, RangeRestriction <long> .NewFixed(startAddress, endAddress))));
        }
コード例 #25
0
        private DiagramItem Transform(ANTLRv4Parser.LexerAltContext context)
        {
            var lexerElements = context.lexerElements().lexerElement().Select(Transform).Select(FSharpChoice <DiagramItem, string> .NewChoice1Of2);

            if (context.lexerCommands() != null)
            {
                return(new Sequence(lexerElements.Concat(new[] { FSharpChoice <DiagramItem, string> .NewChoice1Of2(TerminalWithClass("->", "rightArrows")) })
                                    .Concat(context.lexerCommands()
                                            .lexerCommand()
                                            .Select(x => TerminalWithClass(x.GetText(), "lexerCommands"))
                                            .Select(FSharpChoice <DiagramItem, string> .NewChoice1Of2))));
            }
            return(new Sequence(lexerElements));
        }
コード例 #26
0
        public void Part2()
        {
            var daveParalytic = new Person(
                age: Test1.Dave.Age,
                clothes: Test1.Dave.Clothes,
                gender: Test1.Dave.Gender,
                sobriety: Sobriety.Paralytic);
            var costDaveParalytic = ClubTropicana.CostToEnter(daveParalytic);

            Assert.AreEqual(FSharpChoice.Errors <decimal>("Too old!", "Sober up!"), costDaveParalytic);

            var costRuby = ClubTropicana.CostToEnter2(Test1.Ruby);

            Assert.AreEqual(FSharpChoice.Ok(0m), costRuby);
        }
コード例 #27
0
        public R GetAs <R>(K column, R fallback)
        {
            FSharpChoice <Unit, Tuple <K, long> > fsharpChoice1 = OptionalValueModule.\u007CMissing\u007CPresent\u007C <Tuple <K, long> > (this.index.Lookup(column, Lookup.Exact, (FSharpFunc <long, bool>) new \u0024Series.address\u00401230\u002D1()));

            if (fsharpChoice1 is FSharpChoice <Unit, Tuple <K, long> > .Choice1Of2)
            {
                throw new KeyNotFoundException(((FSharpFunc <K, string>)ExtraTopLevelOperators.PrintFormatToString <FSharpFunc <K, string> >((PrintfFormat <M0, Unit, string, string>) new PrintfFormat <FSharpFunc <K, string>, Unit, string, string, K>("The key %O is not present in the index"))).Invoke(column));
            }
            FSharpChoice <Unit, object> fsharpChoice2 = OptionalValueModule.\u007CMissing\u007CPresent\u007C <object> (this.vector.GetValue(((FSharpChoice <Unit, Tuple <K, long> > .Choice2Of2)fsharpChoice1).get_Item().Item2));

            if (!(fsharpChoice2 is FSharpChoice <Unit, object> .Choice1Of2))
            {
                return(Deedle.Internal.Convert.convertType <R>(ConversionKind.Flexible, ((FSharpChoice <Unit, object> .Choice2Of2)fsharpChoice2).get_Item()));
            }
            return(fallback);
        }
コード例 #28
0
        Tuple <IIndex <K>, VectorConstruction> IIndexBuilder.DropItem <K>(Tuple <IIndex <K>, VectorConstruction> _arg10, K key)
        {
            Tuple <IIndex <K>, VectorConstruction> tuple1 = _arg10;
            VectorConstruction vectorConstruction1        = tuple1.Item2;
            IIndex <K>         index = tuple1.Item1;
            FSharpChoice <Unit, Tuple <K, long> > fsharpChoice = OptionalValueModule.MissingPresent <Tuple <K, long> >(index.Lookup(key, Lookup.Exact, (Func <long, bool>) new LinearIndex.DeedleIndicesIIndexBuilderDropItem()));

            if (fsharpChoice is FSharpChoice <Unit, Tuple <K, long> > .Choice2Of2)
            {
                Tuple <K, long>        tuple2 = ((FSharpChoice <Unit, Tuple <K, long> > .Choice2Of2)fsharpChoice).get_Item();
                VectorConstruction     vectorConstruction2 = VectorConstruction.NewDropRange(vectorConstruction1, RangeRestriction <long> .NewFixed(tuple2.Item2, tuple2.Item2));
                ReadOnlyCollection <K> keys = index.Keys;
                return(new Tuple <IIndex <K>, VectorConstruction>((IIndex <K>) new LinearIndex <K>(System.Array.AsReadOnly <K>(ArrayModule.OfSeq <K>(SeqModule.Filter <K>((Func <K, bool>) new LinearIndex.newKeys <K>(key), (IEnumerable <M0>)keys))), (IIndexBuilder)this, FSharpOption <bool> .Some(index.IsOrdered)), vectorConstruction2));
            }
            string paramName = nameof(key);

            throw new ArgumentException(((Func <K, string>)ExtraTopLevelOperators.PrintFormatToString <Func <K, string> >((PrintfFormat <M0, Unit, string, string>) new PrintfFormat <Func <K, string>, Unit, string, string, K>("The key '%O' is not present in the index."))).Invoke(key), paramName);
        }
コード例 #29
0
        public static FSharpChoice <TR1, TR2, TR3, TR4> Transform <T1, T2, T3, T4, TR1, TR2, TR3, TR4>(this FSharpChoice <T1, T2, T3, T4> choice, Func <T1, TR1> func1, Func <T2, TR2> func2, Func <T3, TR3> func3, Func <T4, TR4> func4)
        {
            switch (choice.Tag)
            {
            case FSharpChoice <T1, T2, T3, T4> .Tags.Choice1Of4:
                return(FSharpChoice <TR1, TR2, TR3, TR4> .NewChoice1Of4(func1((choice.CastTo <FSharpChoice <T1, T2, T3, T4> .Choice1Of4>()).Item)));

            case FSharpChoice <T1, T2, T3, T4> .Tags.Choice2Of4:
                return(FSharpChoice <TR1, TR2, TR3, TR4> .NewChoice2Of4(func2((choice.CastTo <FSharpChoice <T1, T2, T3, T4> .Choice2Of4>()).Item)));

            case FSharpChoice <T1, T2, T3, T4> .Tags.Choice3Of4:
                return(FSharpChoice <TR1, TR2, TR3, TR4> .NewChoice3Of4(func3((choice.CastTo <FSharpChoice <T1, T2, T3, T4> .Choice3Of4>()).Item)));

            case FSharpChoice <T1, T2, T3, T4> .Tags.Choice4Of4:
                return(FSharpChoice <TR1, TR2, TR3, TR4> .NewChoice4Of4(func4((choice.CastTo <FSharpChoice <T1, T2, T3, T4> .Choice4Of4>()).Item)));
            }
            throw new InvalidOperationException();
        }
コード例 #30
0
        public static TResult Match <T1, T2, T3, T4, TResult>(this FSharpChoice <T1, T2, T3, T4> choice, Func <T1, TResult> func1, Func <T2, TResult> func2, Func <T3, TResult> func3, Func <T4, TResult> func4)
        {
            switch (choice.Tag)
            {
            case FSharpChoice <T1, T2, T3, T4> .Tags.Choice1Of4:
                return(func1((choice.CastTo <FSharpChoice <T1, T2, T3, T4> .Choice1Of4>()).Item));

            case FSharpChoice <T1, T2, T3, T4> .Tags.Choice2Of4:
                return(func2((choice.CastTo <FSharpChoice <T1, T2, T3, T4> .Choice2Of4>()).Item));

            case FSharpChoice <T1, T2, T3, T4> .Tags.Choice3Of4:
                return(func3((choice.CastTo <FSharpChoice <T1, T2, T3, T4> .Choice3Of4>()).Item));

            case FSharpChoice <T1, T2, T3, T4> .Tags.Choice4Of4:
                return(func4((choice.CastTo <FSharpChoice <T1, T2, T3, T4> .Choice4Of4>()).Item));
            }
            throw new InvalidOperationException();
        }