Skip to content

Commit 0737d84

Browse files
committed
refactor: update implementation, fix TypeScript annotations, and clean-up
Ref: #1374 Ref: 4253607
1 parent 6f61ff6 commit 0737d84

File tree

10 files changed

+519
-254
lines changed

10 files changed

+519
-254
lines changed

lib/node_modules/@stdlib/array/base/with/README.md

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ limitations under the License.
1818
1919
-->
2020

21-
# withArray
21+
# arrayWith
2222

23-
> Return a new array after replacing an index with a given value.
23+
> Return a new array with the element at the specified index replaced with a provided value.
2424
2525
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
2626

@@ -37,21 +37,21 @@ limitations under the License.
3737
## Usage
3838

3939
```javascript
40-
var withArray = require( '@stdlib/array/base/with' );
40+
var arrayWith = require( '@stdlib/array/base/with' );
4141
```
4242

43-
#### withArray( x, index, value )
43+
#### arrayWith( x, index, value )
4444

45-
Return a new array after updating an index into the input array.
45+
Returns a new array with the element at the specified index replaced with a provided value.
4646

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

50-
var out = withArray( x, 0, 5 );
51-
// returns [5, 2, 3, 4]
50+
var out = arrayWith( x, 0, 5 );
51+
// returns [ 5, 2, 3, 4 ]
5252

53-
out = withArray( x, -1, 6 );
54-
// returns [1, 2, 3, 6]
53+
out = arrayWith( x, -1, 6 );
54+
// returns [ 1, 2, 3, 6 ]
5555

5656
```
5757

@@ -77,12 +77,10 @@ The function accepts the following arguments:
7777
x.with( index, value )
7878
```
7979
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.
80+
If provided an array-like object without a `with` method, the function shallow copies input array data to a new generic array, normalizes a provided index, and sets a specified element.
8181
8282
- Negative indices are resolved relative to the last array element, with the last element corresponding to `-1`.
8383
84-
- If provided out-of-bounds indices, the function always returns `undefined`.
85-
8684
</section>
8785
8886
<!-- /.notes -->
@@ -96,29 +94,25 @@ The function accepts the following arguments:
9694
<!-- eslint no-undef: "error" -->
9795
9896
```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;
97+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
98+
var arrayWith = require( '@stdlib/array/base/with' );
10499
105100
// Define an array:
106-
x = discreteuniform( 10, -100, 100 );
101+
var opts = {
102+
'dtype': 'generic'
103+
};
104+
var x = discreteUniform( 5, -100, 100, opts );
107105
108106
// Define an array containing random index values:
109-
indices = discreteuniform( 100, -x.length, x.length-1 );
107+
var indices = discreteUniform( 100, -x.length, x.length-1, opts );
108+
109+
// Define an array with random values to set:
110+
var values = discreteUniform( indices.length, -10000, 10000, opts );
110111
111-
// Randomly selected values from the input array:
112+
// Randomly set elements in the input array:
112113
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);
114+
for ( i = 0; i < indices.length; i++ ) {
115+
console.log( 'x = [%s]', arrayWith( x, indices[ i ], values[ i ] ).join( ',' ) );
122116
}
123117
```
124118

lib/node_modules/@stdlib/array/base/with/benchmark/benchmark.js

Lines changed: 65 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,32 +20,77 @@
2020

2121
// MODULES //
2222

23-
var withArray = require( '@stdlib/array/base/with/lib' );
24-
var uniform = require( '@stdlib/random/array/uniform' );
2523
var bench = require( '@stdlib/bench' );
26-
var rand = require( '@stdlib/random/base/randu' );
27-
var pkg = require( '@stdlib/array/base/with/package.json' ).name;
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 arrayWith = require( './../lib' );
29+
30+
31+
// FUNCTIONS //
32+
33+
/**
34+
* Creates a benchmark function.
35+
*
36+
* @private
37+
* @param {PositiveInteger} len - array length
38+
* @returns {Function} benchmark function
39+
*/
40+
function createBenchmark( len ) {
41+
var x = ones( len );
42+
return benchmark;
43+
44+
/**
45+
* Benchmark function.
46+
*
47+
* @private
48+
* @param {Benchmark} b - benchmark instance
49+
*/
50+
function benchmark( b ) {
51+
var out;
52+
var i;
53+
54+
b.tic();
55+
for ( i = 0; i < b.iterations; i++ ) {
56+
out = arrayWith( x, i%len, i );
57+
if ( out.length !== len ) {
58+
b.fail( 'unexpected length' );
59+
}
60+
}
61+
b.toc();
62+
if ( !isArray( out ) ) {
63+
b.fail( 'should return an array' );
64+
}
65+
b.pass( 'benchmark finished' );
66+
b.end();
67+
}
68+
}
2869

2970

3071
// MAIN //
3172

32-
bench( pkg, function benchmark( b ) {
33-
var value;
34-
var x;
35-
var v;
73+
/**
74+
* Main execution sequence.
75+
*
76+
* @private
77+
*/
78+
function main() {
79+
var len;
80+
var min;
81+
var max;
82+
var f;
3683
var i;
37-
var j;
3884

39-
x = uniform( 100, 0.0, 10.0 );
85+
min = 1; // 10^min
86+
max = 6; // 10^max
87+
88+
for ( i = min; i <= max; i++ ) {
89+
len = pow( 10, i );
4090

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);
91+
f = createBenchmark( len );
92+
bench( pkg+':dtype=generic,len='+len, f );
4793
}
48-
b.toc();
49-
b.pass( 'benchmark finished' );
50-
b.end();
51-
});
94+
}
95+
96+
main();

lib/node_modules/@stdlib/array/base/with/docs/repl.txt

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
11

22
{{alias}}( x, index, value )
3-
Return a new array after replacing an index with a given value.
3+
Returns a new array with the element at the specified index replaced with a
4+
provided value.
5+
6+
Negative indices are resolved relative to the last array element, with the
7+
last element corresponding to `-1`.
8+
9+
If provided an array-like object having a `with` method , the function
10+
defers execution to that method and assumes that the method has the
11+
following signature:
12+
13+
x.with( index, value )
14+
15+
If provided an array-like object without a `with` method, the function
16+
shallow copies input array data to a new generic array, normalizes a
17+
provided index, and sets a specified element.
418

519
Parameters
620
----------
@@ -11,12 +25,12 @@
1125
Index of the element to be replaced.
1226

1327
value: any
14-
Value to replace the element at the specified index with.
28+
Replacement value.
1529

1630
Returns
1731
-------
1832
out: ArrayLikeObject
19-
Updated array.
33+
Output array.
2034

2135
Examples
2236
--------
@@ -25,6 +39,9 @@
2539
[ 5, 2, 3, 4 ]
2640
> {{alias}}( x, -1, 6 )
2741
[ 1, 2, 3, 6 ]
42+
> x
43+
[ 1, 2, 3, 4 ]
2844

2945
See Also
3046
--------
47+

lib/node_modules/@stdlib/array/base/with/docs/types/index.d.ts

Lines changed: 47 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -20,70 +20,67 @@
2020

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

23-
import { Collection, AccessorArrayLike, Complex128Array, Complex64Array } from '@stdlib/types/array';
23+
import { Collection, RealTypedArray, ComplexTypedArray } from '@stdlib/types/array';
24+
import { ComplexLike } from '@stdlib/types/complex';
2425

2526
/**
26-
* Returns a new array after updating an index into the input array.
27+
* Returns a new array with the element at the specified index replaced with a provided value.
2728
*
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 )
29+
* @param x - input array
30+
* @param index - index at which to set a provided value
31+
* @param value - replacement value
32+
* @returns output array
3333
*
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.
34+
* @example
35+
* var Complex128Array = require( '@stdlib/array/complex128' );
36+
* var Complex128 = require( '@stdlib/complex/float64' );
3637
*
37-
* Negative indices are resolved relative to the last array element, with the last
38-
* element corresponding to `-1`.
38+
* var x = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
3939
*
40-
* If provided out-of-bounds indices, the function always returns `undefined`.
40+
* var out = withArray( x, 0, new Complex128( 7.0, 8.0 ) );
41+
* // returns <Complex128Array>[ 7.0, 8.0, 3.0, 4.0, 5.0, 6.0 ]
42+
*/
43+
declare function withArray<T extends ComplexTypedArray>( x: T, index: number, value: ComplexLike ): T;
44+
45+
/**
46+
* Returns a new array with the element at the specified index replaced with a provided value.
4147
*
4248
* @param x - input array
43-
* @param index - element index
49+
* @param index - index at which to set a provided value
4450
* @param value - replacement value
45-
* @returns updated array
51+
* @returns output array
4652
*
4753
* @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 ]
54+
* var Float64Array = require( '@stdlib/array/float64' );
55+
*
56+
* var x = new Float64Array( [ 1.0, 2.0, 3.0 ] );
57+
*
58+
* var out = withArray( x, 0, 5.0 );
59+
* // returns <Float64Array>[ 5.0, 2.0, 3.0 ]
5560
*/
61+
declare function withArray<T extends RealTypedArray>( x: T, index: number, value: number ): T; // eslint-disable-line @typescript-eslint/unified-signatures
5662

5763
/**
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;
64+
* Returns a new array with the element at the specified index replaced with a provided value.
65+
*
66+
* @param x - input array
67+
* @param index - index at which to set a provided value
68+
* @param value - replacement value
69+
* @returns output array
70+
*
71+
* @example
72+
* var x = [ 1, 2, 3 ];
73+
*
74+
* var out = withArray( x, 0, 7 );
75+
* // returns [ 7, 2, 3 ]
76+
*
77+
* @example
78+
* var x = [ 1, 2, 3, 4, 5, 6 ];
79+
*
80+
* var out = withArray( x, 2, 8 );
81+
* // returns [ 1, 8, 3, 4, 5, 6 ]
82+
*/
83+
declare function withArray<T = unknown>( x: Collection<T>, index: number, value: T ): Array<T>;
8784

8885

8986
// EXPORTS //

0 commit comments

Comments
 (0)