Skip to content

Commit

Permalink
fix: Remove Deno.test from source files (#151)
Browse files Browse the repository at this point in the history
  • Loading branch information
MikuroXina authored Nov 16, 2023
1 parent a3e9b04 commit 749bbbb
Show file tree
Hide file tree
Showing 19 changed files with 1,815 additions and 1,288 deletions.
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"tasks": {
"dev": "deno run --watch mod.ts",
"test": "deno test -A src/",
"test": "deno test -A src/ && deno test -A --doc src/",
"coverage": "deno test -A --coverage=./coverage/cov_profile src/",
"doc": "deno run --allow-read --allow-write --allow-env --deny-run npm:typedoc@0.25.3 --tsconfig tsconfig.doc.json --skipErrorChecking --name @mikuroxina/mini-fn mod.ts"
},
Expand Down
168 changes: 101 additions & 67 deletions src/cat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import type { Monad } from "./type-class/monad.ts";
import { fromProjection as ordFromProjection } from "./type-class/ord.ts";
import { fromProjection as partialEqFromProjection } from "./type-class/partial-eq.ts";
import { fromProjection as partialOrdFromProjection } from "./type-class/partial-ord.ts";
import { monad as optionMonad, none, some } from "./option.ts";
import { assertEquals } from "../deps.ts";

/**
* Contains a `ctx` and can be transformed into another one by some methods.
Expand Down Expand Up @@ -138,35 +136,41 @@ export const doVoidT = <M>(monad: Monad<M>): CatT<M, void> =>
*
* @param monad - The monad implementation for `M`.
* @returns A new `CatT`.
*
* # Examples
*
* ```ts
* import { monad as optionMonad, none, some } from "./option.ts";
* import { assertEquals } from "../deps.ts";
* import { doT } from "./cat.ts";
*
* const optionA = some(1);
* const optionB = some(2);
* const optionC = some(3);
*
* const computation = doT(optionMonad)
* .addM("a", optionA)
* .addM("b", optionB)
* .addWith("bSquared", ({ b }) => b * b)
* .addM("c", optionC);
*
* assertEquals(
* computation
* .addMWith("cSqrt", ({ c }) => {
* const sqrt = Math.sqrt(c);
* return Number.isInteger(sqrt) ? some(sqrt) : none();
* })
* .finish(({ bSquared, cSqrt }) => bSquared + cSqrt),
* none(),
* );
*
* const result = computation.finish(({ a, b, c }) => a + b + c);
* assertEquals(result, some(6));
* ```
*/
export const doT = <M>(monad: Monad<M>): CatT<M, Record<string, never>> =>
catT(monad)(monad.pure({}));

Deno.test("doT", () => {
const optionA = some(1);
const optionB = some(2);
const optionC = some(3);

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

assertEquals(
computation
.addMWith("cSqrt", ({ c }) => {
const sqrt = Math.sqrt(c);
return Number.isInteger(sqrt) ? some(sqrt) : none();
})
.finish(({ bSquared, cSqrt }) => bSquared + cSqrt),
none(),
);

const result = computation.finish(({ a, b, c }) => a + b + c);
assertEquals(result, some(6));
});

/**
* Creates a new `CatT` with the context.
*
Expand Down Expand Up @@ -206,19 +210,24 @@ export interface Cat<T> {
*
* @param value - A value will be contained.
* @returns A new created `Cat`.
*
* # Examples
*
* ```ts
* import { cat } from "./cat.ts";
* import { assertEquals } from "../deps.ts";
*
* const result = cat(-3)
* .feed((x) => x ** 2)
* .feed((x) => x.toString());
* assertEquals(result.value, "9");
* ```
*/
export const cat = <T>(value: T): Cat<T> => ({
value,
feed: <U>(fn: (t: T) => U) => cat(fn(value)),
});

Deno.test("cat", () => {
const result = cat(-3)
.feed((x) => x ** 2)
.feed((x) => x.toString());
assertEquals(result.value, "9");
});

/**
* Gets the contained value from `Cat`. It is convenient to apply the getter for projection to some functor.
*
Expand Down Expand Up @@ -249,22 +258,27 @@ export const ord = ordFromProjection<CatHkt>(get);
*
* @param inspector - An inspector to see the passing value.
* @returns An identity function.
*
* # Examples
*
* ```ts
* import { cat, inspect } from "./cat.ts";
* import { assertEquals } from "../deps.ts";
*
* const result = cat(-3)
* .feed(inspect((x) => assertEquals(x, -3)))
* .feed((x) => x ** 2)
* .feed(inspect((x) => assertEquals(x, 9)))
* .feed((x) => x.toString())
* .feed(inspect((x) => assertEquals(x, "9")));
* assertEquals(result.value, "9");
* ```
*/
export const inspect = <T>(inspector: (t: T) => void) => (t: T) => {
inspector(t);
return t;
};

Deno.test("inspect", () => {
const result = cat(-3)
.feed(inspect((x) => assertEquals(x, -3)))
.feed((x) => x ** 2)
.feed(inspect((x) => assertEquals(x, 9)))
.feed((x) => x.toString())
.feed(inspect((x) => assertEquals(x, "9")));
assertEquals(result.value, "9");
});

/**
* An inspector which applied `console.log` to `inspect`.
*/
Expand Down Expand Up @@ -312,60 +326,80 @@ export const flatten = <T>(catCat: Cat<Cat<T>>): Cat<T> => catCat.value;
* @param a - A `Cat` to be placed at left.
* @param b - A `Cat` to be placed at right.
* @returns A composed `Cat`.
*
* # Examples
*
* ```ts
* import { cat, product } from "./cat.ts";
* import { assertEquals } from "../deps.ts";
*
* const actual = product(cat(5))(cat("foo")).value;
* assertEquals(actual, [5, "foo"]);
* ```
*/
export const product = <A>(a: Cat<A>) => <B>(b: Cat<B>): Cat<[A, B]> =>
cat([a.value, b.value]);

Deno.test("product", () => {
const actual = product(cat(5))(cat("foo")).value;
assertEquals(actual, [5, "foo"]);
});

/**
* Maps an inner value of a `Cat` into another one by applying a function. It is useful to lift a function for `Cat`.
*
* @param fn - A function which maps from `T` to `U`.
* @returns A lifted function which maps from `Cat<T>` to `Cat<U>`.
*
* # Examples
*
* ```ts
* import { cat, map } from "./cat.ts";
* import { assertEquals } from "../deps.ts";
*
* const actual = map((v: number) => v / 2)(cat(10)).value;
* assertEquals(actual, 5);
* ```
*/
export const map = <T, U>(fn: (t: T) => U) => (c: Cat<T>): Cat<U> => c.feed(fn);

Deno.test("map", () => {
const actual = map((v: number) => v / 2)(cat(10)).value;
assertEquals(actual, 5);
});

/**
* Maps an inner value of `Cat` into another `Cat` by applying a function. It is useful to lift a subroutine with `Cat`.
*
* @param fn - A function which maps from `T` to `Cat<U>`.
* @returns A lifted function which maps from `Cat<T>` to `Cat<U>`.
*
* # Examples
*
* ```ts
* import { Cat, cat, flatMap } from "./cat.ts";
* import { assertEquals } from "../deps.ts";
*
* const sub = (num: number): Cat<string> =>
* cat(num).feed((x) => x.toString());
* const actual = flatMap(sub)(cat(6)).value;
* assertEquals(actual, "6");
* ```
*/
export const flatMap = <T, U>(fn: (t: T) => Cat<U>) => (c: Cat<T>): Cat<U> =>
flatten(map(fn)(c));

Deno.test("flatMap", () => {
const sub = (num: number): Cat<string> =>
cat(num).feed((x) => x.toString());
const actual = flatMap(sub)(cat(6)).value;
assertEquals(actual, "6");
});

/**
* Lifts down a `Cat` which contains a mapping function. It is useful to decompose a function in `Cat`.
*
* @param fn - A `Cat` which contains a mapping function.
* @returns An applied function which maps from `Cat<T>` to `Cat<U>`.
*
* # Examples
*
* ```ts
* import { cat, apply } from "./cat.ts";
* import { assertEquals } from "../deps.ts";
*
* const sub = cat((numeral: string) => parseInt(numeral, 10));
* const actual = apply(sub)(cat("1024")).value;
* assertEquals(actual, 1024);
* ```
*/
export const apply =
<T1, U1>(fn: Cat<(t: T1) => U1>) => (t: Cat<T1>): Cat<U1> =>
flatMap(t.feed)(fn);

Deno.test("apply", () => {
const sub = cat((numeral: string) => parseInt(numeral, 10));
const actual = apply(sub)(cat("1024")).value;
assertEquals(actual, 1024);
});

/**
* The monad implementation of `Cat`.
*/
Expand Down
16 changes: 16 additions & 0 deletions src/cont.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { cat } from "./cat.ts";
import { Cont, flatMap, pure, runContT } from "./cont.ts";
import { assertSpyCall, spy } from "../deps.ts";

Deno.test("simple usage", () => {
const calcLength = <A, R>(a: readonly A[]): Cont<R, number> =>
pure(a.length);
const double = <R>(num: number): Cont<R, number> => pure(num * 2);
const callback = spy(() => {});
cat([1, 2, 3]).feed(calcLength).feed(flatMap(double)).feed(runContT).value(
callback,
);
assertSpyCall(callback, 0, {
args: [6],
});
});
Loading

0 comments on commit 749bbbb

Please sign in to comment.