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 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 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 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 IChurchBoolean IsLeaf <N, L>(this IRoseTree <N, L> source) => source.Match <IChurchBoolean>( node: (_, __) => new ChurchFalse(), leaf: _ => new ChurchTrue());