[Power Query] Experimenting with thunks and trampolines
This example demonstrates a manual implementation of tail-call optimization using thunks and a trampoline, written entirely in Power Query. It shows how recursive functions can be restructured to avoid stack overflow by wrapping each recursive call in a thunk—a zero-argument function—and then driving the computation forward with a trampoline that repeatedly invokes the current thunk until a non-function value is produced. The code defines two custom functions: `make_thunk.pq`, which uses `Function.Invoke` to delay a function call, and `trampoline.pq`, which uses `List.Generate` with type checking via `Type.Is` and `Value.Type` to iterate until the result is no longer a function. The example highlights a purely functional, dataflow-driven approach to control flow, with no external libraries or visual rendering, relying solely on Power Query’s standard library.
AI-generated description