O'Reilly logo

Mastering JavaScript Functional Programming by Federico Kereki

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Trampolines and thunks

For the last key to our problem, we shall have to think about the cause of the problem. Each pending recursive call creates a new entry stack. Whenever the stack gets too empty, the program crashes and your algorithm is history. So, if we can work out a way to avoid the stack growth, we should be free. The solution, in this case, is quite sonorous and requires thunks and a trampoline -- let's see what these are!

First, a thunk is really quite simple: it's just a nullary function (so, with no parameters) that helps delay a computation, providing for a form of lazy evaluation. If you have a thunk, unless you call it, you won't get its value. For example, if you want to get the current date and time in ISO format, you ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required