//#region Traverse //public static IEnumerable<Cont<R, B>> Traverse<R, A, B>(this Cont<R, A> @this, Func<A, IEnumerable<B>> f) => @this.Fold(a => f(a).Map(Of<R, B>())); //public static Maybe<Cont<R, B>> Traverse<R, A, B>(this Cont<R, A> @this, Func<A, Maybe<B>> f) => @this.Fold<R, B>(a => f(a).Map(Of<R, B>())); //#endregion #region Callcc /// The callcc function; call with current continuation; is there to allow “escaping” from the current continuation. public static Cont <R, A> CallCC <R, A, B>(Func <Func <A, Cont <R, B> >, Cont <R, A> > f) => Cont <R, A> .Create(k => f(a => Cont <R, B> .Create(x => k(a))).Run(k));
public static Cont <R, B> Select <R, A, B>(this Cont <R, A> @this, Func <A, B> f) => Cont <R, B> .Create(k => @this.Run(a => k(f(a))));
public static Cont <R, B> FlatMap <R, A, B>(this Cont <R, A> @this, Func <A, Cont <R, B> > f) => Cont <R, B> .Create(k => @this.Run(a => f(a).Run(k)));
public static Cont <R, C> ToCont <R, A, B, C>(this Cont <R, A> @this, Cont <R, B> @that, Func <A, B, C> f) => Cont <R, C> .Create(k => @this.Run(a => that.Run(b => k(f(a, b)))));
public static Cont <R, D> ToCont <R, A, B, C, D>(this Cont <R, A> @this, Cont <R, B> @thatb, Cont <R, C> @thatc, Func <A, B, C, D> f) => Cont <R, D> .Create(k => @this.Run(a => @thatb.Run(b => @thatc.Run(c => k(f(a, b, c))))));
public static Cont <R, A> ToCont <R, A>(this A a) => Cont <R, A> .Create(k => k(a));
public static Cont <R, B> ToCont <R, A, B>(this Cont <R, A> @this, Func <A, B> f) => Cont <R, B> .Create(k => @this.Run(f.Compose(k)));
/// <summary> /// Functor `map` operation /// </summary> public static Cont <r, b> Map <r, a, b>(this Cont <r, a> that, Func <a, b> f) => Cont <r, b> .Create(k => that.Case(aˈ => k(f(aˈ))));
/// <summary> /// Call with current continuation /// </summary> public static Cont <r, a> CallCC <r, a, b>(Func <Func <a, Cont <r, b> >, Cont <r, a> > f) => Cont <r, a> .Create(k => f(aˈ => Cont <r, b> .Create(_ => k(aˈ))).Case(k));
/// <summary> /// Applicative functor lift /// </summary> public static Cont <r, c> Lift <r, a, b, c>(this Func <a, b, c> f, Cont <r, a> aˈ, Cont <r, b> bˈ) => Cont <r, c> .Create(k => aˈ.Case(aa => bˈ.Case(bb => k(f(aa, bb)))));
/// <summary> /// Applies a function inside an applicative context (see Applicative functors) /// </summary> public static Cont <r, b> Ap <r, a, b>(this Cont <r, Func <a, b> > f, Cont <r, a> aˈ) => Cont <r, b> .Create(k => aˈ.Case(aa => f.Case(ff => k(ff(aa)))));
/// <summary> /// Monad `join` operation /// </summary> public static Cont <r, a> Flatten <r, a>(this Cont <r, Cont <r, a> > that) => Cont <r, a> .Create(k => that.Case(aˈ => aˈ.Case(k)));