Example #1
0
        public static void LinqRwsTest()
        {
            var cxt = new RwsCxt <Dictionary <int, int>, IList <string>, Monoid.ListAppendImmutableMonoid <string>, int>();

            var computation =
                from x in MaybeT.Lift(cxt.Get())
                from _1 in MaybeT.Lift(cxt.Tell(FuncList.Make("function call 1")))
                from _2 in MaybeT.Lift(cxt.Put(x + 3))
                from env in MaybeT.Lift(cxt.GetEnvironment())
                from _3 in MaybeT.Lift(cxt.Tell(FuncList.Make("function call 2")))
                from twice in MaybeT.HoistRws(cxt, Maybe.JustIf(env.ContainsKey(x), () => env[x]))
                from _4 in MaybeT.Lift(cxt.Tell(FuncList.Make("function call 3")))
                from _5 in MaybeT.Lift(cxt.Put(x + 10))
                from y in MaybeT.Lift(cxt.Get())
                from _6 in MaybeT.Lift(cxt.Put(y + x))
                from goodKey in MaybeT.HoistRws(cxt, Maybe.Just(1))
                select x + twice;

            var(value, state, output) = computation.Run.Run(new Dictionary <int, int> {
                { 1, 2 }, { 4, 8 }, { 16, 32 }
            }, 16);

            Assert.True(value.HasValue);
            Assert.Equal(16 + 32, value.Value());
            Assert.Equal(16 + 26, state);
            Assert.Equal(new[] { "function call 1", "function call 2", "function call 3" }, (IEnumerable <string>)output);

            (value, state, output) = computation.Run.Run(new Dictionary <int, int> {
                { 1, 2 }, { 4, 8 }, { 16, 32 }
            }, 17);

            Assert.False(value.HasValue);
            Assert.Equal(20, state);
            Assert.Equal(new[] { "function call 1", "function call 2" }, (IEnumerable <string>)output);
        }
Example #2
0
        public void FoldTest()
        {
            var fl   = FuncList.Make <int>();
            var fold = L.Make.Folding <FuncList <int>, FuncList <int>, int>(x => x);

            var actual = L.Fold <FuncList <int>, int, int, Group.IntAddGroup>(fold, x => x, fl);

            Assert.Equal(0, actual);

            actual = L.Fold(fold, x => new IntMult(x), fl).Value;
            Assert.Equal(1, actual);

            actual = L.Fold(fold, (x, y) => x * y, 1, fl);
            Assert.Equal(1, actual);

            fl     = FuncList.Make(1, 2, 3, 4);
            actual = L.Fold <FuncList <int>, int, int, Group.IntAddGroup>(fold, x => x, fl);
            Assert.Equal(10, actual);

            actual = L.Fold(fold, x => new IntMult(x), fl).Value;
            Assert.Equal(24, actual);

            actual = L.Fold(fold, (x, y) => x * y, 1, fl);
            Assert.Equal(24, actual);
        }
Example #3
0
        public static void LinqWriterTest()
        {
            var cxt = new WriterCxt <IList <string>, Monoid.ListAppendImmutableMonoid <string> >();

            bool m = true;

            var computation =
                from _1 in MaybeT.Lift(cxt.Tell(FuncList.Make("function call 1")))
                from _2 in MaybeT.Lift(cxt.Tell(FuncList.Make("function call 2")))
                from _3 in MaybeT.HoistWriter(cxt, Maybe.JustIf(m, () => new Unit()))
                from _4 in MaybeT.Lift(cxt.Tell(FuncList.Make("function call 3")))
                select 5;

            var value = computation.Run;

            Assert.True(value.Result.HasValue);
            Assert.Equal(5, value.Result.Value());
            Assert.Equal(new[] { "function call 1", "function call 2", "function call 3" }, (IEnumerable <string>)value.State);

            m           = false;
            computation =
                from _1 in MaybeT.Lift(cxt.Tell(FuncList.Make("function call 1")))
                from _2 in MaybeT.Lift(cxt.Tell(FuncList.Make("function call 2")))
                from _3 in MaybeT.HoistWriter(cxt, Maybe.JustIf(m, () => new Unit()))
                from _4 in MaybeT.Lift(cxt.Tell(FuncList.Make("function call 3")))
                select 5;

            value = computation.Run;

            Assert.False(value.Result.HasValue);
            Assert.Equal(new[] { "function call 1", "function call 2" }, (IEnumerable <string>)value.State);
        }
Example #4
0
        public static void LinqFuncListTest()
        {
            var init        = 5;
            var computation =
                from x in MaybeT.Lift(FuncList.Make("a", "b", "c"))
                from y in MaybeT.Lift(FuncList.Make("c", "d", "e"))
                from _ in MaybeT.HoistFuncList(Maybe.JustIf(init > 0, () => new Unit()))
                from z in MaybeT.Lift(FuncList.Make("f"))
                select x + y + z;

            var value = computation.Run;

            var expected = new[] { "acf", "adf", "aef", "bcf", "bdf", "bef", "ccf", "cdf", "cef" }.Select(x => Maybe.Just(x)).MakeFuncList();

            Assert.Equal(expected, value);

            init        = 0;
            computation =
                from x in MaybeT.Lift(FuncList.Make("a", "b", "c"))
                from y in MaybeT.Lift(FuncList.Make("c", "d", "e"))
                from _ in MaybeT.HoistFuncList(Maybe.JustIf(init > 0, () => new Unit()))
                from z in MaybeT.Lift(FuncList.Make("f"))
                select x + y + z;

            value = computation.Run;

            expected = Enumerable.Repeat(Maybe.Nothing <string>(), 9).MakeFuncList();
            Assert.Equal(expected, value);
        }
Example #5
0
        public static async Task LinqSelectAsyncTest()
        {
            var res =
                from x in Task.FromResult(FuncList.Make(4, 3, 2))
                select x * 2;

            Assert.Equal(new int[] { 8, 6, 4 }, await res);
        }
Example #6
0
        public static void LinqSelectTest()
        {
            var res =
                from x in FuncList.Make(4, 3, 2)
                select x * 2;

            Assert.Equal(new int[] { 8, 6, 4 }, res);
        }
Example #7
0
        public static IEnumerable <object[]> Ap2Data()
        {
            yield return(new object[] {
                FuncList.Make <Func <int, Func <int, int> > >(),
                FuncList.Make <int>(),
                FuncList.Make <int>(),
                FuncList.Make <int>(),
            });

            yield return(new object[] {
                FuncList.Make <Func <int, Func <int, int> > >(),
                FuncList.Make(1),
                FuncList.Make <int>(),
                FuncList.Make <int>(),
            });

            yield return(new object[] {
                FuncList.Make <Func <int, Func <int, int> > >(),
                FuncList.Make(1),
                FuncList.Make(1),
                FuncList.Make <int>(),
            });

            yield return(new object[] {
                FuncList.Make <Func <int, Func <int, int> > >(x => y => x + y),
                FuncList.Make <int>(),
                FuncList.Make <int>(),
                FuncList.Make <int>(),
            });

            yield return(new object[] {
                FuncList.Make <Func <int, Func <int, int> > >(x => y => x + y),
                FuncList.Make(4),
                FuncList.Make(6),
                FuncList.Make(10),
            });

            yield return(new object[] {
                FuncList.Make <Func <int, Func <int, int> > >(x => y => x + y),
                FuncList.Make(1, 2, 3),
                FuncList.Make(7, 8, 9),
                FuncList.Make(8, 9, 10, 9, 10, 11, 10, 11, 12),
            });

            yield return(new object[] {
                FuncList.Make <Func <int, Func <int, int> > >(x => y => x + y, x => y => x * y),
                FuncList.Make(5),
                FuncList.Make(8),
                FuncList.Make(13, 40),
            });

            yield return(new object[] {
                FuncList.Make <Func <int, Func <int, int> > >(x => y => x + y, x => y => x * y),
                FuncList.Make(5, 6, 7),
                FuncList.Make(10, 20, 30),
                FuncList.Make(15, 25, 35, 16, 26, 36, 17, 27, 37, 50, 100, 150, 60, 120, 180, 70, 140, 210),
            });
        }
Example #8
0
        public static void LinqSelectManyTest()
        {
            var res =
                from x in FuncList.Make(2, 3, 5)
                from y in FuncList.Make(7, 11, 13)
                select x *y;

            Assert.Equal(new int[] { 14, 22, 26, 21, 33, 39, 35, 55, 65 }, res);
        }
Example #9
0
        public static async Task LinqSelectManyAsyncTest()
        {
            var res =
                from x in Task.FromResult(FuncList.Make(2, 3, 5))
                from y in Task.FromResult(FuncList.Make(7, 11, 13))
                select x *y;

            Assert.Equal(new int[] { 14, 22, 26, 21, 33, 39, 35, 55, 65 }, await res);
        }
Example #10
0
 public static IEnumerable <object[]> SetOperatorsData()
 {
     yield return(new object[]
     {
         FuncList.Make <Person>(),
         FuncList.Make <Person>(),
         Maybe.Just(0)
     });
 }
Example #11
0
        public static void SelectApForLists(IEnumerable <int> input, IEnumerable <IEnumerable <int> > output)
        {
            var res = input.SelectAp <int, int, FuncList <IEnumerable <int> > >(x => FuncList.Make(x, x * (-1)))
                      .ToFuncList()
                      .Select(x => string.Join(',', x)).ToHashSet();

            var outputSet = output.Select(x => string.Join(',', x)).ToHashSet();

            Assert.Equal(outputSet, res);
        }
Example #12
0
        public static void TraverseTest()
        {
            var m       = Identity.Make(7);
            var actual1 = m.Traverse <FuncList <int>, int>(i => FuncList.Make(i + 1)).Map(x => x.ToIdentity()).ToFuncList();
            var actual2 = m.Map(x => (x + 1).PureUnsafe <int, FuncList <int> >()).ToIdentity().Traverse <FuncList <int>, int>().Map(x => x.ToIdentity()).ToFuncList();

            Assert.Single(actual1);
            Assert.Equal(8, actual1[0].Value);
            Assert.Single(actual2);
            Assert.Equal(8, actual2[0].Value);
        }
Example #13
0
        public static IEnumerable <object[]> EqualsData()
        {
            yield return(new object[] {
                FuncList.Make <int>(),
                FuncList.Make <int>(),
                true
            });

            yield return(new object[] {
                FuncList.Make(1),
                FuncList.Make(1),
                true
            });

            yield return(new object[] {
                FuncList.Make(1, 2, 3),
                FuncList.Make(1, 2, 3),
                true
            });

            yield return(new object[] {
                FuncList.Make(4),
                FuncList.Make <int>(),
                false
            });

            yield return(new object[] {
                FuncList.Make <int>(),
                FuncList.Make(7),
                false
            });

            yield return(new object[] {
                FuncList.Make(1, 2),
                FuncList.Make(5, 6),
                false
            });

            yield return(new object[] {
                FuncList.Make(1, 2),
                FuncList.Make(1),
                false
            });

            yield return(new object[] {
                FuncList.Make(1, 2, 3),
                FuncList.Make(1, 2, 3, 4),
                false
            });
        }
Example #14
0
        public void TraverseMaybeTest()
        {
            var m      = Maybe.Nothing <int>();
            var lens   = L.Make.Traversal <Maybe <int>, Maybe <string>, int, string>();
            var actual = L.Traverse(lens, typeof(FuncList <string>), a => FuncList.Make(a + ""), m).ToFuncList();

            Assert.Single(actual);
            Assert.False(actual[0].HasValue);

            m      = Maybe.Just(6);
            actual = L.Traverse(lens, typeof(FuncList <string>), a => FuncList.Make(a + ""), m).ToFuncList();

            Assert.Single(actual);
            Assert.Equal("6", actual[0].Value());
        }
Example #15
0
        public void ToListTest()
        {
            var fl     = FuncList.Make <int>();
            var fold   = L.Make.Folding <FuncList <int>, FuncList <int>, int>(x => x);
            var actual = L.ToList(fold, fl);

            Assert.Empty(actual);

            fl     = FuncList.Make(1);
            actual = L.ToList(fold, fl);
            Assert.Equal(new int[] { 1 }, actual);

            fl     = FuncList.Make(1, 2, 3);
            actual = L.ToList(fold, fl);
            Assert.Equal(new int[] { 1, 2, 3 }, actual);
        }
Example #16
0
        public void FoldThen()
        {
            var foldOuter = L.Make.Folding <FuncList <FuncList <int> >, FuncList <int> >();
            var foldInner = L.Make.Folding <FuncList <int>, int>();

            var fold = foldOuter.Then(foldInner);

            var fl     = FuncList.Make <FuncList <int> >();
            var actual = L.Fold(fold, (x, y) => x + y, 0, fl);

            Assert.Equal(0, actual);

            fl     = FuncList.Make(FuncList.Make <int>(), FuncList.Make(1, 2, 3), FuncList.Make <int>(), FuncList.Make(4, 5, 6));
            actual = L.Fold(fold, (x, y) => x + y, 0, fl);

            Assert.Equal(21, actual);
        }
Example #17
0
        public static IEnumerable <object[]> Ap1Data()
        {
            yield return(new object[] {
                FuncList.Make <Func <int, int> >(),
                FuncList.Make <int>(),
                FuncList.Make <int>(),
            });

            yield return(new object[] {
                FuncList.Make <Func <int, int> >(),
                FuncList.Make(1),
                FuncList.Make <int>(),
            });

            yield return(new object[] {
                FuncList.Make <Func <int, int> >(x => x + 1),
                FuncList.Make <int>(),
                FuncList.Make <int>(),
            });

            yield return(new object[] {
                FuncList.Make <Func <int, int> >(x => x + 1),
                FuncList.Make(4),
                FuncList.Make(5),
            });

            yield return(new object[] {
                FuncList.Make <Func <int, int> >(x => x + 1),
                FuncList.Make(1, 2, 3),
                FuncList.Make(2, 3, 4),
            });

            yield return(new object[] {
                FuncList.Make <Func <int, int> >(x => x + 1, x => x * 2),
                FuncList.Make(5),
                FuncList.Make(6, 10),
            });

            yield return(new object[] {
                FuncList.Make <Func <int, int> >(x => x + 1, x => x * 2),
                FuncList.Make(5, 6, 7),
                FuncList.Make(6, 7, 8, 10, 12, 14),
            });
        }
Example #18
0
        public static void TraverseTest()
        {
            var arr    = FuncList.Make <int>();
            var actual = arr.Traverse <Maybe <string>, string>(x => Maybe.JustIf(x % 2 == 0, () => x + "_" + x)).ToMaybe();

            Assert.True(actual.HasValue);
            Assert.Empty(actual.Value());

            arr    = FuncList.Make <int>(0, 2, 4, 10, 8);
            actual = arr.Traverse <Maybe <string>, string>(x => Maybe.JustIf(x % 2 == 0, () => x + "_" + x)).ToMaybe();

            Assert.True(actual.HasValue);
            Assert.Equal(new string[] { "0_0", "2_2", "4_4", "10_10", "8_8" }, actual.Value());

            arr    = FuncList.Make <int>(0, 2, 5, 4, 10, 8);
            actual = arr.Traverse <Maybe <string>, string>(x => Maybe.JustIf(x % 2 == 0, () => x + "_" + x)).ToMaybe();

            Assert.False(actual.HasValue);
        }
Example #19
0
        public void TraveseFuncListTest()
        {
            var m      = FuncList.Make <int>();
            var lens   = L.Make.Traversal <FuncList <int>, FuncList <string>, int, string>();
            var actual = L.Traverse(lens, typeof(Maybe <string>), a => Maybe.Just(a + "_" + a), m).ToMaybe();

            Assert.Equal(Array.Empty <string>(), actual.Value());

            m      = FuncList.Make(1, 2, 3);
            actual = L.Traverse(lens, typeof(Maybe <string>), a => Maybe.Just(a + "_" + a), m).ToMaybe();

            Assert.Single(actual);
            Assert.Equal(new string[] { "1_1", "2_2", "3_3" }, actual.Value());

            actual = L.Traverse(lens, a => Maybe.Just(a + "_" + a), m).ToMaybe();
            Assert.Equal(new string[] { "1_1", "2_2", "3_3" }, actual.Value());

            actual = L.Traverse(lens, typeof(Maybe <string>), a => Maybe.JustIf(a % 2 == 0, () => a + "_" + a), m).ToMaybe();
            Assert.False(actual.HasValue);
        }
Example #20
0
        public static void TraverseTest()
        {
            var m       = Maybe.Nothing <int>();
            var actual1 = m.Traverse <FuncList <int>, int>(i => FuncList.Make(i + 1)).Map(x => x.ToMaybe()).ToFuncList();
            var actual2 = m.Map(x => (x + 1).PureUnsafe <int, FuncList <int> >()).ToMaybe().Traverse <FuncList <int>, int>().Map(x => x.ToMaybe()).ToFuncList();

            Assert.Single(actual1);
            Assert.False(actual1[0].HasValue);
            Assert.Single(actual2);
            Assert.False(actual2[0].HasValue);

            m       = Maybe.Just(7);
            actual1 = m.Traverse <FuncList <int>, int>(i => FuncList.Make(i + 1)).Map(x => x.ToMaybe()).ToFuncList();
            actual2 = m.Map(x => (x + 1).PureUnsafe <int, FuncList <int> >()).ToMaybe().Traverse <FuncList <int>, int>().Map(x => x.ToMaybe()).ToFuncList();

            Assert.Single(actual1);
            Assert.Equal(8, actual1[0].Value());
            Assert.Single(actual2);
            Assert.Equal(8, actual2[0].Value());
        }
Example #21
0
        public void WitherTest()
        {
            var lens = L.Make.Withering <FuncList <string>, FuncList <int>, string, int>();

            var actual = L.TraverseMaybe(lens, typeof(Maybe <int>), s => Maybe.JustIf(s.Length % 2 == 0, () => s.Length), FuncList.Make <string>()).ToMaybe();

            Assert.Equal(new int[] { }, actual.Value());

            actual = L.TraverseMaybe(lens, typeof(Maybe <int>), s => Maybe.JustIf(s.Length % 2 == 0, () => s.Length), FuncList.Make("a")).ToMaybe();
            Assert.Equal(new int[] { }, actual.Value());

            actual = L.TraverseMaybe(lens, typeof(Maybe <int>), s => Maybe.JustIf(s.Length % 2 == 0, () => s.Length), FuncList.Make("bb")).ToMaybe();
            Assert.Equal(new int[] { 2 }, actual.Value());

            actual = L.TraverseMaybe(lens, typeof(Maybe <int>), s => Maybe.JustIf(s.Length % 2 == 0, () => s.Length), FuncList.Make("a", "bb", "cccc", "", "gg")).ToMaybe();
            Assert.Equal(new int[] { 2, 4, 0, 2 }, actual.Value());
        }
Example #22
0
        public async static void SelectApAsyncForLists(IEnumerable <int> input, IEnumerable <IEnumerable <int> > output)
        {
            var res = (await input.SelectApAsync <int, int, FuncList <IEnumerable <int> > >(async x => { await Task.Delay(500); return(FuncList.Make(x, x * (-1))); }))
                      .ToFuncList()
                      .Select(x => string.Join(',', x)).ToHashSet();

            var outputSet = output.Select(x => string.Join(',', x)).ToHashSet();

            Assert.Equal(outputSet, res);
        }
Example #23
0
        public static void LinqTellIntSelectTest()
        {
            var rws =
                from env in Rws.GetEnvironment <Dictionary <int, string>, IList <int>, Monoid.ListAppendImmutableMonoid <int>, int>()
                from s in Rws.Get <Dictionary <int, string>, IList <int>, Monoid.ListAppendImmutableMonoid <int>, int>()
                from _ in Rws.Put <Dictionary <int, string>, IList <int>, Monoid.ListAppendImmutableMonoid <int>, int>(s + 1)
                from __ in Rws.Tell <Dictionary <int, string>, IList <int>, Monoid.ListAppendImmutableMonoid <int>, int>(FuncList.Make(28))
                select env[s] + 2;

            var dict = new Dictionary <int, string>
            {
                { 5, "e" }
            };

            var(res, state, w) = rws.Run(dict, 5);

            Assert.Equal("e2", res);
            Assert.Equal(6, state);
            Assert.Equal(new int[] { 28 }, w);
        }
Example #24
0
        public async static void WhereApAsyncForLists(IEnumerable <int> input, IEnumerable <IEnumerable <int> > output)
        {
            var res = (await input.WhereApAsync <int, FuncList <bool>, FuncList <IEnumerable <int> > >(async _ => { await Task.Delay(500); return(FuncList.Make(false, true)); }))
                      .Select(x => string.Join(',', x)).ToHashSet();

            var outputSet = output.Select(x => string.Join(',', x)).ToHashSet();

            Assert.Equal(outputSet, res);
        }
Example #25
0
        public static void WhereApForLists(IEnumerable <int> input, IEnumerable <IEnumerable <int> > output)
        {
            var res = input.WhereAp <int, FuncList <bool>, FuncList <IEnumerable <int> > >(_ => FuncList.Make(false, true))
                      .Select(x => string.Join(',', x)).ToHashSet();

            var outputSet = output.Select(x => string.Join(',', x)).ToHashSet();

            Assert.Equal(outputSet, res);
        }
Example #26
0
        public void TraverseThenTest()
        {
            var outer = L.Make.Traversal <FuncList <FuncList <int> >, FuncList <FuncList <string> >, FuncList <int>, FuncList <string> >();
            var inner = L.Make.Traversal <FuncList <int>, FuncList <string>, int, string>();

            var combined = outer.Then(inner);

            var actual = L.Traverse(combined, typeof(Maybe <string>), i => Maybe.Just(i + ""), FuncList.Make <FuncList <int> >()).ToMaybe();

            Assert.Empty(actual.Value());

            actual = L.Traverse(combined, typeof(Maybe <string>), i => Maybe.Just(i + ""), FuncList.Make(FuncList.Make <int>())).ToMaybe();

            Assert.Single(actual.Value());
            Assert.Empty(actual.Value()[0]);

            actual = L.Traverse(combined, typeof(Maybe <string>), i => Maybe.JustIf(i % 2 == 0, () => i + ""), FuncList.Make(FuncList.Make(0, 2, 4, 6), FuncList.Make(10, 12))).ToMaybe();

            Assert.Equal(2, actual.Value().Count);
            Assert.Equal(new string[] { "0", "2", "4", "6" }, actual.Value()[0]);
            Assert.Equal(new string[] { "10", "12" }, actual.Value()[1]);

            actual = L.Traverse(combined, typeof(Maybe <string>), i => Maybe.JustIf(i % 2 == 0, () => i + ""), FuncList.Make(FuncList.Make(0, 1, 2, 4, 6), FuncList.Make(10, 12))).ToMaybe();

            Assert.False(actual.HasValue);
        }
Example #27
0
        public void WitherThenTest()
        {
            var lensOuter = L.Make.Withering <FuncList <FuncList <string> >, FuncList <FuncList <int> >, FuncList <string>, FuncList <int> >();
            var lensInner = L.Make.Withering <FuncList <string>, FuncList <int>, string, int>();

            var lens = lensOuter.Then(lensInner);

            var actual = L.TraverseMaybe(lens, typeof(Maybe <int>), s => Maybe.JustIf(s.Length % 2 == 0, () => s.Length), FuncList.Make <FuncList <string> >()).ToMaybe();

            Assert.Empty(actual.Value());

            actual = L.TraverseMaybe(lens, typeof(Maybe <int>), s => Maybe.JustIf(s.Length % 2 == 0, () => s.Length), FuncList.Make(FuncList.Make <string>())).ToMaybe();
            Assert.Single(actual.Value());
            Assert.Equal(new int[] { }, actual.Value()[0]);

            actual = L.TraverseMaybe(lens, typeof(Maybe <int>), s => Maybe.JustIf(s.Length % 2 == 0, () => s.Length), FuncList.Make(FuncList.Make("a", "bb", "cccc", "", "gg"), FuncList.Make <string>(), FuncList.Make("hhhh"))).ToMaybe();
            Assert.Equal(3, actual.Value().Count);
            Assert.Equal(new int[] { 2, 4, 0, 2 }, actual.Value()[0]);
            Assert.Equal(new int[] { }, actual.Value()[1]);
            Assert.Equal(new int[] { 4 }, actual.Value()[2]);
        }