Last active
July 21, 2019 14:17
-
-
Save itacirgabral/17fc1606e47a9da218808a72d5444698 to your computer and use it in GitHub Desktop.
capturing side effects in a task
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 P = f => { | |
const LazyRight = h => ({ | |
map: f => LazyRight(() => f(h())), | |
then: (f, g) => g(h()), | |
toString: () => `Resolve(${h})`, | |
}) | |
const LazyLeft = h => ({ | |
map: f => LazyLeft(h), | |
then: (f, g) => f(h()), | |
toString: () => `Reject(${h})`, | |
}) | |
const dust = Symbol() | |
let L = dust | |
let R | |
f(rej => L = rej, res => R = res) | |
return (L === dust) ? LazyRight(() => R) : LazyLeft(() => L) | |
}; | |
P((rej, res) => { | |
if (Math.random() > 0.5) { | |
res(0) | |
} else { | |
rej(0) | |
} | |
}).map(n => console.log(`log ${n}`) || n + 1) | |
.map(n => console.log(`log ${n}`) || n + 1) | |
.map(n => console.log(`log ${n}`) || n + 1) | |
.map(n => console.log(`log ${n}`) || n + 1) | |
.map(n => console.log(`log ${n}`) || n + 1) | |
.then( | |
n => console.log(`Reject - n: ${n}`), | |
n => console.log(`Resolve - n: ${n}`) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment