• Calls computation with the current continuation. This provides an escape continuation mechanism for use with continuation monads. Escape continuation exit can abort the current computation and return a value immediately, like non local exits or exception. The advantage of using this over calling pure is that makes the continuation explicit, allowing more flexibility and better control.

    The standard idiom used with callCC is to provide a lambda expression to name the continuation as a label. Then calling the label anywhere within its scope will escape from the computation, even if it is many layers deep within nested computations.

    Type Parameters

    • R

    • M

    • A

    • B

    Parameters

    • computation: ((exit) => ContT<R, M, A>)

      The computation will be provided exit.

        • (exit): ContT<R, M, A>
        • Parameters

          • exit: ((a) => ContT<R, M, B>)
              • (a): ContT<R, M, B>
              • Parameters

                • a: A

                Returns ContT<R, M, B>

          Returns ContT<R, M, A>

    Returns ContT<R, M, A>

    The label trigger to abort.

    Examples

    import { Cont, callCC, flatMap, pure, runCont, when } from "./cont.ts";
    import { IdentityHkt, id } from "./identity.ts";
    import { cat } from "./cat.ts";
    import { assertEquals } from "../deps.ts";

    const validateName =
    (name: string) =>
    (exit: (a: string) => Cont<string, never[]>): Cont<string, never[]> =>
    when(name.length === 0)(exit("expected at least 1 character"));
    const whatYourName = (name: string): string => {
    const cont = callCC<string, IdentityHkt, string, never[]>(
    (exit) =>
    cat(validateName(name)(exit)).feed(
    flatMap(() => pure(`Welcome, ${name}!`)),
    ).value,
    );
    return runCont(cont)(id);
    };
    assertEquals(whatYourName("Alice"), "Welcome, Alice!");
    assertEquals(whatYourName(""), "expected at least 1 character");

Generated using TypeDoc