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)) );
// 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 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 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);