shared-helper-recursive-with-callback.ts 696 B

123456789101112131415
  1. // Genuine recursion with a callback parameter: `walkTree` passes its own `visit` on to the
  2. // re-entrant call, so the inner activation has the SAME bindings as the outer one. That is
  3. // recursion: one `loop recursive` region, the callback inlined once inside it, and a `recur`
  4. // jump at the re-entrant call.
  5. interface Tree {
  6. name: string;
  7. children: Tree[];
  8. }
  9. async function walkTree(node: Tree, visit: (n: Tree) => Promise<string>): Promise<void> {
  10. await visit(node);
  11. for (const child of node.children) await walkTree(child, visit);
  12. }
  13. const root: Tree = { name: "r", children: [] };
  14. await walkTree(root, async (n) => agent(`v-${n.name}`).ask<string>(`visit ${n.name}`));
  15. return "done";