Skip to content

Instantly share code, notes, and snippets.

@YBogomolov
Last active September 12, 2019 06:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YBogomolov/a19f52c3242367674f673c96938f2500 to your computer and use it in GitHub Desktop.
Save YBogomolov/a19f52c3242367674f673c96938f2500 to your computer and use it in GitHub Desktop.
Profunctor instances for UpStar and DownStar
import { Functor } from 'fp-ts/lib/Functor';
import { HKT, Kind3, URIS3 } from 'fp-ts/lib/HKT';
export type Fn<A, B> = (a: A) => B;
export interface Profunctor<F extends URIS3, G> {
dimap: <A, B>(ab: Fn<A, B>) => <C, D>(cd: Fn<C, D>) => (fbc: Kind3<F, G, B, C>) => Kind3<F, G, A, D>;
}
export type UpStar<F, A, B> = (a: A) => HKT<F, B>;
export const UpStarURI = 'UpStar';
export type UpStarURI = typeof UpStarURI;
export type DownStar<F, A, B> = (fa: HKT<F, A>) => B;
export const DownStarURI = 'DownStar';
export type DownStarURI = typeof DownStarURI;
export const UpStar = <F, A, B>(afb: (a: A) => HKT<F, B>): UpStar<F, A, B> => afb;
declare module 'fp-ts/lib/HKT' {
interface URItoKind3<R, E, A> {
UpStar: UpStar<R, E, A>;
DownStar: DownStar<R, E, A>;
}
}
export const getUpStarProfunctor = <F>(F: Functor<F>): Profunctor<UpStarURI, F> => ({
dimap: (ab) => (cd) => (fbc) => (a) => F.map(fbc(ab(a)), cd),
});
export const getDownStarProfunctor = <F>(F: Functor<F>): Profunctor<DownStarURI, F> => ({
dimap: (ab) => (cd) => (fbc) => (fa) => cd(fbc(F.map(fa, ab))),
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment