-
-
Notifications
You must be signed in to change notification settings - Fork 846
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
Planeshifter
merged 8 commits into
stdlib-js:develop
from
Pratik772846:package-stdlib/array/base/with
Feb 26, 2024
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
51e277a
feat: added @stdlib/array/base/with
Pratik772846 1159eba
Apply suggestions from code review
Planeshifter 7d39708
Merge branch 'stdlib-js:develop' into package-stdlib/array/base/with
Pratik772846 ede3880
feat: added @stdlib/array/base/with
Pratik772846 33d0e3a
Apply suggestions from code review
Planeshifter 7bbfc7d
Update lib/node_modules/@stdlib/array/base/with/package.json
Planeshifter 4546ff2
Update lib/node_modules/@stdlib/array/base/with/lib/index.js
Planeshifter 003e3bb
Update lib/node_modules/@stdlib/array/base/with/lib/main.js
Planeshifter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
51
lib/node_modules/@stdlib/array/base/with/benchmark/benchmark.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
91
lib/node_modules/@stdlib/array/base/with/docs/types/index.d.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 // | ||
Planeshifter marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
export = withArray; |
68 changes: 68 additions & 0 deletions
68
lib/node_modules/@stdlib/array/base/with/docs/types/test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.