iterate-function

Repeatedly apply a function.

Usage no npm install needed!

<script type="module">
  import iterateFunction from 'https://cdn.skypack.dev/iterate-function';
</script>

README

iterate-function

Repeatedly apply a function.

iterate(limit, inital_value, func)

  // or:

iterate(limit, func)

func gets passed two arguments:

iterate(limit, inital_value, func(index, applied_value) { do_stuff(); })

'index' is the current index of the loop. 'applied_value' is the return value of that function for the previous iteration.

Simple example:

  iterate(3, function(i) { console.log(i) })
  > 0
  > 1
  > 2

Using the applied value to get the powers of 2:

power = iterate(4, 1, function(i, applied) { return applied * 2 });
> 16

see similar: http://hackage.haskell.org/packages/archive/base/latest/doc/html/Prelude.html#v:iterate