Last active
July 14, 2018 20:12
-
-
Save Benvie/c2ef1253d2a9930950d30e6d70096d5a to your computer and use it in GitHub Desktop.
Omega Monad from Haskell translated to JavaScript.
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
/** | |
* A depth-first traversal of nested thunks. | |
* | |
* @param thunks The list of thunks to traverse. | |
**/ | |
async function omega(thunks) { | |
if (typeof thunks.iterate !== "function") { | |
thunks = [thunks]; | |
} | |
for await (let thunk of thunks) { | |
let result = thunk(); | |
if (result) { | |
if (typeof result === "function") { | |
omega([result]); | |
} else if (typeof result.iterate === "function") { | |
omega(result); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment