Skip to content

Commit ab96811

Browse files
committed
feat: add array/base/fill-by
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent b41b329 commit ab96811

File tree

10 files changed

+1598
-0
lines changed

10 files changed

+1598
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
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 -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var pow = require( '@stdlib/math/base/special/pow' );
25+
var isArray = require( '@stdlib/assert/is-array' );
26+
var ones = require( '@stdlib/array/base/ones' );
27+
var pkg = require( './../package.json' ).name;
28+
var fillBy = require( './../lib' );
29+
30+
31+
// FUNCTIONS //
32+
33+
/**
34+
* Callback function.
35+
*
36+
* @private
37+
* @param {*} value - current array element
38+
* @param {NonNegativeInteger} idx - current array element index
39+
* @param {Collection} arr - input array
40+
* @returns {*} fill value
41+
*/
42+
function fcn( value, idx ) {
43+
return value + idx;
44+
}
45+
46+
/**
47+
* Creates a benchmark function.
48+
*
49+
* @private
50+
* @param {PositiveInteger} len - array length
51+
* @returns {Function} benchmark function
52+
*/
53+
function createBenchmark( len ) {
54+
var x = ones( len );
55+
return benchmark;
56+
57+
/**
58+
* Benchmark function.
59+
*
60+
* @private
61+
* @param {Benchmark} b - benchmark instance
62+
*/
63+
function benchmark( b ) {
64+
var out;
65+
var i;
66+
67+
b.tic();
68+
for ( i = 0; i < b.iterations; i++ ) {
69+
out = fillBy( x, 0, len, fcn );
70+
if ( out.length !== len ) {
71+
b.fail( 'unexpected length' );
72+
}
73+
}
74+
b.toc();
75+
if ( !isArray( out ) ) {
76+
b.fail( 'should return an array' );
77+
}
78+
b.pass( 'benchmark finished' );
79+
b.end();
80+
}
81+
}
82+
83+
84+
// MAIN //
85+
86+
/**
87+
* Main execution sequence.
88+
*
89+
* @private
90+
*/
91+
function main() {
92+
var len;
93+
var min;
94+
var max;
95+
var f;
96+
var i;
97+
98+
min = 1; // 10^min
99+
max = 6; // 10^max
100+
101+
for ( i = min; i <= max; i++ ) {
102+
len = pow( 10, i );
103+
104+
f = createBenchmark( len );
105+
bench( pkg+':dtype=generic,len='+len, f );
106+
}
107+
}
108+
109+
main();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2+
{{alias}}( x, start, end, fcn[, thisArg] )
3+
Fills all elements within a portion of an array according to a provided
4+
callback function.
5+
6+
This function mutates the input array.
7+
8+
Negative `start` and `end` indices are resolved to positive indices by
9+
counting from the end of the array with `-1` corresponding to the last
10+
indexed element.
11+
12+
Parameters
13+
----------
14+
x: ArrayLikeObject
15+
Input array.
16+
17+
start: integer
18+
Starting index (inclusive).
19+
20+
end: integer
21+
Ending index (exclusive).
22+
23+
fcn: Function
24+
Callback function.
25+
26+
thisArg: any (optional)
27+
Callback function execution context.
28+
29+
Returns
30+
-------
31+
out: ArrayLikeObject
32+
Modified input array.
33+
34+
Examples
35+
--------
36+
> function fcn() { return 5; };
37+
> var out = {{alias}}( [ 1, 2, 3, 4 ], 1, 3, fcn )
38+
[ 1, 5, 5, 4 ]
39+
40+
See Also
41+
--------
42+

0 commit comments

Comments
 (0)