Last active
June 28, 2018 21:59
-
-
Save Willmo36/7b76e05e6149134e855d0362c7f2283e to your computer and use it in GitHub Desktop.
fp-ts & most applicative things
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 { Apply1 } from "fp-ts/lib/Apply"; | |
import { Type, URIS } from "fp-ts/lib/HKT"; | |
import { Task, task } from "fp-ts/lib/Task"; | |
import { Stream, zip } from "most"; | |
import { append } from "ramda"; | |
import { Applicative1 } from "fp-ts/lib/Applicative"; | |
export const sequenceTaskArray = <T>(ts: Task<T>[]): Task<T[]> => | |
ts.reduce((acc, t) => { | |
const y = t.map(x => (ts: T[]) => append(x, ts)); | |
return acc.ap(y); | |
}, task.of<T[]>([])); | |
export const sequenceApplicativeArray = <F extends URIS>(A: Applicative1<F>) => <A>( | |
ts: Type<F, A>[] | |
): Type<F, A[]> => { | |
const empty = A.of<A[]>([]); | |
return ts.reduce((fas, fa) => { | |
const fab = A.map(fa, a => (as: A[]) => append(a, as)); | |
return A.ap(fab, fas); | |
}, empty); | |
}; | |
export const zipTaskStreams = <T1, T2, U>( | |
zipper: (a: T1) => (b: T2) => U, | |
a$: Stream<Task<T1>>, | |
b$: Stream<Task<T2>> | |
) => zip((a, b) => b.ap(a.map(zipper)), a$, b$); | |
//heavily following https://github.com/gcanti/fp-ts/blob/master/HKT.md | |
export const zipApplyStreams = <F extends URIS>(A: Apply1<F>) => <T1, T2, U>( | |
zipper: (a: T1) => (b: T2) => U, | |
a$: Stream<Type<F, T1>>, | |
b$: Stream<Type<F, T2>> | |
) => zip((fa, fb) => A.ap(A.map(fa, zipper), fb), a$, b$); | |
export const zipTaskStreams2 = zipApplyStreams(task); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment