Skip to content

Instantly share code, notes, and snippets.

@Benvie
Last active July 14, 2018 20:12
Show Gist options
  • Save Benvie/c2ef1253d2a9930950d30e6d70096d5a to your computer and use it in GitHub Desktop.
Save Benvie/c2ef1253d2a9930950d30e6d70096d5a to your computer and use it in GitHub Desktop.
Omega Monad from Haskell translated to JavaScript.
/**
* 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