Skip to main content
Module

x/ramda/times.js

:ram: Practical functional Javascript
Very Popular
Go to Latest
File
import _curry2 from './internal/_curry2';

/** * Calls an input function `n` times, returning an array containing the results * of those function calls. * * `fn` is passed one argument: The current value of `n`, which begins at `0` * and is gradually incremented to `n - 1`. * * @func * @memberOf R * @since v0.2.3 * @category List * @sig (Number -> a) -> Number -> [a] * @param {Function} fn The function to invoke. Passed one argument, the current value of `n`. * @param {Number} n A value between `0` and `n - 1`. Increments after each function call. * @return {Array} An array containing the return values of all calls to `fn`. * @see R.repeat * @example * * R.times(R.identity, 5); //=> [0, 1, 2, 3, 4] * @symb R.times(f, 0) = [] * @symb R.times(f, 1) = [f(0)] * @symb R.times(f, 2) = [f(0), f(1)] */var times = _curry2(function times(fn, n) { var len = Number(n); var idx = 0; var list;
if (len < 0 || isNaN(len)) { throw new RangeError('n must be a non-negative number'); } list = new Array(len); while (idx < len) { list[idx] = fn(idx); idx += 1; } return list;});export default times;