> This could work, but the fact that we are using global mutable state ...<p>What? Why would you consider using global state for this? I'd do it like this. Mutability without globals.<p><pre><code> export function transform(ast) {
const queries = [];
walk(ast.root, queries);
return { ast, queries };
}
function walk(node, queries) {
// recursively called on child nodes
// can mutate node, and append to queries
}
</code></pre>
> Surprise! This is exactly the writer monad! This whole post has been a monad tutorial in disguise!<p>Aha. That explains why it stopped making sense about 6 paragraphs ago.