The label trigger to abort.
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
Calls
computationwith the current continuation. This provides an escape continuation mechanism for use with continuation monads. Escape continuationexitcan abort the current computation and return a value immediately, like non local exits or exception. The advantage of using this over callingpureis that makes the continuation explicit, allowing more flexibility and better control.The standard idiom used with
callCCis 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.