Skip to content

feat: add array/base/with #1374

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
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
151 changes: 151 additions & 0 deletions lib/node_modules/@stdlib/array/base/with/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
<!--

@license Apache-2.0

Copyright (c) 2024 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.

-->

# withArray

> Return a new array after replacing an index with a given value.

<!-- 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 withArray = require( '@stdlib/array/base/with' );
```

#### withArray( x, index, value )

Return a new array after updating an index into the input array.

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

var out = withArray( x, 0, 5 );
// returns [5, 2, 3, 4]

out = withArray( x, -1, 6 );
// returns [1, 2, 3, 6]

```

The function accepts the following arguments:

- **x**: an input array.
- **index**: element index.
- **value**: replacement value.

</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

- If provided an array-like object having a `with` method, the function defers execution to that method and assumes that the method has the following signature:

```text
x.with( index, value )
```

If provided an array-like object without a `with` method, the function manually shallow copied that object and assign provided value to that index.

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

- If provided out-of-bounds indices, the function always returns `undefined`.

</section>

<!-- /.notes -->

<!-- Package usage examples. -->

<section class="examples">

## Examples

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

```javascript
var discreteuniform = require( '@stdlib/random/array/discrete-uniform' );
var withArray = require( '@stdlib/array/base/with' );
var rand = require( '@stdlib/random/base/randu' );
var x;
var indices;

// Define an array:
x = discreteuniform( 10, -100, 100 );

// Define an array containing random index values:
indices = discreteuniform( 100, -x.length, x.length-1 );

// Randomly selected values from the input array:
var i;
var index;
var newvalue;
var updatedarray;
for (i = 0; i < indices.length; i++) {
index = indices[i];
newvalue = rand(); // Random value between -100 and 100
updatedarray = withArray(x, index, newvalue); // Update the value at the given index
console.log('Updated x[%d] to %d', index, newvalue);
console.log('Updated array:', updatedarray);
}
```

</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 -->
51 changes: 51 additions & 0 deletions lib/node_modules/@stdlib/array/base/with/benchmark/benchmark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2024 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 withArray = require( '@stdlib/array/base/with/lib' );
var uniform = require( '@stdlib/random/array/uniform' );
var bench = require( '@stdlib/bench' );
var rand = require( '@stdlib/random/base/randu' );
var pkg = require( '@stdlib/array/base/with/package.json' ).name;


// MAIN //

bench( pkg, function benchmark( b ) {
var value;
var x;
var v;
var i;
var j;

x = uniform( 100, 0.0, 10.0 );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
j = ( i%20 );
value = rand();
v = withArray( x, j, value );
b.equal(v[j], value, 'index ' + j + ' should be updated to ' + value);
}
b.toc();
b.pass( 'benchmark finished' );
b.end();
});
30 changes: 30 additions & 0 deletions lib/node_modules/@stdlib/array/base/with/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

{{alias}}( x, index, value )
Return a new array after replacing an index with a given value.

Parameters
----------
x: ArrayLikeObject
Input array.

index: integer
Index of the element to be replaced.

value: any
Value to replace the element at the specified index with.

Returns
-------
out: ArrayLikeObject
Updated array.

Examples
--------
> var x = [ 1, 2, 3, 4 ];
> {{alias}}( x, 0, 5 )
[ 5, 2, 3, 4 ]
> {{alias}}( x, -1, 6 )
[ 1, 2, 3, 6 ]

See Also
--------
91 changes: 91 additions & 0 deletions lib/node_modules/@stdlib/array/base/with/docs/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* @license Apache-2.0
*
* Copyright (c) 2024 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.
*/

// TypeScript Version: 4.1

/// <reference types="@stdlib/types"/>

import { Collection, AccessorArrayLike, Complex128Array, Complex64Array } from '@stdlib/types/array';

/**
* Returns a new array after updating an index into the input array.
*
* If provided an array-like object having a `with` method , the function defers
* execution to that method and assumes that the method has the following
* signature:
*
* x.with( index, value )
*
* If provided an array-like object without a `with` method, the function manually
* shallow copied that object and assign provided value to that index.
*
* Negative indices are resolved relative to the last array element, with the last
* element corresponding to `-1`.
*
* If provided out-of-bounds indices, the function always returns `undefined`.
*
* @param x - input array
* @param index - element index
* @param value - replacement value
* @returns updated array
*
* @example
* var x = [ 1, 2, 3, 4 ];
* var out = with( x, 0, 5 );
* // returns [ 5, 2, 3, 4 ]

* @example
* var out = with( x, -1, 6 );
* // returns [ 1, 2, 3, 6 ]
*/

/**
* Sets the value at the specified index in a Complex128Array.
*
* @param x - Complex128Array to modify
* @param index - index at which to set the value
* @param value - new value to set
* @returns modified Complex128Array if successful; otherwise, throws a range error.
*/
declare function withArray( x: Complex128Array, index: number, value: any ): Complex128Array | void;

/**
* Sets the value at the specified index in a Complex64Array.
*
* @param x - Complex64Array to modify
* @param index - index at which to set the value
* @param value - new value to set
* @returns modified Complex64Array if successful; otherwise, throws a range error
*/
declare function withArray( x: Complex64Array, index: number, value: any ): Complex64Array | void;

/**
* Sets the value at the specified index in an array and returns the modified array.
*
* @template T - type of elements in the array
* @param x - array to modify, which can be either a Collection or an AccessorArrayLike
* @param index - index at which to set the value
* @param value - new value to set
* @returns modified array if successful; otherwise, throws range error
*/
declare function withArray< T = unknown >( x: Collection<T> | AccessorArrayLike<T>, index: number, value: any ): Collection<T> | AccessorArrayLike<T> | void;


// EXPORTS //

export = withArray;
68 changes: 68 additions & 0 deletions lib/node_modules/@stdlib/array/base/with/docs/types/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* @license Apache-2.0
*
* Copyright (c) 2024 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.
*/

import Complex128Array = require( '@stdlib/array/complex128' );
import Complex64Array = require( '@stdlib/array/complex64' );
import toAccessorArray = require( '@stdlib/array/base/to-accessor-array' );
import withArray = require( './index' );


// TESTS //

// The function returns an updated array...
{
withArray( [ 1, 2, 3, 4 ], 0, 5 ); // $ExpectType void | Collection<number> | AccessorArrayLike<number>
withArray( new Complex128Array( 5 ), 0, { 're': 1.0, 'im': 1.0 } ); // $ExpectType void | Complex128Array
withArray( new Complex64Array( 5 ), 0, { 're': 1.0, 'im': 1.0 } ); // $ExpectType void | Complex64Array
withArray( toAccessorArray( [ 1, 2, 3, 4 ] ), 0, 5 ); // $ExpectType void | Collection<number> | AccessorArrayLike<number>
}

// The compiler throws an error if the function is provided a first argument which is not a collection...
{
withArray( 5, 0, 5 ); // $ExpectError
withArray( true, 0, 5 ); // $ExpectError
withArray( false, 0, 5 ); // $ExpectError
withArray( null, 0, 5 ); // $ExpectError
withArray( void 0, 0, 5 ); // $ExpectError
withArray( {}, 0, 5 ); // $ExpectError
withArray( ( x: number ): number => x, 0, 5 ); // $ExpectError
}

// The compiler throws an error if the function is provided a second argument which is not a number...
{
const x = [ 1, 2, 3, 4 ];

withArray( x, 'abc', 5 ); // $ExpectError
withArray( x, true, 5 ); // $ExpectError
withArray( x, false, 5 ); // $ExpectError
withArray( x, null, 5 ); // $ExpectError
withArray( x, void 0, 5 ); // $ExpectError
withArray( x, [ '1' ], 5 ); // $ExpectError
withArray( x, {}, 5 ); // $ExpectError
withArray( x, ( x: number ): number => x, 5 ); // $ExpectError
}

// The compiler throws an error if the function is provided an unsupported number of arguments...
{
const x = [ 1, 2, 3, 4 ];

withArray(); // $ExpectError
withArray( x ); // $ExpectError
withArray( x, 0 ); // $ExpectError
withArray( x, 0, 0, 5 ); // $ExpectError
}
Loading