Пример #1
0
        /// <summary>
        /// Folds values coming down-stream, when the predicate returns true the folded value is yielded
        /// </summary>
        /// <param name="Initial">Initial state</param>
        /// <param name="Fold">Fold operation</param>
        /// <param name="UntilValue">Predicate</param>
        /// <returns>A pipe that folds</returns>
        public static Producer <RT, S, Unit> FoldWhile <RT, S, A>(this Producer <RT, S, A> ma, S Initial, Func <S, A, S> Fold, Func <S, bool> WhileState)
            where RT : struct, HasCancel <RT>
        {
            var state = Initial;

            return(ma.Bind(
                       x =>
            {
                state = Fold(state, x);
                if (WhileState(state))
                {
                    return Producer.Pure <RT, S, Unit>(unit);
                }
                else
                {
                    var nstate = state;
                    state = Initial;
                    return Producer.yield <RT, S>(nstate);
                }
            }));
        }
Пример #2
0
        /// <summary>
        /// Folds values coming down-stream, when the predicate returns true the folded value is yielded
        /// </summary>
        /// <param name="Initial">Initial state</param>
        /// <param name="Fold">Fold operation</param>
        /// <param name="UntilValue">Predicate</param>
        /// <returns>A pipe that folds</returns>
        public static Producer <RT, S, Unit> FoldUntil <RT, S, A>(this Producer <RT, S, A> ma, S Initial, Func <S, A, S> Fold, Func <A, bool> UntilValue)
            where RT : struct, HasCancel <RT>
        {
            var state = Initial;

            return(ma.Bind(
                       x =>
            {
                if (UntilValue(x))
                {
                    var nstate = state;
                    state = Initial;
                    return Producer.yield <RT, S>(nstate);
                }
                else
                {
                    state = Fold(state, x);
                    return Producer.Pure <RT, S, Unit>(unit);
                }
            }));
        }
Пример #3
0
 public override Producer <RT, OUT, A> Interpret() =>
 Producer.Pure <RT, OUT, A>(Value);
Пример #4
0
 public override Producer <RT, OUT, A> ToProducer <OUT>() =>
 Producer.Pure <RT, OUT, A>(Value);