Skip to content

Commit

Permalink
fix!: Remove oneShot and Enforce test for func (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
MikuroXina authored Dec 25, 2022
1 parent 57236bd commit e9e4e19
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
36 changes: 36 additions & 0 deletions src/func.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { absurd, constant, flip, id, until } from "./func.js";
import { expect, test } from "vitest";

test("id", () => {
expect(id(2)).toBe(2);
expect(id("foo")).toBe("foo");
});

test("constant", () => {
const fn = constant(4);
expect(fn(3)).toBe(4);
expect(fn("foo")).toBe(4);
});

test("absurd", () => {
expect(() => {
absurd<number>();
throw new Error("this line must not be run");
}).toThrowError("PANIC: absurd must not be called");
});

test("flip", () => {
const fn = flip((a: string) => (b: string) => a + b);
expect(fn("a")("b")).toEqual("ba");
expect(fn("asd")("btg")).toEqual("btgasd");
});

test("until", () => {
const padLeft = until((x: string) => 4 <= x.length)((x) => "0" + x);
expect(padLeft("")).toBe("0000");
expect(padLeft("1")).toBe("0001");
expect(padLeft("13")).toBe("0013");
expect(padLeft("131")).toBe("0131");
expect(padLeft("1316")).toBe("1316");
expect(padLeft("1316534")).toBe("1316534");
});
5 changes: 0 additions & 5 deletions src/func.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@ export const absurd = <T>(): T => {
throw new Error("PANIC: absurd must not be called");
};

export const oneShot =
<T, U>(f: (a: T) => U) =>
(a: T): U =>
f(a);

export const compose =
<U, V>(f: (u: U) => V) =>
<T>(g: (t: T) => U) =>
Expand Down
6 changes: 2 additions & 4 deletions src/type-class/foldable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { List, build } from "../list.js";
import { Monoid, append } from "./monoid.js";
import { Option, isNone, none, some, unwrapOrElse } from "../option.js";
import { andMonoid, orMonoid } from "../bool.js";
import { compose, oneShot } from "../func.js";
import { compose, id } from "../func.js";

import type { PartialEq } from "./partial-eq.js";

Expand Down Expand Up @@ -34,9 +34,7 @@ export const foldL =
<A, B>(f: (b: B) => (a: A) => B) =>
(init: B) =>
(data: GetHktA1<T, A>): B =>
foldable.foldR((x: A) => (k: (b: B) => B) => oneShot((z: B) => k(f(z)(x))))((x: B): B => x)(
data,
)(init);
foldable.foldR((x: A) => (k: (b: B) => B) => (z: B) => k(f(z)(x)))(id)(data)(init);

export const foldR1 =
<T>(foldable: Foldable1<T>) =>
Expand Down

0 comments on commit e9e4e19

Please sign in to comment.