Continuation passing style
If we have recursive calls stacked too high, we already know that our logic will fail. On the other hand, we know that tail calls should alleviate that problem, but don't, because of browser implementations! However, there's a way out of this. Let's first consider how we can transform recursive calls into tail calls by using a well-known FP concept—continuations—and we'll leave the problem of solving TCO limitations for the next section. (We mentioned continuations in the Callbacks, promises, and continuations section of Chapter 3, Starting Out with Functions – A Core Concept, but we didn't go into detail.)
In FP parlance, a continuation is something that represents the state of a process and allows processing to ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access