Last active
June 9, 2018 11:15
-
-
Save awto/0352bafa822f0d3594db61fdd4957f3d to your computer and use it in GitHub Desktop.
Converting Coroutine to State monads
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
function* incr() { | |
return (yield set((yield get) + 1)) | |
} | |
function* incrX2() { | |
return (yield* incr()) + (yield* incr()) | |
} | |
const main = state(incrX2) | |
// framework | |
function state(iter) { | |
const i = iter()[Symbol.iterator]() | |
return walk() | |
function walk(arg) { | |
const step = i.next(arg) | |
return step.done ? | |
state => [state, step.value] : | |
state => { | |
const [next, value] = step.value(state) | |
return walk(value)(next) | |
} | |
} | |
} | |
function set(s) { return _ => [s, s] } | |
function get(s) { return [s, s] } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment