Exemplo n.º 1
0
 private static WBT <Pair <k, v> > .N Alter(Func <k, Ord> p, k key, Func <Opt <v>, Opt <v> > f, WBT <Pair <k, v> > .N t) =>
 WBT <Pair <k, v> > .Tip(t)
         ? (f(Ctors.None())).Case(_ => WBT <Pair <k, v> > .Tip(), (v vv) => WBT <Pair <k, v> > .Singleton(Ctors.Pair(key, vv)))
         : (p(t.v.Left()).Case(
                _ => WBT <Pair <k, v> > .Balance(t.v, Alter(p, key, f, t.l), t.r),
                _ => f(Ctors.Some(t.v.Right())).Case(
                    __ => WBT <Pair <k, v> > .Glue(t.l, t.r),
                    vv => WBT <Pair <k, v> > .Bin(t.s, Ctors.Pair(key, vv), t.l, t.r)),
                _ => WBT <Pair <k, v> > .Balance(t.v, t.l, Alter(p, key, f, t.r))));
Exemplo n.º 2
0
            public static Pair <a, N> DeleteFindMax(N t)
            {
                if (Tip(t))
                {
                    throw new InvalidOperationException("No maximum for an empty tree");
                }
                if (Tip(t.r))
                {
                    return(Ctors.Pair(t.v, t.l));
                }
                var max = DeleteFindMax(t.r);

                return(Ctors.Pair(max.Left(), BalanceL(t.v, t.l, max.Right())));
            }
Exemplo n.º 3
0
            public static Pair <a, N> DeleteFindMin(N t)
            {
                if (Tip(t))
                {
                    throw new InvalidOperationException("No minimum for an empty tree");
                }
                if (Tip(t.l))
                {
                    return(Ctors.Pair(t.v, t.r));
                }
                var min = DeleteFindMin(t.l);

                return(Ctors.Pair(min.Left(), BalanceR(t.v, min.Right(), t.r)));
            }
Exemplo n.º 4
0
 public TreeMap <k, v> Singleton(k key, v value, ROrder <k, Unit> ord = null) => New(ord, WBT <Pair <k, v> > .Singleton(Ctors.Pair(key, value)));
Exemplo n.º 5
0
 private static WBT <Pair <k, v> > .N Map(Func <k, v, v> f, WBT <Pair <k, v> > .N t) =>
 WBT <Pair <k, v> > .Tip(t) ? t : WBT <Pair <k, v> > .Bin(t.s, Ctors.Pair(t.v.Left(), t.v.Case(f)), Map(f, t.l), Map(f, t.r));
Exemplo n.º 6
0
 public static Opt <a> Lookup(Func <a, Ord> p, N t) => Tip(t) ? Ctors.None() : p(t.v).Case(_ => Lookup(p, t.l), _ => Ctors.Some(t.v), _ => Lookup(p, t.r));
Exemplo n.º 7
0
 public static Func <a, Ord> MkComp(ROrder <a, Unit> ord, a value) =>
 v => (ord ?? AOrdComparer.Create(Ctors.Func <a, a, int>(Comparer <a> .Default.Compare).Map(Orders.ToOrd))).Compare(value, v);
Exemplo n.º 8
0
 /// <summary>
 /// Basic closure-evading pattern matcher
 /// </summary>
 public static outˈ Case <falseCtx, trueCtx, outˈ>(this bool boolˈ, falseCtx falseCtxˈ, Func <falseCtx, Unit, outˈ> falseˈ, trueCtx trueCtxˈ, Func <trueCtx, Unit, outˈ> trueˈ) =>
 boolˈ?trueˈ(trueCtxˈ, Ctors.Unit()) : falseˈ(falseCtxˈ, Ctors.Unit());
Exemplo n.º 9
0
 /// <summary>
 /// Basic pattern matcher
 /// </summary>
 public static outˈ Case <outˈ>(this bool boolˈ, Func <Unit, outˈ> falseˈ, Func <Unit, outˈ> trueˈ) => boolˈ.Case(falseˈ, Fst, trueˈ, Fst)(Ctors.Unit());
Exemplo n.º 10
0
 /// <summary>
 /// True projection
 /// </summary>
 public static Unit True(this bool that) => that?Ctors.Unit() : Fail <Unit> .Tag(that, nameof(True));
Exemplo n.º 11
0
 /// <summary>
 /// False projection
 /// </summary>
 public static Unit False(this bool that) => !that?Ctors.Unit() : Fail <Unit> .Tag(that, nameof(False));
Exemplo n.º 12
0
 /// <summary>
 /// Extend a list with an element
 /// </summary>
 /// <example>cons [1,2,3] 4 → [1,2,3,4]</example>
 public static List <val> Cons <val>(this List <val> that, val value) => Ctors.Cons(value, that);