Skip to content

Commit 332f138

Browse files
committed
Auto-generated commit
1 parent 1c620a8 commit 332f138

File tree

12 files changed

+830
-1
lines changed

12 files changed

+830
-1
lines changed

.npmignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ branches.md
2929
.postinstall.json
3030
Makefile
3131

32-
# Ignore `binding.gyp` file to avoid compilation of native addon when installing package:
32+
# Ignore files to avoid compilation of native addon when installing package:
3333
binding.gyp
34+
include.gypi
3435

3536
# Directories #
3637
###############

CONTRIBUTORS

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Dominik Moritz <[email protected]>
1515
Dorrin Sotoudeh <[email protected]>
1616
Frank Kovacs <[email protected]>
1717
GUNJ JOSHI <[email protected]>
18+
1819
Harshita Kalani <[email protected]>
1920
James Gelok <[email protected]>
2021
Jaysukh Makvana <[email protected]>
@@ -24,6 +25,7 @@ Jordan Gallivan <[email protected]>
2425
Joris Labie <[email protected]>
2526
Justin Dennison <[email protected]>
2627
Karthik Prakash <[email protected]>
28+
2729
Marcus Fantham <[email protected]>
2830
Matt Cochrane <[email protected]>
2931
Milan Raj <[email protected]>

base/with/README.md

+151
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2024 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+
# withArray
22+
23+
> Return a new array after replacing an index with a given value.
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 withArray = require( '@stdlib/array/base/with' );
41+
```
42+
43+
#### withArray( x, index, value )
44+
45+
Return a new array after updating an index into the input array.
46+
47+
```javascript
48+
var x = [ 1, 2, 3, 4 ];
49+
50+
var out = withArray( x, 0, 5 );
51+
// returns [5, 2, 3, 4]
52+
53+
out = withArray( x, -1, 6 );
54+
// returns [1, 2, 3, 6]
55+
56+
```
57+
58+
The function accepts the following arguments:
59+
60+
- **x**: an input array.
61+
- **index**: element index.
62+
- **value**: replacement value.
63+
64+
</section>
65+
66+
<!-- /.usage -->
67+
68+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
69+
70+
<section class="notes">
71+
72+
## Notes
73+
74+
- 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:
75+
76+
```text
77+
x.with( index, value )
78+
```
79+
80+
If provided an array-like object without a `with` method, the function manually shallow copied that object and assign provided value to that index.
81+
82+
- Negative indices are resolved relative to the last array element, with the last element corresponding to `-1`.
83+
84+
- If provided out-of-bounds indices, the function always returns `undefined`.
85+
86+
</section>
87+
88+
<!-- /.notes -->
89+
90+
<!-- Package usage examples. -->
91+
92+
<section class="examples">
93+
94+
## Examples
95+
96+
<!-- eslint no-undef: "error" -->
97+
98+
```javascript
99+
var discreteuniform = require( '@stdlib/random/array/discrete-uniform' );
100+
var withArray = require( '@stdlib/array/base/with' );
101+
var rand = require( '@stdlib/random/base/randu' );
102+
var x;
103+
var indices;
104+
105+
// Define an array:
106+
x = discreteuniform( 10, -100, 100 );
107+
108+
// Define an array containing random index values:
109+
indices = discreteuniform( 100, -x.length, x.length-1 );
110+
111+
// Randomly selected values from the input array:
112+
var i;
113+
var index;
114+
var newvalue;
115+
var updatedarray;
116+
for (i = 0; i < indices.length; i++) {
117+
index = indices[i];
118+
newvalue = rand(); // Random value between -100 and 100
119+
updatedarray = withArray(x, index, newvalue); // Update the value at the given index
120+
console.log('Updated x[%d] to %d', index, newvalue);
121+
console.log('Updated array:', updatedarray);
122+
}
123+
```
124+
125+
</section>
126+
127+
<!-- /.examples -->
128+
129+
<!-- 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. -->
130+
131+
<section class="references">
132+
133+
</section>
134+
135+
<!-- /.references -->
136+
137+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
138+
139+
<section class="related">
140+
141+
</section>
142+
143+
<!-- /.related -->
144+
145+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
146+
147+
<section class="links">
148+
149+
</section>
150+
151+
<!-- /.links -->

base/with/benchmark/benchmark.js

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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 withArray = require( './../../../base/with/lib' );
24+
var uniform = require( '@stdlib/random/array/uniform' );
25+
var bench = require( '@stdlib/bench' );
26+
var rand = require( '@stdlib/random/base/randu' );
27+
var pkg = require( './../../../base/with/package.json' ).name;
28+
29+
30+
// MAIN //
31+
32+
bench( pkg, function benchmark( b ) {
33+
var value;
34+
var x;
35+
var v;
36+
var i;
37+
var j;
38+
39+
x = uniform( 100, 0.0, 10.0 );
40+
41+
b.tic();
42+
for ( i = 0; i < b.iterations; i++ ) {
43+
j = ( i%20 );
44+
value = rand();
45+
v = withArray( x, j, value );
46+
b.equal(v[j], value, 'index ' + j + ' should be updated to ' + value);
47+
}
48+
b.toc();
49+
b.pass( 'benchmark finished' );
50+
b.end();
51+
});

base/with/docs/repl.txt

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
{{alias}}( x, index, value )
3+
Return a new array after replacing an index with a given value.
4+
5+
Parameters
6+
----------
7+
x: ArrayLikeObject
8+
Input array.
9+
10+
index: integer
11+
Index of the element to be replaced.
12+
13+
value: any
14+
Value to replace the element at the specified index with.
15+
16+
Returns
17+
-------
18+
out: ArrayLikeObject
19+
Updated array.
20+
21+
Examples
22+
--------
23+
> var x = [ 1, 2, 3, 4 ];
24+
> {{alias}}( x, 0, 5 )
25+
[ 5, 2, 3, 4 ]
26+
> {{alias}}( x, -1, 6 )
27+
[ 1, 2, 3, 6 ]
28+
29+
See Also
30+
--------

base/with/docs/types/index.d.ts

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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+
// TypeScript Version: 4.1
20+
21+
/// <reference types="@stdlib/types"/>
22+
23+
import { Collection, AccessorArrayLike, Complex128Array, Complex64Array } from '@stdlib/types/array';
24+
25+
/**
26+
* Returns a new array after updating an index into the input array.
27+
*
28+
* If provided an array-like object having a `with` method , the function defers
29+
* execution to that method and assumes that the method has the following
30+
* signature:
31+
*
32+
* x.with( index, value )
33+
*
34+
* If provided an array-like object without a `with` method, the function manually
35+
* shallow copied that object and assign provided value to that index.
36+
*
37+
* Negative indices are resolved relative to the last array element, with the last
38+
* element corresponding to `-1`.
39+
*
40+
* If provided out-of-bounds indices, the function always returns `undefined`.
41+
*
42+
* @param x - input array
43+
* @param index - element index
44+
* @param value - replacement value
45+
* @returns updated array
46+
*
47+
* @example
48+
* var x = [ 1, 2, 3, 4 ];
49+
* var out = with( x, 0, 5 );
50+
* // returns [ 5, 2, 3, 4 ]
51+
52+
* @example
53+
* var out = with( x, -1, 6 );
54+
* // returns [ 1, 2, 3, 6 ]
55+
*/
56+
57+
/**
58+
* Sets the value at the specified index in a Complex128Array.
59+
*
60+
* @param x - Complex128Array to modify
61+
* @param index - index at which to set the value
62+
* @param value - new value to set
63+
* @returns modified Complex128Array if successful; otherwise, throws a range error.
64+
*/
65+
declare function withArray( x: Complex128Array, index: number, value: any ): Complex128Array | void;
66+
67+
/**
68+
* Sets the value at the specified index in a Complex64Array.
69+
*
70+
* @param x - Complex64Array to modify
71+
* @param index - index at which to set the value
72+
* @param value - new value to set
73+
* @returns modified Complex64Array if successful; otherwise, throws a range error
74+
*/
75+
declare function withArray( x: Complex64Array, index: number, value: any ): Complex64Array | void;
76+
77+
/**
78+
* Sets the value at the specified index in an array and returns the modified array.
79+
*
80+
* @template T - type of elements in the array
81+
* @param x - array to modify, which can be either a Collection or an AccessorArrayLike
82+
* @param index - index at which to set the value
83+
* @param value - new value to set
84+
* @returns modified array if successful; otherwise, throws range error
85+
*/
86+
declare function withArray< T = unknown >( x: Collection<T> | AccessorArrayLike<T>, index: number, value: any ): Collection<T> | AccessorArrayLike<T> | void;
87+
88+
89+
// EXPORTS //
90+
91+
export = withArray;

0 commit comments

Comments
 (0)