Skip to content

Commit

Permalink
feat: Add some partial-applied and Enforce test (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
MikuroXina authored Dec 25, 2022
1 parent fbfe2d9 commit c76c35e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
31 changes: 30 additions & 1 deletion src/cont.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,39 @@
import { Cont, callCC, flatMap, pure, runCont, runContT, when } from "../src/cont.js";
import {
Cont,
callCC,
evalCont,
flatMap,
mapCont,
pure,
runCont,
runContT,
when,
withCont,
} from "../src/cont.js";
import { expect, test, vitest } from "vitest";

import type { IdentityHktKey } from "../src/identity.js";
import { cat } from "../src/cat.js";
import { id } from "../src/func.js";

test("eval", () => {
const actual = evalCont<number>(pure(42));
expect(actual).toBe(42);
});

test("map", () => {
const actual = evalCont(mapCont((x: number) => x + 1)(pure(42)));
expect(actual).toBe(43);
});

test("with", () => {
const cont = withCont((fn: (x: number) => boolean) => (a: string) => fn(parseInt(a, 10)))(
(callback) => callback("foo"),
);
const actual = runCont(cont)(Number.isNaN);
expect(actual).toBe(true);
});

test("simple usage", () => {
const calcLength = <A, R>(a: A[]): Cont<R, number> => pure(a.length);
const double = <R>(num: number): Cont<R, number> => pure(num * 2);
Expand Down
4 changes: 4 additions & 0 deletions src/cont.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ export const withCont: <A, B, R>(
callback: (fn: (b: B) => R) => (a: A) => R,
) => (cont: Cont<R, A>) => Cont<R, B> = withContT;

export const reset: <R, S>(contT: Cont<R, R>) => Cont<S, R> = resetT(Identity.monad);
export const shift: <R, A>(continuation: (callback: (a: A) => R) => Cont<R, R>) => Cont<R, A> =
shiftT(Identity.monad);

export const pure =
<R, M, A>(a: A): ContT<R, M, A> =>
(fn) =>
Expand Down

0 comments on commit c76c35e

Please sign in to comment.