A package providing some common default typeclass methods implementations given minimum complete definition of said typeclass
npm install @hanshi/typeclass@hanshi/typeclass``Typescript
import {
FirstParameter,
Functional,
PartialApplied,
Unary,
cons,
left,
partial,
proxied,
right
} from '@hanshi/prelude';
import {
defineLeftTie,
defineLift,
defineReplace,
defineInsert,
defineTie,
defineTraverse
} from '../lib';
const fmap =
f: F,
aa: FirstParameter
): PartialApplied
const v$ = (a: A, bs: B[]): A[] => bs.map(() => a);
const replace2 = defineReplace(fmap) as (a: A, ab: B[]) => A[];
const pure =
const tie =
aa.flatMap((a) => af.map((f) => partial(f, a)));
const lift =
proxied(
(target, args) =>
args
.reduce(
(prev, as) =>
prev.length === 0
? as.map((a) => [a])
: as.flatMap((a) => prev.map((p) => p.concat(a))),
[]
)
.map((as) => target(...as)),
f
);
const lift2 = defineLift(fmap, tie);
const tie2 = defineTie(lift2);
const rightTie = (as: A[], bs: B[]) =>
bs.flatMap((b) => as.flatMap((a) => right(a, b)));
const insert2 = defineInsert(replace2, tie2);
const leftTie = (as: A[], bs: B[]) =>
bs.flatMap((b) => as.flatMap((a) => left(a, b)));
const leftTie2 = defineLeftTie(lift2);
const sequence = (() => {
const sequence: Unary = (fa) => {
if (fa.length === 0) return pure(fa);
const [x, ...xs] = fa;
return tie(fmap(cons, x), sequence(xs));
};
return sequence;
})();
const traverse = defineTraverse(fmap, sequence);
``