Skip to content
This repository has been archived by the owner on May 3, 2021. It is now read-only.

Commit

Permalink
feat: simplify typeclass composition functions
Browse files Browse the repository at this point in the history
  • Loading branch information
baetheus committed Sep 15, 2020
1 parent 79e762e commit 50afb19
Showing 1 changed file with 18 additions and 39 deletions.
57 changes: 18 additions & 39 deletions composition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,27 +90,20 @@ export type MonadComposition2<F, G> = ApplicativeComposition2<F, G> &
/**
* Functor
*/
export const getFunctorComposition = <F, G>(
F: TC.Functor<F>,
G: TC.Functor<G>
): FunctorComposition<F, G> => ({
export const getFunctorComposition: {
<F, G>(F: TC.Functor<F>, G: TC.Functor<G>): FunctorComposition<F, G>;
<F, G>(F: TC.Functor2<F>, G: TC.Functor<G>): FunctorComposition2<F, G>;
} = <F, G>(F: TC.Functor<F>, G: TC.Functor<G>): FunctorComposition<F, G> => ({
map: (fab, FGa) => F.map((Ga) => G.map(fab, Ga), FGa),
});

export const getFunctor2Composition = <F, G>(
F: TC.Functor2<F>,
G: TC.Functor<G>
): FunctorComposition2<F, G> => ({
map: (fab, fga) => F.map((ga) => G.map(fab, ga), fga),
});

/**
* Apply
*/
export const getApplyComposition = <F, G>(
F: TC.Apply<F>,
G: TC.Apply<G>
): ApplyComposition<F, G> => ({
export const getApplyComposition: {
<F, G>(F: TC.Apply<F>, G: TC.Apply<G>): ApplyComposition<F, G>;
<F, G>(F: TC.Apply2<F>, G: TC.Apply<G>): ApplyComposition2<F, G>;
} = <F, G>(F: TC.Apply<F>, G: TC.Apply<G>): ApplyComposition<F, G> => ({
...getFunctorComposition(F, G),
ap: <A, B>(FGfab: $<F, [$<G, [(a: A) => B]>]>, FGfa: $<F, [$<G, [A]>]>) =>
F.ap(
Expand All @@ -119,36 +112,22 @@ export const getApplyComposition = <F, G>(
),
});

export const getApply2Composition = <F, G>(
F: TC.Apply2<F>,
G: TC.Apply<G>
): ApplyComposition2<F, G> => ({
...getFunctor2Composition(F, G),
ap: <E, A, B>(
FGfab: $<F, [E, $<G, [(a: A) => B]>]>,
FGfa: $<F, [E, $<G, [A]>]>
) =>
F.ap(
F.map((h) => (ga: $<G, [A]>) => G.ap(h, ga), FGfab),
FGfa
),
});

/**
* Applicative
*/
export const getApplicativeComposition = <F, G>(
export const getApplicativeComposition: {
<F, G>(F: TC.Applicative<F>, G: TC.Applicative<G>): ApplicativeComposition<
F,
G
>;
<F, G>(F: TC.Applicative2<F>, G: TC.Applicative<G>): ApplicativeComposition2<
F,
G
>;
} = <F, G>(
F: TC.Applicative<F>,
G: TC.Applicative<G>
): ApplicativeComposition<F, G> => ({
...getApplyComposition(F, G),
of: (a) => F.of(G.of(a)),
});

export const getApplicative2Composition = <F, G>(
F: TC.Applicative2<F>,
G: TC.Applicative<G>
): ApplicativeComposition2<F, G> => ({
...getApply2Composition(F, G),
of: (a) => F.of(G.of(a)),
});

0 comments on commit 50afb19

Please sign in to comment.