@@ -16,6 +16,27 @@ function isVector (v) {
16
16
return false
17
17
}
18
18
19
+ /**
20
+ * Returns an array of length `n` filled with the values
21
+ * provided as `data` or computed if `data` is a function.
22
+ * @param {number } n
23
+ * @param {*|function } data `data` can be any literal value,
24
+ * _all the array will be filled with the same value_,
25
+ * or a function with signature `(number i)` that will compute
26
+ * a value based on the index `i` which is `i >= 0 && i < n`.
27
+ * @example <caption>Array of length 10 initialized to 0</caption>
28
+ * newArrayOf(10, 0) // [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
29
+ * @example <caption>Array of length `n` with random values from 0 to `n`</caption>
30
+ * const n = 10
31
+ * newArrayOf(n, () => StdRandom.uniform(n)) // [0, 3, 9, 2, 2, 8, 4, 2, 4, 7]
32
+ * @example <caption>Array of random doubles from [0, 1)</caption>
33
+ * newArrayOf(3, () => StdRandom.uniform(0, 0.999)) // [0.712488850522207, 0.9280585621957204, 0.8184198432124032]
34
+ * newArrayOf(3, () => StdRandom.uniform()) // this is equivalent
35
+ * @example <caption>Array with numbers from 0 to 9</caption>
36
+ * newArrayOf(10, i => i) // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
37
+ * @example <caption>Array of even numbers</caption>
38
+ * newArrayOf(5, i => (i + 1) * 2) // [2, 4, 6, 8, 10]
39
+ */
19
40
function newArrayOf ( n , data ) {
20
41
const a = [ ]
21
42
0 commit comments