Created
October 3, 2016 13:22
-
-
Save staltz/6ee58f9a14e54a06605f06dc066d2489 to your computer and use it in GitHub Desktop.
Playful Haskell Stream I/O with ES6 Generators
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 *main() { | |
const confirmRequest = { | |
type: 'confirm', | |
value: 'Are you sure?', | |
}; | |
const confirmResponse = yield confirmRequest; | |
if (confirmResponse === true) { | |
const consoleRequest = { | |
type: 'console', | |
value: 'You are sure', | |
}; | |
yield consoleRequest; | |
} else { | |
const consoleRequest = { | |
type: 'console', | |
value: 'You are not sure', | |
}; | |
yield consoleRequest; | |
} | |
} | |
function run(mainFn) { | |
requests = mainFn(); | |
let next = requests.next(); | |
while (!next.done) { | |
const request = next.value; | |
let response = null; | |
if (request.type === 'console') { | |
console.log(request.value); | |
} else if (request.type === 'confirm') { | |
response = window.confirm(request.value); | |
} | |
next = requests.next(response); | |
} | |
} | |
run(main); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The program above is equivalent to