Exemplo n.º 1
0
        private static State <IReadOnlyList <TA>, BinTree> Build2 <TA>(int n)
        {
            if (n == 1)
            {
                return(State <IReadOnlyList <TA> > .Get().Bind(xs =>
                {
                    var head = xs.First();
                    var tail = (IReadOnlyList <TA>)xs.Skip(1).ToList();
                    return State <IReadOnlyList <TA> > .Put(tail).Bind(_ =>
                    {
                        BinTree leaf = new Leaf <TA>(head);
                        return State <IReadOnlyList <TA> > .Return(leaf);
                    });
                }));
            }

            var m = n / 2;

            return(Build2 <TA>(m).Bind(
                       u => Build2 <TA>(n - m).Bind(
                           v =>
            {
                BinTree fork = new Fork(u, v);
                return State <IReadOnlyList <TA> > .Return(fork);
            })));
        }
Exemplo n.º 2
0
        private static State <Tuple <IReadOnlyList <TA>, int>, BinTree> Build2 <TA>(int n)
        {
            if (n == 1)
            {
                return(State <Tuple <IReadOnlyList <TA>, int> > .Get().Bind(
                           s1 =>
                {
                    var xs = s1.Item1;
                    var index = s1.Item2;
                    var s2 = Tuple.Create(xs, index + 1);
                    var head = xs[index];
                    BinTree leaf = new Leaf <TA>(head);
                    return State <Tuple <IReadOnlyList <TA>, int> > .Put(s2).BindIgnoringLeft(
                        State <Tuple <IReadOnlyList <TA>, int> > .Return(leaf));
                }));
            }

            var m = n / 2;

            return(Build2 <TA>(m).Bind(
                       u => Build2 <TA>(n - m).Bind(
                           v =>
            {
                BinTree fork = new Fork(u, v);
                return State <Tuple <IReadOnlyList <TA>, int> > .Return(fork);
            })));
        }
Exemplo n.º 3
0
        private static Tuple <BinTree, IReadOnlyList <TA> > Build2 <TA>(int n, IReadOnlyList <TA> xs)
        {
            if (n == 1)
            {
                var     head = xs.First();
                var     tail = (IReadOnlyList <TA>)xs.Skip(1).ToList();
                BinTree leaf = new Leaf <TA>(head);
                return(Tuple.Create(leaf, tail));
            }

            var     m    = n / 2;
            var     t1   = Build2(m, xs);
            var     u    = t1.Item1;
            var     xs1  = t1.Item2;
            var     t2   = Build2(n - m, xs1);
            var     v    = t2.Item1;
            var     xs2  = t2.Item2;
            BinTree fork = new Fork(u, v);

            return(Tuple.Create(fork, xs2));
        }
Exemplo n.º 4
0
        private static Tuple <BinTree, Tuple <IReadOnlyList <TA>, int> > Build2 <TA>(int n, Tuple <IReadOnlyList <TA>, int> state)
        {
            if (n == 1)
            {
                var     xs    = state.Item1;
                var     index = state.Item2;
                var     head  = xs[index];
                BinTree leaf  = new Leaf <TA>(head);
                return(Tuple.Create(leaf, Tuple.Create(xs, index + 1)));
            }

            var     m      = n / 2;
            var     t1     = Build2(m, state);
            var     u      = t1.Item1;
            var     state1 = t1.Item2;
            var     t2     = Build2(n - m, state1);
            var     v      = t2.Item1;
            var     state2 = t2.Item2;
            BinTree fork   = new Fork(u, v);

            return(Tuple.Create(fork, state2));
        }