UNPKG

1.19 kBJavaScriptView Raw
1var _curry2 =
2/*#__PURE__*/
3require("./internal/_curry2");
4/**
5 * Calls an input function `n` times, returning an array containing the results
6 * of those function calls.
7 *
8 * `fn` is passed one argument: The current value of `n`, which begins at `0`
9 * and is gradually incremented to `n - 1`.
10 *
11 * @func
12 * @memberOf R
13 * @since v0.2.3
14 * @category List
15 * @sig (Number -> a) -> Number -> [a]
16 * @param {Function} fn The function to invoke. Passed one argument, the current value of `n`.
17 * @param {Number} n A value between `0` and `n - 1`. Increments after each function call.
18 * @return {Array} An array containing the return values of all calls to `fn`.
19 * @see R.repeat
20 * @example
21 *
22 * R.times(R.identity, 5); //=> [0, 1, 2, 3, 4]
23 * @symb R.times(f, 0) = []
24 * @symb R.times(f, 1) = [f(0)]
25 * @symb R.times(f, 2) = [f(0), f(1)]
26 */
27
28
29var times =
30/*#__PURE__*/
31_curry2(function times(fn, n) {
32 var len = Number(n);
33 var idx = 0;
34 var list;
35
36 if (len < 0 || isNaN(len)) {
37 throw new RangeError('n must be a non-negative number');
38 }
39
40 list = new Array(len);
41
42 while (idx < len) {
43 list[idx] = fn(idx);
44 idx += 1;
45 }
46
47 return list;
48});
49
50module.exports = times;
\No newline at end of file