|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2025 The Stdlib Authors. |
| 6 | +
|
| 7 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +you may not use this file except in compliance with the License. |
| 9 | +You may obtain a copy of the License at |
| 10 | +
|
| 11 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +
|
| 13 | +Unless required by applicable law or agreed to in writing, software |
| 14 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +See the License for the specific language governing permissions and |
| 17 | +limitations under the License. |
| 18 | +
|
| 19 | +--> |
| 20 | + |
| 21 | +# fillBy |
| 22 | + |
| 23 | +> Fill all elements within a portion of an array according to a callback function. |
| 24 | +
|
| 25 | +<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> |
| 26 | + |
| 27 | +<section class="intro"> |
| 28 | + |
| 29 | +</section> |
| 30 | + |
| 31 | +<!-- /.intro --> |
| 32 | + |
| 33 | +<!-- Package usage documentation. --> |
| 34 | + |
| 35 | +<section class="usage"> |
| 36 | + |
| 37 | +## Usage |
| 38 | + |
| 39 | +```javascript |
| 40 | +var fillBy = require( '@stdlib/array/base/fill-by' ); |
| 41 | +``` |
| 42 | + |
| 43 | +#### fillBy( x, start, end, fcn\[, thisArg] ) |
| 44 | + |
| 45 | +Fills all elements within a portion of an array from an inclusive `start` index to an exclusive `end` index according to a provided callback function. |
| 46 | + |
| 47 | +```javascript |
| 48 | +function fcn() { |
| 49 | + return 10; |
| 50 | +} |
| 51 | + |
| 52 | +var x = [ 1, 2, 3, 4, 5, 6 ]; |
| 53 | + |
| 54 | +var out = fillBy( x, 1, 4, fcn ); |
| 55 | +// returns [ 1, 10, 10, 10, 5, 6 ] |
| 56 | + |
| 57 | +var bool = ( out === x ); |
| 58 | +// returns true |
| 59 | +``` |
| 60 | + |
| 61 | +The callback function is provided the following arguments: |
| 62 | + |
| 63 | +- **value**: current array element. |
| 64 | +- **index**: current array element index. |
| 65 | +- **arr**: input array. |
| 66 | + |
| 67 | +To set the callback execution context, provide a `thisArg`. |
| 68 | + |
| 69 | +<!-- eslint-disable no-invalid-this --> |
| 70 | + |
| 71 | +```javascript |
| 72 | +function fcn() { |
| 73 | + this.count += 1; |
| 74 | + return 10; |
| 75 | +} |
| 76 | + |
| 77 | +var x = [ 1, 2, 3, 4, 5, 6 ]; |
| 78 | + |
| 79 | +var ctx = { |
| 80 | + 'count': 0 |
| 81 | +}; |
| 82 | +var out = fillBy( x, 1, 4, fcn, ctx ); |
| 83 | +// returns [ 1, 10, 10, 10, 5, 6 ] |
| 84 | + |
| 85 | +var bool = ( out === x ); |
| 86 | +// returns true |
| 87 | + |
| 88 | +var cnt = ctx.count; |
| 89 | +// returns 3 |
| 90 | +``` |
| 91 | + |
| 92 | +</section> |
| 93 | + |
| 94 | +<!-- /.usage --> |
| 95 | + |
| 96 | +<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 97 | + |
| 98 | +<section class="notes"> |
| 99 | + |
| 100 | +## Notes |
| 101 | + |
| 102 | +- Negative `start` and `end` indices are resolved to positive indices by counting from the end of the array with `-1` corresponding to the last indexed element. |
| 103 | +- The function **mutates** the input array. |
| 104 | + |
| 105 | +</section> |
| 106 | + |
| 107 | +<!-- /.notes --> |
| 108 | + |
| 109 | +<!-- Package usage examples. --> |
| 110 | + |
| 111 | +<section class="examples"> |
| 112 | + |
| 113 | +## Examples |
| 114 | + |
| 115 | +<!-- eslint no-undef: "error" --> |
| 116 | + |
| 117 | +```javascript |
| 118 | +var constantFunction = require( '@stdlib/utils/constant-function' ); |
| 119 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 120 | +var zeroTo = require( '@stdlib/array/base/zero-to' ); |
| 121 | +var fillBy = require( '@stdlib/array/base/fill-by' ); |
| 122 | + |
| 123 | +var x = new Float64Array( zeroTo( 6 ) ); |
| 124 | +// returns <Float64Array>[ 0.0, 1.0, 2.0, 3.0, 4.0, 5.0 ] |
| 125 | + |
| 126 | +var y = fillBy( x, 0, 6, constantFunction( 10.0 ) ); |
| 127 | +// returns <Float64Array>[ 10.0, 10.0, 10.0, 10.0, 10.0, 10.0 ] |
| 128 | + |
| 129 | +y = fillBy( x, 0, 4, constantFunction( 0.0 ) ); |
| 130 | +// returns <Float64Array>[ 0.0, 0.0, 0.0, 0.0, 10.0, 10.0 ] |
| 131 | + |
| 132 | +y = fillBy( x, 2, 6, constantFunction( 20.0 ) ); |
| 133 | +// returns <Float64Array>[ 0.0, 0.0, 20.0, 20.0, 20.0, 20.0 ] |
| 134 | + |
| 135 | +y = fillBy( x, 2, 4, constantFunction( 30.0 ) ); |
| 136 | +// returns <Float64Array>[ 0.0, 0.0, 30.0, 30.0, 20.0, 20.0 ] |
| 137 | +``` |
| 138 | + |
| 139 | +</section> |
| 140 | + |
| 141 | +<!-- /.examples --> |
| 142 | + |
| 143 | +<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 144 | + |
| 145 | +<section class="references"> |
| 146 | + |
| 147 | +</section> |
| 148 | + |
| 149 | +<!-- /.references --> |
| 150 | + |
| 151 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 152 | + |
| 153 | +<section class="related"> |
| 154 | + |
| 155 | +</section> |
| 156 | + |
| 157 | +<!-- /.related --> |
| 158 | + |
| 159 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 160 | + |
| 161 | +<section class="links"> |
| 162 | + |
| 163 | +</section> |
| 164 | + |
| 165 | +<!-- /.links --> |
0 commit comments