Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
195 changes: 195 additions & 0 deletions lib/node_modules/@stdlib/array/base/to-filled-by/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
<!--

@license Apache-2.0

Copyright (c) 2026 The Stdlib Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-->

# toFilledBy

> Return a new array with all elements within a specified range replaced according to a provided callback function.

<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->

<section class="intro">

</section>

<!-- /.intro -->

<!-- Package usage documentation. -->

<section class="usage">

## Usage

```javascript
var toFilledBy = require( '@stdlib/array/base/to-filled-by' );
```

#### toFilledBy( x, start, end, fcn\[, thisArg] )

Returns a new array with all elements within a specified range replaced according to a provided callback function.

```javascript
function fcn() {
return 5;
}

var x = [ 1, 2, 3, 4 ];

var out = toFilledBy( x, 1, 3, fcn );
// returns [ 1, 5, 5, 4 ]

out = toFilledBy( x, -3, -1, fcn );
// returns [ 1, 5, 5, 4 ]
```

The function accepts the following arguments:

- **x**: an input array.
- **start**: starting index (inclusive).
- **end**: ending index (exclusive).
- **fcn**: callback function.
- **thisArg**: callback function execution context.

The callback function is provided the following arguments:

- **value**: current array element.
- **index**: current array element index.
- **arr**: input array.

To set the callback execution context, provide a `thisArg`.

<!-- eslint-disable no-invalid-this -->

```javascript
function fcn() {
this.count += 1;
return 10;
}

var x = [ 1, 2, 3, 4, 5, 6 ];

var ctx = {
'count': 0
};
var out = toFilledBy( x, 1, 4, fcn, ctx );
// returns [ 1, 10, 10, 10, 5, 6 ]

var cnt = ctx.count;
// returns 3
```

#### toFilledBy.assign( x, start, end, fcn, out, stride, offset\[, thisArg] )

Copies elements from one array to another array and replaces all elements within a specified range according to a provided callback function.

```javascript
function fcn() {
return 5;
}

var x = [ 1, 2, 3, 4 ];

var out = [ 0, 0, 0, 0 ];
var arr = toFilledBy.assign( x, 1, 3, fcn, out, 1, 0 );
// returns [ 1, 5, 5, 4 ]

var bool = ( arr === out );
// returns true
```

The function accepts the following arguments:

- **x**: an input array.
- **start**: starting index (inclusive).
- **end**: ending index (exclusive).
- **fcn**: callback function.
- **out**: output array.
- **stride**: output array stride.
- **offset**: output array offset.
- **thisArg**: callback function execution context.

</section>

<!-- /.usage -->

<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="notes">

## Notes

- Negative indices are resolved relative to the last array element, with the last element corresponding to `-1`.

</section>

<!-- /.notes -->

<!-- Package usage examples. -->

<section class="examples">

## Examples

<!-- eslint no-undef: "error" -->

```javascript
var zeroTo = require( '@stdlib/array/base/zero-to' );
var toFilledBy = require( '@stdlib/array/base/to-filled-by' );

function fcn( value ) {
return value * 10;
}

var x = zeroTo( 6 );
// returns [ 0, 1, 2, 3, 4, 5 ]

var y = toFilledBy( x, 1, 4, fcn );
// returns [ 0, 10, 20, 30, 4, 5 ]

var z = toFilledBy( x, -3, -1, fcn );
// returns [ 0, 1, 2, 30, 40, 5 ]
```

</section>

<!-- /.examples -->

<!-- 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. -->

<section class="references">

</section>

<!-- /.references -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">

</section>

<!-- /.related -->

<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="links">

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2026 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var bench = require( '@stdlib/bench' );
var pow = require( '@stdlib/math/base/special/pow' );
var isArray = require( '@stdlib/assert/is-array' );
var ones = require( '@stdlib/array/base/ones' );
var zeros = require( '@stdlib/array/base/zeros' );
var constantFunction = require( '@stdlib/utils/constant-function' );
var format = require( '@stdlib/string/format' );
var pkg = require( './../package.json' ).name;
var toFilledBy = require( './../lib' );


// FUNCTIONS //

/**
* Creates a benchmark function.
*
* @private
* @param {PositiveInteger} len - array length
* @returns {Function} benchmark function
*/
function createBenchmark( len ) {
var fcn = constantFunction( 5 );
var out = zeros( len );
var x = ones( len );
return benchmark;

/**
* Benchmark function.
*
* @private
* @param {Benchmark} b - benchmark instance
*/
function benchmark( b ) {
var v;
var i;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
v = toFilledBy.assign( x, 0, len, fcn, out, 1, 0 );
if ( typeof v !== 'object' ) {
b.fail( 'should return an array' );
}
}
b.toc();
if ( !isArray( v ) ) {
b.fail( 'should return an array' );
}
b.pass( 'benchmark finished' );
b.end();
}
}


// MAIN //

/**
* Main execution sequence.
*
* @private
*/
function main() {
var len;
var min;
var max;
var f;
var i;

min = 1; // 10^min
max = 6; // 10^max

for ( i = min; i <= max; i++ ) {
len = pow( 10, i );

f = createBenchmark( len );
bench( format( '%s:assign:dtype=generic,len=%d', pkg, len ), f );
}
}

main();
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2026 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var bench = require( '@stdlib/bench' );
var pow = require( '@stdlib/math/base/special/pow' );
var isArray = require( '@stdlib/assert/is-array' );
var ones = require( '@stdlib/array/base/ones' );
var constantFunction = require( '@stdlib/utils/constant-function' );
var format = require( '@stdlib/string/format' );
var pkg = require( './../package.json' ).name;
var toFilledBy = require( './../lib' );


// FUNCTIONS //

/**
* Creates a benchmark function.
*
* @private
* @param {PositiveInteger} len - array length
* @returns {Function} benchmark function
*/
function createBenchmark( len ) {
var fcn = constantFunction( 5 );
var x = ones( len );
return benchmark;

/**
* Benchmark function.
*
* @private
* @param {Benchmark} b - benchmark instance
*/
function benchmark( b ) {
var out;
var i;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = toFilledBy( x, 0, len, fcn );
if ( out.length !== len ) {
b.fail( 'unexpected length' );
}
}
b.toc();
if ( !isArray( out ) ) {
b.fail( 'should return an array' );
}
b.pass( 'benchmark finished' );
b.end();
}
}


// MAIN //

/**
* Main execution sequence.
*
* @private
*/
function main() {
var len;
var min;
var max;
var f;
var i;

min = 1; // 10^min
max = 6; // 10^max

for ( i = min; i <= max; i++ ) {
len = pow( 10, i );

f = createBenchmark( len );
bench( format( '%s:dtype=generic,len=%d', pkg, len ), f );
}
}

main();
Loading