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()); }
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))); }
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 );
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); }
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>() );
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); }
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)) );
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) );
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)); }
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)); }
// 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)))); }
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)); }
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); }
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)); }
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); }
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()); }
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 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()); }
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); }
public static Maybe <IRoseTree <N, L1> > Choose <N, L, L1>( this IRoseTree <N, L> source, Func <L, Maybe <L1> > func) => source.Cata(
public static IChurchBoolean IsLeaf <N, L>(this IRoseTree <N, L> source) => source.Match <IChurchBoolean>( node: (_, __) => new ChurchFalse(), leaf: _ => new ChurchTrue());
public static IChurchBoolean IsNode <N, L>(this IRoseTree <N, L> source) => new ChurchNot(source.IsLeaf());
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 );
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 );
public static int Max(this IRoseTree <int, int> source) => source.Cata((x, xs) => xs.Any() ? Math.Max(x, xs.Max()) : x, l => l);
public static int Sum(this IRoseTree <int, int> source) => source.Cata((x, xs) => x + xs.Sum(), l => l);
public static IRoseTree <N, L1> Select <N, L, L1>( this IRoseTree <N, L> source, Func <L, L1> fn) => source.MapLeaf(fn);
public static IRoseTree <N, L1> MapLeaf <N, L, L1>( this IRoseTree <N, L> source, Func <L, L1> func) => source.BiMap(n => n, func);
public static IRoseTree <N1, L> MapNode <N, N1, L>( this IRoseTree <N, L> source, Func <N, N1> func) => source.BiMap(func, l => l);