Skip to content

Commit

Permalink
fix: Improve CatT method name (#142)
Browse files Browse the repository at this point in the history
  • Loading branch information
MikuroXina authored Nov 14, 2023
1 parent 18029cb commit a42aca9
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 40 deletions.
26 changes: 13 additions & 13 deletions src/cat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export interface CatT<M, CTX> {
* @param value - The wrapped value to bind.
* @returns A new `CatT` containing the value at the key.
*/
readonly let: <const K extends PropertyKey, A>(
readonly addM: <const K extends PropertyKey, A>(
key: K,
value: Get1<M, A>,
) => CatT<M, Record<K, A> & CTX>;
Expand All @@ -45,7 +45,7 @@ export interface CatT<M, CTX> {
* @param fn - The calculation.
* @returns A new `CatT` containing the value at the key.
*/
readonly thenLet: <const K extends PropertyKey, A>(
readonly addWith: <const K extends PropertyKey, A>(
key: K,
fn: (ctx: CTX) => A,
) => CatT<M, Record<K, A> & CTX>;
Expand All @@ -56,7 +56,7 @@ export interface CatT<M, CTX> {
* @param computation - The computation to run.
* @returns A new `CatT` with modified environment.
*/
readonly then: <T>(computation: Get1<M, T>) => CatT<M, T>;
readonly run: <T>(computation: Get1<M, T>) => CatT<M, T>;

/**
* Binds a new value wrapped by the monad, calculated from `ctx` by `fn`.
Expand All @@ -65,7 +65,7 @@ export interface CatT<M, CTX> {
* @param fn - The calculation which returns the wrapped value.
* @returns A new `CatT` containing the value at the key.
*/
readonly flatLet: <const K extends PropertyKey, A>(
readonly addMWith: <const K extends PropertyKey, A>(
key: K,
fn: (ctx: CTX) => Get1<M, A>,
) => CatT<M, Record<K, A> & CTX>;
Expand All @@ -89,15 +89,15 @@ export interface CatT<M, CTX> {
export const catT =
<M>(monad: Monad<M>) => <CTX>(ctx: Get1<M, CTX>): CatT<M, CTX> => ({
ctx,
let: <const K extends PropertyKey, A>(key: K, value: Get1<M, A>) =>
addM: <const K extends PropertyKey, A>(key: K, value: Get1<M, A>) =>
catT(monad)(
monad.flatMap((c: CTX) =>
monad.map((v: A) =>
({ ...c, [key]: v }) as Record<K, A> & CTX
)(value)
)(ctx),
),
thenLet: <const K extends PropertyKey, A>(
addWith: <const K extends PropertyKey, A>(
key: K,
fn: (ctx: CTX) => A,
) => catT(monad)(
Expand All @@ -109,9 +109,9 @@ export const catT =
}) as Record<K, A> & CTX,
)(ctx),
),
then: (computation) =>
run: (computation) =>
catT(monad)(monad.flatMap(() => computation)(ctx)),
flatLet: <const K extends PropertyKey, A>(
addMWith: <const K extends PropertyKey, A>(
key: K,
fn: (ctx: CTX) => Get1<M, A>,
) => catT(monad)(
Expand Down Expand Up @@ -148,14 +148,14 @@ Deno.test("doT", () => {
const optionC = some(3);

const computation = doT(optionMonad)
.let("a", optionA)
.let("b", optionB)
.thenLet("bSquared", ({ b }) => b * b)
.let("c", optionC);
.addM("a", optionA)
.addM("b", optionB)
.addWith("bSquared", ({ b }) => b * b)
.addM("c", optionC);

assertEquals(
computation
.flatLet("cSqrt", ({ c }) => {
.addMWith("cSqrt", ({ c }) => {
const sqrt = Math.sqrt(c);
return Number.isInteger(sqrt) ? some(sqrt) : none();
})
Expand Down
6 changes: 3 additions & 3 deletions src/free.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,14 @@ Deno.test("hello language", async (t) => {
assertEquals(comparator.eq(empty, empty), true);
assertEquals(runProgram(example), "Hello.\nHello.\nBye.\n");

const exampleCode = doVoidT(m).then(hello).then(hello).then(bye).ctx;
const exampleCode = doVoidT(m).run(hello).run(hello).run(bye).ctx;
assertEquals(comparator.eq(example, exampleCode), true);
});

await t.step("program monad", () => {
const subRoutine = doVoidT(m).then(hello).then(yearsOld(25)).ctx;
const subRoutine = doVoidT(m).run(hello).run(yearsOld(25)).ctx;
const program =
doVoidT(m).then(hey).then(subRoutine).then(hey).then(bye).ctx;
doVoidT(m).run(hey).run(subRoutine).run(hey).run(bye).ctx;

assertEquals(
runProgram(program),
Expand Down
10 changes: 5 additions & 5 deletions src/free.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,9 @@ Deno.test("retract", () => {
const lift = liftF<OptionHkt>(optionMonad);
const retracted = retractOption<string>(
catT(m)(lift(some("hoge")))
.then(lift(some("fuga")))
.then(lift<string>(none()))
.then(lift(some("foo"))).ctx,
.run(lift(some("fuga")))
.run(lift<string>(none()))
.run(lift(some("foo"))).ctx,
);
assertEquals(isNone(retracted), true);
});
Expand Down Expand Up @@ -202,8 +202,8 @@ Deno.test("iter", () => {
const lift = liftF<OptionHkt>(optionMonad);
const iterated = iterOption<string>(
catT(m)(lift(some("hoge")))
.then(lift(some("fuga")))
.then(lift(some("foo"))).ctx,
.run(lift(some("fuga")))
.run(lift(some("foo"))).ctx,
);
assertEquals(iterated, "foo");
});
Expand Down
6 changes: 3 additions & 3 deletions src/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1762,9 +1762,9 @@ export const monad: Monad<ListHkt> = {
Deno.test("with CatT", () => {
// Find patterns where `x + y + z == 5` for all natural number `x`, `y`, and `z`.
const patterns = Cat.doT(monad)
.let("x", range(0, 6))
.flatLet("y", ({ x }) => range(0, 6 - x))
.thenLet("z", ({ x, y }) => 5 - (x + y))
.addM("x", range(0, 6))
.addMWith("y", ({ x }) => range(0, 6 - x))
.addWith("z", ({ x, y }) => 5 - (x + y))
.finish(({ x, y, z }) => [x, y, z] as const);

assertEquals(toArray(patterns), [
Expand Down
28 changes: 14 additions & 14 deletions src/type-class/monad.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ export const liftM2 =
(ma: Get1<S, A>) =>
(mb: Get1<S, B>): Get1<S, C> =>
doT(m)
.let("a", ma)
.let("b", mb)
.addM("a", ma)
.addM("b", mb)
.finish(({ a, b }) => f(a)(b));

export const liftM3 =
Expand All @@ -71,9 +71,9 @@ export const liftM3 =
(mb: Get1<S, B>) =>
(mc: Get1<S, C>): Get1<S, D> =>
doT(m)
.let("a", ma)
.let("b", mb)
.let("c", mc)
.addM("a", ma)
.addM("b", mb)
.addM("c", mc)
.finish(({ a, b, c }) => f(a)(b)(c));

export const liftM4 =
Expand All @@ -84,10 +84,10 @@ export const liftM4 =
(mc: Get1<S, C>) =>
(md: Get1<S, D>): Get1<S, E> =>
doT(m)
.let("a", ma)
.let("b", mb)
.let("c", mc)
.let("d", md)
.addM("a", ma)
.addM("b", mb)
.addM("c", mc)
.addM("d", md)
.finish(({ a, b, c, d }) => f(a)(b)(c)(d));

export const liftM5 = <S>(m: Monad<S>) =>
Expand All @@ -100,11 +100,11 @@ export const liftM5 = <S>(m: Monad<S>) =>
(md: Get1<S, D>) =>
(me: Get1<S, E>): Get1<S, F> =>
doT(m)
.let("a", ma)
.let("b", mb)
.let("c", mc)
.let("d", md)
.let("e", me)
.addM("a", ma)
.addM("b", mb)
.addM("c", mc)
.addM("d", md)
.addM("e", me)
.finish(({ a, b, c, d, e }) => f(a)(b)(c)(d)(e));

export const begin = <S>(m: Monad<S>): Get1<S, object> => m.pure({});
Expand Down
4 changes: 2 additions & 2 deletions src/writer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ Deno.test("censor with log decoration", () => {
const m = makeMonad(monoidArray<string>());

const hello = doVoidT(m)
.then(tell(["Hello!"]))
.then(tell(["What do you do?"])).ctx;
.run(tell(["Hello!"]))
.run(tell(["What do you do?"])).ctx;
const log = censor((messages: string[]) =>
messages.map((message) => `[LOG] ${message}`)
)(
Expand Down

0 comments on commit a42aca9

Please sign in to comment.