public IEnumerator <object[]> GetEnumerator() { yield return(new object[] { PhotoLeaf("1", 2018, 11, 9, 11, 47, 17), "D", Node("D", new [] { RoseTree.Node("2018-11", new [] { new RoseLeaf <string, FileInfo>(new FileInfo("1")) }) }) }); yield return(new object[] { RoseTree.Node("S", new [] { PhotoLeaf("4", 1972, 6, 6, 16, 15, 0) }), "D", RoseTree.Node("D", new [] { RoseTree.Node("1972-06", new [] { new RoseLeaf <string, FileInfo>(new FileInfo("4")) }) }) }); yield return(new object[] { RoseTree.Node("S", new [] { PhotoLeaf("L", 2002, 10, 12, 17, 16, 15), PhotoLeaf("J", 2007, 4, 21, 17, 18, 19), }), "D", RoseTree.Node("D", new [] { RoseTree.Node("2002-10", new [] { new RoseLeaf <string, FileInfo>(new FileInfo("L")) }), RoseTree.Node("2007-04", new [] { new RoseLeaf <string, FileInfo>(new FileInfo("J")) }), }) }); yield return(new object[] { RoseTree.Node("1", new [] { PhotoLeaf("a", 2010, 1, 12, 17, 16, 15), PhotoLeaf("b", 2010, 3, 12, 17, 16, 15), PhotoLeaf("c", 2010, 1, 21, 17, 18, 19), }), "2", RoseTree.Node("2", new [] { RoseTree.Node("2010-01", new [] { new RoseLeaf <string, FileInfo>(new FileInfo("a")), new RoseLeaf <string, FileInfo>(new FileInfo("c")) }), RoseTree.Node("2010-03", new [] { new RoseLeaf <string, FileInfo>(new FileInfo("b")) }), }) }); yield return(new object[] { RoseTree.Node("foo", RoseTree.Node("bar", new [] { PhotoLeaf("a", 2010, 1, 12, 17, 16, 15), PhotoLeaf("b", 2010, 3, 12, 17, 16, 15), PhotoLeaf("c", 2010, 1, 21, 17, 18, 19), }), RoseTree.Node("baz", PhotoLeaf("d", 2010, 3, 1, 2, 3, 4), PhotoLeaf("e", 2011, 3, 4, 3, 2, 1) ) ), "qux", RoseTree.Node("qux", new [] { RoseTree.Node("2010-01", new [] { new RoseLeaf <string, FileInfo>(new FileInfo("a")), new RoseLeaf <string, FileInfo>(new FileInfo("c")) }), RoseTree.Node("2010-03", new [] { new RoseLeaf <string, FileInfo>(new FileInfo("b")), new RoseLeaf <string, FileInfo>(new FileInfo("d")) }), RoseTree.Node("2011-03", new [] { new RoseLeaf <string, FileInfo>(new FileInfo("e")) }), }) }); }
// Impure public static IRoseTree <string, string> readTree(string path) { if (File.Exists(path)) { return(new RoseLeaf <string, string>(path)); } else { var dirsAndFiles = Directory.EnumerateFileSystemEntries(path); var branches = dirsAndFiles.Select(readTree).ToArray(); return(RoseTree.Node(path, branches)); } }
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)); }
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())); }
public RoseTreeMenuTest() { commandStore = new Dictionary <string, Command>() { { "Find", new FindCommand("Find") } }; 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")); }