-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5a5b574
commit 5f927fb
Showing
19 changed files
with
520 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import type { FnHkt } from "./func.js"; | ||
import type { Get2, Hkt2 } from "./hkt.js"; | ||
import type { GenericBifunctor } from "./type-class/bifunctor.js"; | ||
import type { Category } from "./type-class/category.js"; | ||
import type { Monoid } from "./type-class/monoid.js"; | ||
import type { Contravariant } from "./type-class/variance.js"; | ||
|
||
export interface Dual<A, B> { | ||
(b: B): A; | ||
} | ||
|
||
export interface DualHkt extends Hkt2 { | ||
readonly type: Dual<this["arg2"], this["arg1"]>; | ||
} | ||
|
||
export const cat: Category<DualHkt> = { | ||
identity: () => (x) => x, | ||
compose: (funcA) => (funcB) => (c) => funcB(funcA(c)), | ||
}; | ||
|
||
export const contra: Contravariant<DualHkt> = { | ||
contraMap: | ||
<T, U>(f: (t: T) => U) => | ||
<A>(bDual: Dual<A, U>): Dual<A, T> => | ||
(t) => | ||
bDual(f(t)), | ||
}; | ||
|
||
export const monoid = <A, B>(m: Monoid<A>): Monoid<Dual<A, B>> => ({ | ||
identity: () => m.identity, | ||
combine: (f, g) => (b) => m.combine(f(b), g(b)), | ||
}); | ||
|
||
export const bifunctor = <T>( | ||
bf: GenericBifunctor<FnHkt, FnHkt, FnHkt, T>, | ||
): GenericBifunctor<DualHkt, DualHkt, DualHkt, T> => ({ | ||
cat1: cat, | ||
cat2: cat, | ||
cat3: cat, | ||
genericBiMap: | ||
<A, B>(f: Dual<A, B>) => | ||
<C, D>(g: Dual<C, D>): Dual<Get2<T, A, C>, Get2<T, B, D>> => | ||
bf.genericBiMap(f)(g), | ||
}); |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import type { Hkt1 } from "./hkt.js"; | ||
import type { Monoid } from "./type-class/monoid.js"; | ||
import type { Contravariant } from "./type-class/variance.js"; | ||
|
||
export interface Predicate<A> { | ||
(a: A): boolean; | ||
} | ||
|
||
export interface PredicateHkt extends Hkt1 { | ||
readonly type: Predicate<this["arg1"]>; | ||
} | ||
|
||
export const contra: Contravariant<PredicateHkt> = { | ||
contraMap: (mapper) => (predB) => (a) => predB(mapper(a)), | ||
}; | ||
|
||
export const monoid = <A>(): Monoid<Predicate<A>> => ({ | ||
identity: () => true, | ||
combine: (predL, predR) => (a) => predL(a) && predR(a), | ||
}); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import type { Apply3Only, Get1, Hkt1, Hkt3 } from "./hkt.js"; | ||
import type { Functor } from "./type-class/functor.js"; | ||
import { Profunctor, rightMap } from "./type-class/profunctor.js"; | ||
|
||
export interface Star<F, D, C> { | ||
(d: D): Get1<F, C>; | ||
} | ||
|
||
export interface StarHkt extends Hkt3 { | ||
readonly type: Star<this["arg3"], this["arg2"], this["arg1"]>; | ||
} | ||
|
||
export const pro = <F extends Hkt1>(functor: Functor<F>): Profunctor<Apply3Only<StarHkt, F>> => ({ | ||
diMap: (f) => (g) => (m) => (d) => functor.map(g)(m(f(d))), | ||
}); | ||
|
||
export const map = <F extends Hkt1>(functor: Functor<F>) => rightMap(pro(functor)); |
Oops, something went wrong.