コード例 #1
0
        public static void RunSimpleInvertibleDemo()
        {
            Source      source             = new Source();
            Destination dest               = new Destination();
            InvertibleArrow <int, int> arr = Op.Arr((int x) => x + 1, (int x) => x - 1);

            BindingsManager.CreateBinding(source.GetBindPoint("source"), arr, dest.GetBindPoint("result"));

            bool   passed = true;
            Random rand   = new Random();

            for (int i = 0; i < 100; i++)
            {
                int next = rand.Next();
                source.source = next;
                if (dest.result != next + 1)
                {
                    passed = false;
                }
                dest.result -= 1;
                if (source.source != next - 1)
                {
                    passed = false;
                }
            }

            if (passed)
            {
                Console.WriteLine("Invertible works too!");
            }
            else
            {
                Console.WriteLine("Invertible doesn't work tho D:");
            }
        }
コード例 #2
0
 public static void blahblah()
 {
     Arrow <int, double> pythagoras = Op.LiftA2((int x, int y) => x + y,
                                                Op.Arr((int x) => x * x),
                                                Op.Arr((int y) => y * y))
                                      .Combine(Op.Arr((int x) => Math.Sqrt(x)));
 }
コード例 #3
0
        protected override void InitialiseArrow()
        {
            var square = Op.Arr((int x) => x * x);
            Func <int, int, int> add = ((int x, int y) => x + y);

            arrow = Op.LiftA2(add, square, square).Combine(Op.Arr((int x) => (int)Math.Sqrt(x)));
        }
コード例 #4
0
 public void InitialiseArrows()
 {
     progressArrow = Op.Arr((int x) => 50.0 + (50.0*Math.Sin(x / 30.0)));
     var sinArrow = progressArrow.Combine(Op.Arr((double x) => (int)x));
     var textSizeArrow = Op.Arr((int length) => new String('☺', length));
     arrow = sinArrow.Combine(textSizeArrow);
 }
コード例 #5
0
        public static void RunSimpleMultiDemo()
        {
            Source      source = new Source();
            Destination dest   = new Destination();

            Arrow <Tuple <int, int>, int> multiplier = Op.Arr((Tuple <int, int> x) => x.Item1 * x.Item2);

            Arrow <int, int>     square = Op.Arr((int x) => x * x);
            Func <int, int, int> add    = (int x, int y) => x + y;

            Arrow <Tuple <int, int>, int> pythagoras = Op.And(square, square)
                                                       .Unsplit(add)
                                                       .Combine(Op.Arr((int x) => (int)Math.Sqrt(x)));

            BindingsManager.CreateBinding(BindingsManager.BindPoints(new BindPoint(source, "source"), new BindPoint(source, "multiplies")),
                                          pythagoras,
                                          BindingsManager.BindPoints(new BindPoint(dest, "result")));
            source.multiplies = 2;
            source.source     = 3;

            if (dest.result == pythagoras.Invoke(Tuple.Create(source.source, source.multiplies)))
            {
                Console.WriteLine("Multibindings in one direction work :)");
            }
            else
            {
                Console.WriteLine("Ohnoes multibindings in one direction don't work");
            }
        }
コード例 #6
0
        public static bool TestDoubleInversion()
        {
            var arr1 = Op.Arr((int x) => x * 7, (int y) => y / 7);
            var arr2 = arr1.Invert().Invert();

            return(ArrowTestUtils.AssertArrowsGiveSameOutput(arr1, arr2));
        }
コード例 #7
0
        protected override void InitialiseArrow()
        {
            var square = Op.Arr((int x) => x * x);
            Func <int, int, int> add = ((int x, int y) => x + y);

            //arrow = Op.LiftA2(add, square, square).Combine(Op.Arr((int x) => (int)Math.Sqrt(x)));
            //arrow = Op.Split<int>().Combine(square.And(square)).Unsplit(add).Combine(Op.Arr((int x) => (int)Math.Sqrt(x)));
            arrow = Op.Arr((int x) => Tuple.Create(x * x, x * x)).Unsplit(add).Combine(Op.Arr((int x) => (int)Math.Sqrt(x)));
        }
コード例 #8
0
        public static bool TestFirstInversion()
        {
            var arrow = Op.Arr((int x) => x + 54, (int y) => y - 54);

            var invFirst = arrow.First(default(int)).Invert();
            var firstInv = arrow.Invert().First(default(int));

            return(ArrowTestUtils.AssertPairInvertibleArrowsGiveSameOutput(invFirst, firstInv));
        }
コード例 #9
0
        public static bool TestInversionDistributivity()
        {
            var arr1 = Op.Arr((int x) => x * 9 - 5, (int y) => (y + 5) / 9);
            var arr2 = Op.Arr((int x) => x * 10 - 4, (int y) => (y + 4) / 10);

            var undistributed = (arr1.Combine(arr2)).Invert();
            var distributed   = (arr2.Invert()).Combine(arr1.Invert());

            return(ArrowTestUtils.AssertInvertibleArrowsGiveSameOutput(undistributed, distributed));
        }
コード例 #10
0
        public static bool TestCompositionWithIdentity()
        {
            var id = new InvertibleIDArrow <int>();
            var f  = Op.Arr((int x) => x * 7 - 1,
                            (int x) => (x + 1) / 7);

            var fID = f.Combine(id);
            var idF = id.Combine(f);

            return(ArrowTestUtils.AssertInvertibleArrowsGiveSameOutput(fID, idF));
        }
コード例 #11
0
        public static bool TestLaw2()
        {
            /*
             * Similar to law 1 but checking that Op.Arr(f).Combine(new IDArrow<T>()) = Op.Arr(f)
             */

            Arrow <int, int> arrF     = Op.Arr((int x) => (x * x) - 5);
            Arrow <int, int> id       = new IDArrow <int>();
            Arrow <int, int> combined = arrF.Combine(id);

            return(ArrowTestUtils.AssertArrowsGiveSameOutput(arrF, combined));
        }
コード例 #12
0
        public static bool TestLaw5()
        {
            /*
             * first (arr f) = arr (f x id)
             */

            Func <int, int> f = ArrowTestUtils.GenerateFunc();
            Arrow <Tuple <int, int>, Tuple <int, int> > firstArr = Op.Arr(f).First(default(int));
            Arrow <Tuple <int, int>, Tuple <int, int> > arrFId   = Op.Arr(
                (Tuple <int, int> x) => Tuple.Create(f(x.Item1), x.Item2));

            return(ArrowTestUtils.AssertPairArrowsGiveSameOutput(firstArr, arrFId));
        }
コード例 #13
0
        public void InitialiseCircleArrow(int xRadius, int yRadius, int cX, int cY)
        {
            Arrow <int, Tuple <int, int> > timeDup = Op.Split <int>();

            Arrow <int, double> sin = Op.Arr((int x) => cX + xRadius * Math.Sin((double)x / 20));
            Arrow <int, double> cos = Op.Arr((int y) => cY + yRadius * Math.Cos((double)y / 20));
            Arrow <Tuple <int, int>, Tuple <double, double> > sinCos = sin.And(cos);

            Arrow <double, int> doubleToInt = Op.Arr((double x) => (int)x);
            Arrow <Tuple <double, double>, Tuple <int, int> > adapter = doubleToInt.And(doubleToInt);

            circle = timeDup.Combine(sinCos).Combine(adapter);
        }
コード例 #14
0
        public static bool TestLaw1()
        {
            /*
             * Tests that IDArrow<T>.Combine(Op.Arr(f)) = Op.Arr(f)
             * That is, that the identity arrow combined with another arrow is exactly the same as
             * the other arrow on its own
             */

            Arrow <int, int> arrF     = Op.Arr((int x) => (x * x) - 5);
            Arrow <int, int> id       = new IDArrow <int>();
            Arrow <int, int> combined = id.Combine(arrF);

            return(ArrowTestUtils.AssertArrowsGiveSameOutput(arrF, combined));
        }
コード例 #15
0
        public static bool TestFirstCommutativity()
        {
            var f  = new Func <int, int>(x => x + 1);
            var g  = new Func <int, int>(y => y - 1);
            var id = new Func <int, int>(x => x);

            var firstArrow = Op.Arr((int x) => x * 7, (int y) => y / 7).First(default(int));
            var fgArrow    = id.Mult(f).Arr(id.Mult(g));

            var firstArrowFirst = firstArrow.Combine(fgArrow);
            var firstArrowLast  = fgArrow.Combine(firstArrow);

            return(ArrowTestUtils.AssertPairInvertibleArrowsGiveSameOutput(firstArrowFirst, firstArrowLast));
        }
コード例 #16
0
        public static bool TestFirstPipingSimplification()
        {
            /*
             * Tests that piping a function before type simplification is equivalent to simplifying
             * type before connecting to the unpiped function.
             * That is, first f >>> arr ((s,t) -> s) = arr ((s,t) -> s) >>> f
             */

            Arrow <int, int> f = Op.Arr(ArrowTestUtils.GenerateFunc());
            Arrow <Tuple <int, int>, int> firstFArr = Op.First <int, int, int>(f).Combine(Op.Arr((Tuple <int, int> x) => x.Item1));
            Arrow <Tuple <int, int>, int> arrF      = Op.Arr((Tuple <int, int> x) => x.Item1).Combine(f);

            return(ArrowTestUtils.AssertPairToSingleArrowsGiveSameOutput(firstFArr, arrF));
        }
コード例 #17
0
        public static bool TestLaw8()
        {
            /*
             * Tests that a split arrow commutes with the 'fst' function (which returns the first
             * element of a tuple), that is:
             * first f >>> arr fst = arr fst >>> f
             */

            Arrow <int, int> f = Op.Arr(ArrowTestUtils.GenerateFunc());
            Arrow <Tuple <int, int>, int> fstArrow     = Op.Arr((Tuple <int, int> x) => x.Item1);
            Arrow <Tuple <int, int>, int> firstFArrFst = f.First(default(int)).Combine(fstArrow);
            Arrow <Tuple <int, int>, int> arrFstF      = fstArrow.Combine(f);

            return(ArrowTestUtils.AssertPairToSingleArrowsGiveSameOutput(firstFArrFst, arrFstF));
        }
コード例 #18
0
        public static bool TestArrOperatorDistributivity()
        {
            /*
             * Tests that the Arr operator distributes over function composition.
             * That is, g(f(x)) = (arr f) >>> (arr g)
             * We test this by comparing a lambda combination of two functions with their arrow
             * combination.
             */

            Func <int, int>  f   = ArrowTestUtils.GenerateFunc();
            Func <int, int>  g   = ArrowTestUtils.GenerateFunc();
            Func <int, int>  fg  = (x => g(f(x)));
            Arrow <int, int> arr = Op.Arr(f).Combine(Op.Arr(g));

            return(ArrowTestUtils.AssertArrowEqualsFunc(arr, fg));
        }
コード例 #19
0
        public static bool TestLaw6()
        {
            /*
             * Tests that the First operator distributes over arrow composition
             * first (f >>> g) = first f >>> first g
             */

            Arrow <int, int> f = Op.Arr(ArrowTestUtils.GenerateFunc());
            Arrow <int, int> g = Op.Arr(ArrowTestUtils.GenerateFunc());

            Arrow <Tuple <int, int>, Tuple <int, int> > firstOutside     = f.Combine(g).First(default(int));
            Arrow <Tuple <int, int>, Tuple <int, int> > firstDistributed = f.First(default(int))
                                                                           .Combine(g.First(default(int)));

            return(ArrowTestUtils.AssertPairArrowsGiveSameOutput(firstOutside, firstDistributed));
        }
コード例 #20
0
        public static bool TestLaw4()
        {
            /*
             * Tests that the arrow of a composed pair of functions is equal to the composition of
             * arrows made from the individual functions, ie:
             * arr (g · f) = arr f >>> arr g
             */

            Func <int, int> f = ArrowTestUtils.GenerateFunc();
            Func <int, int> g = ArrowTestUtils.GenerateFunc();

            Arrow <int, int> arrowCompose  = Op.Arr((int x) => g(f(x)));
            Arrow <int, int> composeArrows = Op.Arr(f).Combine(Op.Arr(g));

            return(ArrowTestUtils.AssertArrowsGiveSameOutput(arrowCompose, composeArrows));
        }
コード例 #21
0
        public static bool TestLaw3()
        {
            /*
             * Tests that (f.Combine(g)).Combine(h) = f.Combine(g.Combine(h))
             * That is, arrow combination is associative
             */

            Arrow <int, int> arrF = Op.Arr((int x) => (x * x) - 5);
            Arrow <int, int> arrG = Op.Arr((int x) => (x + 5) * 7);
            Arrow <int, int> arrH = Op.Arr((int x) => (x - 50) * x);

            Arrow <int, int> fgH = (arrF.Combine(arrG)).Combine(arrH);
            Arrow <int, int> fGH = arrF.Combine(arrG.Combine(arrH));

            return(ArrowTestUtils.AssertArrowsGiveSameOutput(fgH, fGH));
        }
コード例 #22
0
        public static bool TestPipingCommutativity()
        {
            /*
             * If an identity is merged with a second function to form an arrow, attaching it to a
             * piped function must be commutative. In code:
             * arr (id *** g) >>> first f = first f >>> arr (id *** g)
             * This is tested by constructing the two arrows and checking their outputs match.
             */

            Arrow <int, int> f = Op.Arr(ArrowTestUtils.GenerateFunc());
            Arrow <int, int> g = Op.Arr(ArrowTestUtils.GenerateFunc());

            Arrow <Tuple <int, int>, Tuple <int, int> > mergeFirst = new IDArrow <int>().And(g).Combine(f.First(default(int)));
            Arrow <Tuple <int, int>, Tuple <int, int> > firstMerge = f.First(default(int)).Combine(new IDArrow <int>().And(g));

            return(ArrowTestUtils.AssertPairArrowsGiveSameOutput(mergeFirst, firstMerge));
        }
コード例 #23
0
        public static bool TestArrFirstOrderingIrrelevance()
        {
            /*
             * Tests that the First and Arr operators, used in conjunction, will have the same
             * effect regardless of ordering. That is:
             * arr (first f) = first (arr f)
             */

            Func <int, int> f = ArrowTestUtils.GenerateFunc();
            Arrow <Tuple <int, int>, Tuple <int, int> > arrFirst = Op.Arr(
                (Tuple <int, int> x) =>
                Tuple.Create(f(x.Item1), x.Item2)
                );
            Arrow <Tuple <int, int>, Tuple <int, int> > firstArr = Op.First(Op.Arr(f), default(int));

            return(ArrowTestUtils.AssertPairArrowsGiveSameOutput(arrFirst, firstArr));
        }
コード例 #24
0
        public static bool TestFirstOperatorDistributivity()
        {
            /*
             * Tests that the First operator distributes over function competition, ie:
             * first (f >>> g) = first f >>> first g
             * This test is done using two arrows on pairs, f and g
             */

            Arrow <int, int> f = Op.Arr(ArrowTestUtils.GenerateFunc());
            Arrow <int, int> g = Op.Arr(ArrowTestUtils.GenerateFunc());

            Arrow <Tuple <int, int>, Tuple <int, int> > firstFG =
                Op.First(f.Combine(g), default(int));

            Arrow <Tuple <int, int>, Tuple <int, int> > firstFfirstG =
                Op.First(f, default(int)).Combine(Op.First(g, default(int)));

            return(ArrowTestUtils.AssertPairArrowsGiveSameOutput(firstFG, firstFfirstG));
        }
コード例 #25
0
        public static bool TestPipingReassociation()
        {
            /*
             * Tests the following thing:
             * first (first f) >>> arr assoc = arr assoc >>> first f
             * The code itself is probably more expressive than any explanation I could come up
             * with.
             */

            Arrow <int, int>           f     = Op.Arr(ArrowTestUtils.GenerateFunc());
            AssocArrow <int, int, int> assoc = new AssocArrow <int, int, int>();

            Arrow <Tuple <Tuple <int, int>, int>, Tuple <int, Tuple <int, int> > > firstFirstArr =
                f.First(default(int)).First(default(int))
                .Combine(assoc);
            Arrow <Tuple <Tuple <int, int>, int>, Tuple <int, Tuple <int, int> > > arrFirst =
                assoc.Combine(f.First(default(Tuple <int, int>)));

            return(ArrowTestUtils.AssertReassociationArrowsGiveSameOutput(firstFirstArr, arrFirst));
        }
コード例 #26
0
 public void InitialiseArrow()
 {
     nameArrow = Op.Arr((string x) => Tuple.Create(x.Split()[0], x.Split()[1]),
                        (Tuple <string, string> splitName) =>
                        String.Format("{0} {1}", splitName.Item1, splitName.Item2));
 }
コード例 #27
0
ファイル: Quadratic.cs プロジェクト: g4idrijs/C-sharp-arrows
 protected override void InitialiseArrow()
 {
     arrow = Op.Arr((int x) => (x * x) + (2 * x) - 5);
 }
コード例 #28
0
 public static void blah()
 {
     Arrow <int, string> test  = Op.Arr((int x) => x + 1).Combine(Op.Arr((int x) => x.ToString()));
     Arrow <int, string> test1 = Op.Combine(Op.Arr((int x) => x + 1), Op.Arr((int x) => x.ToString()));
 }
コード例 #29
0
 protected override void InitialiseArrow()
 {
     arrow = Op.Arr((int x) => (int)(Math.Atan(x) * (180.0 / Math.PI)));
 }
コード例 #30
0
 protected override void InitialiseArrow()
 {
     arrow = Op.Arr((int x) => x + 1);
 }