Created
April 21, 2021 00:36
-
-
Save baetheus/20b0c3e8ca1ad0c40573278d3851315f to your computer and use it in GitHub Desktop.
Functor Composition with nullpub/hkts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import type { Kind, URIS } from "./hkt.ts"; | |
import type * as TC from "./type_classes.ts"; | |
import { flow, pipe } from "./fns.ts"; | |
export type FunctorComposition<URI extends URIS, VRI extends URIS> = { | |
readonly map: <A, I>( | |
fai: (a: A) => I, | |
) => <B, C, D, E, F, G>( | |
ta: Kind<URI, [Kind<VRI, [A, B, C, D]>, E, F, G]>, | |
) => Kind<URI, [Kind<VRI, [I, B, C, D]>, E, F, G]>; | |
}; | |
export const getFunctorComposition = <URI extends URIS, VRI extends URIS>( | |
U: TC.Functor<URI>, | |
V: TC.Functor<VRI>, | |
): FunctorComposition<URI, VRI> => ({ | |
map: flow(V.map, U.map), | |
}); | |
import * as E from "./either.ts"; | |
import * as O from "./option.ts"; | |
const t1 = getFunctorComposition(E.Functor, O.Functor); | |
const t2 = getFunctorComposition(O.Functor, E.Functor); | |
const f1 = (n: number) => n + 1; | |
const m1 = t1.map(f1); | |
const m2 = t2.map(f1); | |
const r1 = m1(E.right(O.some(1))); | |
const r2 = m1(E.left("asdf")); | |
const r3 = m2(O.some(E.right(1))); | |
const r4 = m2(O.none); | |
const r5 = m2(O.some(E.left("asdf"))); | |
console.log({ r1, r2, r3, r4, r5 }); | |
// { | |
// r1: { tag: "Right", right: { tag: "Some", value: 2 } }, | |
// r2: { tag: "Left", left: "asdf" }, | |
// r3: { tag: "Some", value: { tag: "Right", right: 2 } }, | |
// r4: { tag: "None" }, | |
// r5: { tag: "Some", value: { tag: "Left", left: "asdf" } } | |
// } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment