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
const promise: Promise<A>; | |
const a = await promise; | |
// a of type A | |
const monad: Monad<B>; | |
const b = yield monad; | |
// b is of type B, but not automatically inferred |
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
fetchA().then(fetchB).then(fetchC).then(fetchD); |
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
interface Promise<A> { | |
then<B>(callback: (a: A) => B | Promise<B>): Promise<B> | |
} |
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
type Monad<A> = Array<A>; | |
function of<A>(a: A): Monad<A> { | |
return [a]; | |
} | |
function flatMap<A, B>(monad: Monad<A>, cb: (a: A) => Monad<B>): Monad<B> { | |
return monad.flatMap(cb); | |
} |
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
multi function rightTrianglesG(maxLengthC: number) { | |
const c = pick range(1, maxLengthC + 1); | |
const a = pick range(1, c); | |
const b = pick range(1, a); | |
pick where(a**2 + b**2 === c**2); | |
return [a, b, c] as const; | |
} | |
multi function where(cond: boolean) { | |
if (cond) return pick []; |
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
fetchA() | |
.then((a) => fetchB(a) | |
.then((b) => fetchC(b) | |
.then((c) => fetchD(c)))) | |
// this can be safely refacoted as | |
fetchA().then(fetchB).then(fetchC).then(fetchD) |
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
// Given | |
declare const monad: Monad<A> | |
declare function f(a: A): Monad<B>; | |
declare function g(b: B): Monad<C>; | |
// Then | |
flatMap(flatMap(monad, f), g) === flatMap(monad, (a) => flatMap(f(a), 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
function of<A>(value: A): Monad<A>; | |
function flatMap<A, B>(monad: Monad<A>, callback: (a: A) => Monad<B>): Monad<B>; |
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
// Given | |
declare const monad: Monad<A>; | |
// Then | |
flatMap(monad, a => of(a)) === monad | |
// === means both parts are equivalent | |
// what equality means can be defined by the monad |
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
// Given | |
declare const a: A; | |
declare function f(a: A): Monad<B>; | |
// Then | |
flatMap(of(a), a => f(a)) === f(a); | |
// === means here that both parts are equivalent | |
// what equality means can be defined by the monad |