Esempio n. 1
0
        public static Unit writeTree(IRoseTree <string, Move> tree)
        {
            void copy(Move m)
            {
                Directory.CreateDirectory(m.Destination.DirectoryName);
                m.Source.CopyTo(m.Destination.FullName);
                Console.WriteLine($"Copied to {m.Destination.FullName}");
            }

            bool compareFiles(Move m)
            {
                var sourceStream      = File.ReadAllBytes(m.Source.FullName);
                var destinationStream = File.ReadAllBytes(m.Destination.FullName);

                return(sourceStream == destinationStream);
            }

            Unit move(Move m)
            {
                copy(m);
                if (compareFiles(m))
                {
                    m.Source.Delete();
                }
                return(new Unit());
            }

            tree.Iter(move);
            return(Unit());
        }
Esempio n. 2
0
 public static TResult Cata <N, L, TResult>(
     this IRoseTree <N, L> tree,
     Func <N, IEnumerable <TResult>, TResult> node,
     Func <L, TResult> leaf)
 {
     return(tree.Accept(new CataVisitor <N, L, TResult>(node, leaf)));
 }
Esempio n. 3
0
 public static TResult Cata <N, L, TResult>(
     this IRoseTree <N, L> tree,
     Func <N, IEnumerable <TResult>, TResult> node,
     Func <L, TResult> leaf)
 => tree.Match(
     node: (n, branches) => node(n, branches.Select(t => t.Cata(node, leaf))),
     leaf: leaf
     );
Esempio n. 4
0
        public void MoveToDestination(IRoseTree <string, PhotoFile> source, string destination, IRoseTree <string, FileInfo> expected)
        {
            var actual      = Archive.moveTo(destination, source);
            var expectedVal = expected.Select(i => i.ToString());
            var actualVal   = actual.Select(i => i.ToString());

            Assert.Equal(expectedVal, actualVal);
        }
Esempio n. 5
0
 public static IEnumerable <L> FindAll <N, L>(this IRoseTree <N, L> source, Func <L, bool> f)
 => source.Match(
     node: (x, xs) => xs.ToList().Aggregate(new List <L>(), (acc, t) => acc.Concat(t.FindAll(f)).ToList()),
     leaf: l => f(l) ? new List <L>()
 {
     l
 } : new List <L>()
     );
Esempio n. 6
0
        public void CalculateMoves(IRoseTree <string, FileInfo> source, IRoseTree <string, Move> expected)
        {
            var actual      = Archive.calculateMoves(source);
            var expectedVal = expected.Select(i => (i.Source.ToString(), i.Destination.ToString()));
            var actualVal   = actual.Select(i => (i.Source.ToString(), i.Destination.ToString()));

            Assert.Equal(expectedVal, actualVal);
        }
Esempio n. 7
0
 public static IRoseTree <N1, L1> BiMap <N, N1, L, L1>(
     this IRoseTree <N, L> source,
     Func <N, N1> mapNode,
     Func <L, L1> mapLeaf)
 => source.Cata(
     node: (n, branches) => new RoseNode <N1, L1>(mapNode(n), branches),
     leaf: l => (IRoseTree <N1, L1>) new RoseLeaf <N1, L1>(mapLeaf(l))
     );
Esempio n. 8
0
 public static TResult Fold <N, L, TResult>(
     this IRoseTree <N, L> source,
     Func <TResult, L, TResult> g,
     TResult acc
     )
 => source.Match(
     node: (x, xs) => xs.ToList().Aggregate(acc, (ac, t) => t.Fold(g, ac)),
     leaf: l => g(acc, l)
     );
Esempio n. 9
0
        public void SecondFunctorLawHoldsForMapLeaf(IRoseTree <int, string> t)
        {
            bool f(int x) => x % 2 == 0;
            int g(string s) => s.Length;

            Assert.Equal(
                t.MapLeaf(x => f(g(x))),
                t.MapLeaf(g).MapLeaf(f));
        }
Esempio n. 10
0
        public void SecondFunctorLawHoldsForMapNode(IRoseTree <int, string> t)
        {
            char f(bool b) => b ? 'T' : 'F';
            bool g(int i) => i % 2 == 0;

            Assert.Equal(
                t.MapNode(x => f(g(x))),
                t.MapNode(g).MapNode(f));
        }
Esempio n. 11
0
 // Bifunctor
 public static IRoseTree <N1, L1> SelectBoth <N, N1, L, L1>(
     this IRoseTree <N, L> source,
     Func <N, N1> selectNode,
     Func <L, L1> selectLeaf)
 {
     return(source.Cata(
                node: (n, branches) => new RoseNode <N1, L1>(selectNode(n), branches),
                leaf: l => (IRoseTree <N1, L1>) new RoseLeaf <N1, L1>(selectLeaf(l))));
 }
Esempio n. 12
0
        public void ConsistencyLawHolds(IRoseTree <int, string> t)
        {
            DateTime f(int i) => new DateTime(i);
            bool g(string s) => string.IsNullOrWhiteSpace(s);

            Assert.Equal(t.BiMap(f, g), t.MapLeaf(g).MapNode(f));
            Assert.Equal(
                t.MapNode(f).MapLeaf(g),
                t.MapLeaf(g).MapNode(f));
        }
Esempio n. 13
0
        public void MatchNode()
        {
            IRoseTree <string, int> tree =
                RoseTree.Node("foo",
                              new RoseLeaf <string, int>(42),
                              new RoseLeaf <string, int>(1337));
            int actual = tree.Accept(new MatchStringNodeVisitor());

            Assert.Equal(3, actual);
        }
Esempio n. 14
0
        public void MapBothCompositionLawHolds(IRoseTree <int, string> t)
        {
            char f(bool b) => b ? 'T' : 'F';
            bool g(int x) => x % 2 == 0;
            bool h(int x) => x % 2 == 0;
            int i(string s) => s.Length;

            Assert.Equal(
                t.BiMap(x => f(g(x)), y => h(i(y))),
                t.BiMap(g, i).BiMap(f, h));
        }
Esempio n. 15
0
        public void CataNode()
        {
            IRoseTree <string, int> tree =
                RoseTree.Node(
                    "foo",
                    new RoseLeaf <string, int>(42),
                    new RoseLeaf <string, int>(1337));
            int actual = tree.Cata((x, xs) => x.Length + xs.Sum(), x => x);

            Assert.Equal(1382, actual);
        }
Esempio n. 16
0
        public void UseMeertensTree()
        {
            IRoseTree <Unit, int> meertensTree =
                RoseTree.Node(Unit.Instance,
                              RoseTree.Node(Unit.Instance,
                                            RoseTree.Node(Unit.Instance,
                                                          new RoseLeaf <Unit, int>(2112)),
                                            new RoseLeaf <Unit, int>(42),
                                            new RoseLeaf <Unit, int>(1337),
                                            new RoseLeaf <Unit, int>(90125)),
                              RoseTree.Node(Unit.Instance,
                                            new RoseLeaf <Unit, int>(1984)),
                              new RoseLeaf <Unit, int>(666));

            Assert.False(meertensTree.IsLeaf().ToBool());
        }
Esempio n. 17
0
        public static IRoseTree <string, Move> calculateMoves(IRoseTree <string, FileInfo> tree)
        {
            FileInfo ReplaceDirectory(FileInfo f, string d)
            => new FileInfo(Path.Combine(d, f.Name));

            IRoseTree <string, Move> imp(string path, IRoseTree <string, FileInfo> tree)
            => tree.Match(
                leaf: l => RoseTree.Leaf <string, Move>(new Move {
                Source = l, Destination = ReplaceDirectory(l, path)
            }),
                node: (x, xs) =>
            {
                var newPath = Path.Combine(path, x);
                return(RoseTree.Node <string, Move>(newPath, xs.Select(t => imp(newPath, t)).ToArray()));
            });

            return(imp("", tree));
        }
Esempio n. 18
0
        public static IRoseTree <string, FileInfo> moveTo(string destination, IRoseTree <string, PhotoFile> t)
        {
            string dirNameOf(DateTime dt) => dt.ToString("yyyy-MM");


            Dictionary <string, IEnumerable <FileInfo> > groupByDir(Dictionary <string, IEnumerable <FileInfo> > m, PhotoFile pf)
            {
                var key    = dirNameOf(pf.TakenOn);
                var exists = m.TryGetValue(key, out var item);
                IEnumerable <FileInfo> dir;

                if (exists)
                {
                    dir    = item;
                    m[key] = dir.Concat(new List <FileInfo> {
                        pf.File
                    });
                }
                else
                {
                    m.Add(key, new List <FileInfo> {
                        pf.File
                    });
                }

                return(m);
            }

            List <IRoseTree <string, FileInfo> > addDir(IEnumerable <IRoseTree <string, FileInfo> > dirs, KeyValuePair <string, IEnumerable <FileInfo> > pair)
            {
                var name     = pair.Key;
                var files    = pair.Value;
                var branches = files.Select(f => RoseTree.Leaf <string, FileInfo>(f));

                return(dirs.Concat(new[] { RoseTree.Node(name, branches.ToArray()) }).ToList());
            }

            var m = t.Fold((acc, x) => groupByDir(acc, x), new Dictionary <string, IEnumerable <FileInfo> >());

            var dirs = m.Aggregate(new List <IRoseTree <string, FileInfo> >(), addDir);

            return(RoseTree.Node(destination, dirs.ToArray()));
        }
Esempio n. 19
0
        public void test()
        {
            IRoseTree <string, Command> editMenu =
                from name in editMenuTemplate
                select commandStore.Lookup(name).GetOrElse(new Command(name));

            Maybe <Command> find;


            using StringWriter sw = new StringWriter();

            Console.SetOut(sw);
            find = editMenu.Find(e => e.Name == "Find");
            var findAll = editMenu.FindAll(e => e.Name == "Find" || e.Name == "Copy");

            var concat = editMenu.Fold((acc, c) => acc + c.Name, "");

            find.Match(Nothing: () => { }, Just: c => c.Execute());

            Assert.Equal($"Find\r\n", sw.ToString());
        }
Esempio n. 20
0
        public void MenuExample()
        {
            IRoseTree <string, string> editMenuTemplate =
                RoseTree.Node("Edit",
                              RoseTree.Node("Find and Replace",
                                            new RoseLeaf <string, string>("Find"),
                                            new RoseLeaf <string, string>("Replace")),
                              RoseTree.Node("Case",
                                            new RoseLeaf <string, string>("Upper"),
                                            new RoseLeaf <string, string>("Lower")),
                              new RoseLeaf <string, string>("Cut"),
                              new RoseLeaf <string, string>("Copy"),
                              new RoseLeaf <string, string>("Paste"));

            var commandStore = new CommandStore();
            IRoseTree <string, Command> editMenu =
                from name in editMenuTemplate
                select commandStore.Lookup(name);

            var roundTripped = from command in editMenu
                               select command.Name;

            Assert.Equal(editMenuTemplate, roundTripped);
        }
Esempio n. 21
0
 public static Maybe <IRoseTree <N, L1> > Choose <N, L, L1>(
     this IRoseTree <N, L> source,
     Func <L, Maybe <L1> > func)
 => source.Cata(
Esempio n. 22
0
 public static IChurchBoolean IsLeaf <N, L>(this IRoseTree <N, L> source)
 => source.Match <IChurchBoolean>(
     node: (_, __) => new ChurchFalse(),
     leaf: _ => new ChurchTrue());
Esempio n. 23
0
 public static IChurchBoolean IsNode <N, L>(this IRoseTree <N, L> source)
 => new ChurchNot(source.IsLeaf());
Esempio n. 24
0
 public static Maybe <L> Find <N, L>(this IRoseTree <N, L> source, Func <L, bool> f)
 => source.Cata(
     node: (x, xs) => xs.ToList().Find(x => f(x)),
     leaf: l => l
     );
Esempio n. 25
0
 public static int CountLeaves <N, L>(this IRoseTree <N, L> source)
 => source.Cata(
     node: (x, xs) => xs.ToList().FindAll(x => x == 1).Count(),
     leaf: l => 1
     );
Esempio n. 26
0
 public static int Max(this IRoseTree <int, int> source)
 => source.Cata((x, xs) => xs.Any() ? Math.Max(x, xs.Max()) : x, l => l);
Esempio n. 27
0
 public static int Sum(this IRoseTree <int, int> source)
 => source.Cata((x, xs) => x + xs.Sum(), l => l);
Esempio n. 28
0
 public static IRoseTree <N, L1> Select <N, L, L1>(
     this IRoseTree <N, L> source,
     Func <L, L1> fn)
 => source.MapLeaf(fn);
Esempio n. 29
0
 public static IRoseTree <N, L1> MapLeaf <N, L, L1>(
     this IRoseTree <N, L> source,
     Func <L, L1> func)
 => source.BiMap(n => n, func);
Esempio n. 30
0
 public static IRoseTree <N1, L> MapNode <N, N1, L>(
     this IRoseTree <N, L> source,
     Func <N, N1> func)
 => source.BiMap(func, l => l);