How do I replace while loops with a functional programming alternative without tail call optimization? How do I replace while loops with a functional programming alternative without tail call optimization? javascript javascript

How do I replace while loops with a functional programming alternative without tail call optimization?


An example in JavaScript

Here's an example using JavaScript. Currently, most browsers do not support tail call optimisation and therefore the following snippet will fail

const repeat = n => f => x =>  n === 0 ? x : repeat (n - 1) (f) (f(x))  console.log(repeat(1e3) (x => x + 1) (0)) // 1000console.log(repeat(1e5) (x => x + 1) (0)) // Error: Uncaught RangeError: Maximum call stack size exceeded