Haskell's `iterate` in Javascript
Haskell’s iterate
accepts a function f
and an arugment x
, and produces an infinite list of repeated application of f
to x
.
iterate f x == [x, f x, f (f x), ...]
It’s handy for recursion.
With ES6’s Generators, this is can be easily implemented in Javascript.
function* iterate(f, x) {
yield x;
while (true) {
x = yield f(x);
}
}
If a takeWhile
is implemented alongside, then we have something that takes a predicate for the base case of the recursion.