Skip to content

Instantly share code, notes, and snippets.

@staltz staltz/streamio.js

Created Oct 3, 2016
Embed
What would you like to do?
Playful Haskell Stream I/O with ES6 Generators
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);
@staltz

This comment has been minimized.

Copy link
Owner Author

staltz commented Oct 3, 2016

The program above is equivalent to

var response = window.confirm('Are you sure');
if (response === true) {
  console.log('You are sure');
} else {
  console.log('You are not sure');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.