Created
March 12, 2020 17:50
-
-
Save Willmo36/f2f61647513b58469824e090ef80250a to your computer and use it in GitHub Desktop.
Higher Kinded Data fp-ts
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 {Applicative1} from 'fp-ts/lib/Applicative'; | |
| import * as Array from 'fp-ts/lib/Array'; | |
| import {Kind, URIS} from 'fp-ts/lib/HKT'; | |
| import {pipe} from 'fp-ts/lib/pipeable'; | |
| export type HKD<F extends URIS, A> = {[K in keyof A]: Kind<F, A[K]>}; | |
| export const sequenceHKD = <F extends URIS>(F: Applicative1<F>) => <A>( | |
| lifted: HKD<F, A>, | |
| ): Kind<F, A> => { | |
| const fa = pipe( | |
| lifted, | |
| Object.keys, | |
| Array.map(key => { | |
| const fak = lifted[key] as Kind<F, unknown>; | |
| const foo = F.map(fak, value => (obj: Partial<A>) => ({ | |
| ...obj, | |
| [key]: value, | |
| })); | |
| return foo; | |
| }), | |
| Array.reduce(F.of({}), (acc, fab) => F.ap(fab, acc)), | |
| ) as Kind<F, A>; | |
| return fa; | |
| }; | |
| export const liftHKD = <F extends URIS>(F: Applicative1<F>) => <A>( | |
| obj: A, | |
| ): HKD<F, A> => { | |
| const lifted = pipe( | |
| obj, | |
| Object.keys, | |
| Array.map(key => { | |
| const liftedValue = F.of(obj[key] as unknown); | |
| return (acc: Partial<HKD<F, A>>) => ({ | |
| ...acc, | |
| [key]: liftedValue, | |
| }); | |
| }), | |
| Array.reduce({}, (acc, ctor) => ctor(acc)), | |
| ) as HKD<F, A>; | |
| return lifted; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment