Skip to content

Commit 34e5e27

Browse files
dhruvilmehtakgrytestdlib-botgururaj1512
authored
feat: add accessor protocol support to stats/base/range-by
PR-URL: #5914 Closes: #5680 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Co-authored-by: stdlib-bot <[email protected]> Co-authored-by: Gururaj Gurram <[email protected]>
1 parent 643a522 commit 34e5e27

File tree

13 files changed

+508
-129
lines changed

13 files changed

+508
-129
lines changed

lib/node_modules/@stdlib/stats/base/range-by/README.md

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ The [**range**][range] is defined as the difference between the maximum and mini
3838
var rangeBy = require( '@stdlib/stats/base/range-by' );
3939
```
4040

41-
#### rangeBy( N, x, stride, clbk\[, thisArg] )
41+
#### rangeBy( N, x, strideX, clbk\[, thisArg] )
4242

43-
Calculates the [range][range] of strided array `x` via a callback function.
43+
Computes the [range][range] of a strided array via a callback function.
4444

4545
```javascript
4646
function accessor( v ) {
@@ -57,7 +57,7 @@ The function has the following parameters:
5757

5858
- **N**: number of indexed elements.
5959
- **x**: input [`Array`][mdn-array], [`typed array`][mdn-typed-array], or an array-like object (excluding strings and functions).
60-
- **stride**: index increment.
60+
- **strideX**: stride length.
6161
- **clbk**: callback function.
6262
- **thisArg**: execution context (_optional_).
6363

@@ -89,27 +89,23 @@ var cnt = context.count;
8989
// returns 8
9090
```
9191

92-
The `N` and `stride` parameters determine which elements in `x` are accessed at runtime. For example, to access every other element
92+
The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to access every other element
9393

9494
```javascript
95-
var floor = require( '@stdlib/math/base/special/floor' );
96-
9795
function accessor( v ) {
9896
return v * 2.0;
9997
}
10098

10199
var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ];
102-
var N = floor( x.length / 2 );
103100

104-
var v = rangeBy( N, x, 2, accessor );
101+
var v = rangeBy( 4, x, 2, accessor );
105102
// returns 12.0
106103
```
107104

108105
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
109106

110107
```javascript
111108
var Float64Array = require( '@stdlib/array/float64' );
112-
var floor = require( '@stdlib/math/base/special/floor' );
113109

114110
function accessor( v ) {
115111
return v * 2.0;
@@ -120,16 +116,15 @@ var x0 = new Float64Array( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ] );
120116

121117
// Create an offset view...
122118
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
123-
var N = floor( x0.length/2 );
124119

125120
// Access every other element...
126-
var v = rangeBy( N, x1, 2, accessor );
121+
var v = rangeBy( 3, x1, 2, accessor );
127122
// returns 8.0
128123
```
129124

130-
#### rangeBy.ndarray( N, x, stride, offset, clbk\[, thisArg] )
125+
#### rangeBy.ndarray( N, x, strideX, offsetX, clbk\[, thisArg] )
131126

132-
Calculates the [range][range] of strided array `x` via a callback function and using alternative indexing semantics.
127+
Computes the [range][range] of a strided array via a callback function and using alternative indexing semantics.
133128

134129
```javascript
135130
function accessor( v ) {
@@ -144,9 +139,9 @@ var v = rangeBy.ndarray( x.length, x, 1, 0, accessor );
144139

145140
The function has the following additional parameters:
146141

147-
- **offset**: starting index.
142+
- **offsetX**: starting index.
148143

149-
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying `buffer`, the `offset` parameter supports indexing semantics based on a starting index. For example, to access only the last three elements of `x`
144+
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameter supports indexing semantics based on a starting index. For example, to access only the last three elements of `x`
150145

151146
```javascript
152147
function accessor( v ) {
@@ -170,6 +165,7 @@ var v = rangeBy.ndarray( 3, x, 1, x.length-3, accessor );
170165
- If `N <= 0`, both functions return `NaN`.
171166
- A provided callback function should return a numeric value.
172167
- If a provided callback function does not return any value (or equivalently, explicitly returns `undefined`), the value is **ignored**.
168+
- Both functions support array-like objects having getter and setter accessors for array element access (e.g., [`@stdlib/array/base/accessor`][@stdlib/array/base/accessor]).
173169
- When possible, prefer using [`drange`][@stdlib/stats/strided/drange], [`srange`][@stdlib/stats/strided/srange], and/or [`range`][@stdlib/stats/base/range], as, depending on the environment, these interfaces are likely to be significantly more performant.
174170

175171
</section>
@@ -183,15 +179,16 @@ var v = rangeBy.ndarray( 3, x, 1, x.length-3, accessor );
183179
<!-- eslint no-undef: "error" -->
184180

185181
```javascript
186-
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory;
187-
var filledarrayBy = require( '@stdlib/array/filled-by' );
182+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
188183
var rangeBy = require( '@stdlib/stats/base/range-by' );
189184

190185
function accessor( v ) {
191186
return v * 2.0;
192187
}
193188

194-
var x = filledarrayBy( 10, 'float64', discreteUniform( -50, 50 ) );
189+
var x = discreteUniform( 10, -50, 50, {
190+
'dtype': 'float64'
191+
});
195192
console.log( x );
196193

197194
var v = rangeBy( x.length, x, 1, accessor );
@@ -231,6 +228,8 @@ console.log( v );
231228

232229
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
233230

231+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
232+
234233
<!-- <related-links> -->
235234

236235
[@stdlib/stats/strided/drange]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/drange

lib/node_modules/@stdlib/stats/base/range-by/benchmark/benchmark.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,18 @@
2121
// MODULES //
2222

2323
var bench = require( '@stdlib/bench' );
24-
var randu = require( '@stdlib/random/base/randu' );
24+
var uniform = require( '@stdlib/random/array/uniform' );
2525
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2626
var pow = require( '@stdlib/math/base/special/pow' );
2727
var pkg = require( './../package.json' ).name;
28-
var rangeBy = require( './../lib/range_by.js' );
28+
var rangeBy = require( './../lib/main.js' );
29+
30+
31+
// VARIABLES //
32+
33+
var options = {
34+
'dtype': 'generic'
35+
};
2936

3037

3138
// FUNCTIONS //
@@ -49,13 +56,7 @@ function accessor( value ) {
4956
* @returns {Function} benchmark function
5057
*/
5158
function createBenchmark( len ) {
52-
var x;
53-
var i;
54-
55-
x = [];
56-
for ( i = 0; i < len; i++ ) {
57-
x.push( ( randu()*20.0 ) - 10.0 );
58-
}
59+
var x = uniform( len, -10, 10, options );
5960
return benchmark;
6061

6162
function benchmark( b ) {

lib/node_modules/@stdlib/stats/base/range-by/benchmark/benchmark.ndarray.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,20 @@
2121
// MODULES //
2222

2323
var bench = require( '@stdlib/bench' );
24-
var randu = require( '@stdlib/random/base/randu' );
24+
var uniform = require( '@stdlib/random/array/uniform' );
2525
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2626
var pow = require( '@stdlib/math/base/special/pow' );
2727
var pkg = require( './../package.json' ).name;
2828
var rangeBy = require( './../lib/ndarray.js' );
2929

3030

31+
// VARIABLES //
32+
33+
var options = {
34+
'dtype': 'generic'
35+
};
36+
37+
3138
// FUNCTIONS //
3239

3340
/**
@@ -49,13 +56,7 @@ function accessor( value ) {
4956
* @returns {Function} benchmark function
5057
*/
5158
function createBenchmark( len ) {
52-
var x;
53-
var i;
54-
55-
x = [];
56-
for ( i = 0; i < len; i++ ) {
57-
x.push( ( randu()*20.0 ) - 10.0 );
58-
}
59+
var x = uniform( len, -10, 10, options );
5960
return benchmark;
6061

6162
function benchmark( b ) {

lib/node_modules/@stdlib/stats/base/range-by/docs/repl.txt

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11

2-
{{alias}}( N, x, stride, clbk[, thisArg] )
3-
Calculates the range of a strided array via a callback function.
2+
{{alias}}( N, x, strideX, clbk[, thisArg] )
3+
Computes the range of a strided array via a callback function.
44

5-
The `N` and `stride` parameters determine which elements in `x` are accessed
6-
at runtime.
5+
The `N` and stride parameters determine which elements in the strided array
6+
are accessed at runtime.
77

88
Indexing is relative to the first index. To introduce an offset, use typed
99
array views.
@@ -31,8 +31,8 @@
3131
Input array/collection. If provided an object, the object must be array-
3232
like (excluding strings and functions).
3333

34-
stride: integer
35-
Index increment for `x`.
34+
strideX: integer
35+
Stride length.
3636

3737
clbk: Function
3838
Callback function.
@@ -53,25 +53,24 @@
5353
> {{alias}}( x.length, x, 1, accessor )
5454
18.0
5555

56-
// Using `N` and `stride` parameters:
56+
// Using `N` and stride parameters:
5757
> x = [ -2.0, 1.0, 3.0, -5.0, 4.0, -1.0, -3.0 ];
58-
> var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 );
59-
> {{alias}}( N, x, 2, accessor )
58+
> {{alias}}( 3, x, 2, accessor )
6059
12.0
6160

6261
// Using view offsets:
6362
> var x0 = new {{alias:@stdlib/array/float64}}( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ] );
6463
> var x1 = new {{alias:@stdlib/array/float64}}( x0.buffer, x0.BYTES_PER_ELEMENT*1 );
65-
> N = {{alias:@stdlib/math/base/special/floor}}( x0.length / 2 );
66-
> {{alias}}( N, x1, 2, accessor )
64+
> {{alias}}( 3, x1, 2, accessor )
6765
8.0
6866

69-
{{alias}}.ndarray( N, x, stride, offset, clbk[, thisArg] )
67+
68+
{{alias}}.ndarray( N, x, strideX, offsetX, clbk[, thisArg] )
7069
Calculates the range of a strided array via a callback function and using
7170
alternative indexing semantics.
7271

7372
While typed array views mandate a view offset based on the underlying
74-
buffer, the `offset` parameter supports indexing semantics based on a
73+
buffer, the offset parameter supports indexing semantics based on a
7574
starting index.
7675

7776
Parameters
@@ -83,11 +82,11 @@
8382
Input array/collection. If provided an object, the object must be array-
8483
like (excluding strings and functions).
8584

86-
stride: integer
87-
Index increment for `x`.
85+
strideX: integer
86+
Stride length.
8887

89-
offset: integer
90-
Starting index of `x`.
88+
offsetX: integer
89+
Starting index.
9190

9291
clbk: Function
9392
Callback function.
@@ -110,8 +109,7 @@
110109

111110
// Using an index offset:
112111
> x = [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ];
113-
> var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 );
114-
> {{alias}}.ndarray( N, x, 2, 1, accessor )
112+
> {{alias}}.ndarray( 3, x, 2, 1, accessor )
115113
8.0
116114

117115
See Also

lib/node_modules/@stdlib/stats/base/range-by/docs/types/index.d.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@
2020

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

23-
import { Collection } from '@stdlib/types/array';
23+
import { Collection, AccessorArrayLike } from '@stdlib/types/array';
24+
25+
/**
26+
* Input array.
27+
*/
28+
type InputArray<T> = Collection<T> | AccessorArrayLike<T>;
2429

2530
/**
2631
* Returns an accessed value.
@@ -83,7 +88,7 @@ type Callback<T, U> = Nullary<U> | Unary<T, U> | Binary<T, U> | Ternary<T, U> |
8388
*/
8489
interface Routine {
8590
/**
86-
* Calculates the range of a strided array via a callback function.
91+
* Computes the range of a strided array via a callback function.
8792
*
8893
* ## Notes
8994
*
@@ -98,7 +103,7 @@ interface Routine {
98103
*
99104
* @param N - number of indexed elements
100105
* @param x - input array
101-
* @param stride - stride length
106+
* @param strideX - stride length
102107
* @param clbk - callback
103108
* @param thisArg - execution context
104109
* @returns range
@@ -113,10 +118,10 @@ interface Routine {
113118
* var v = rangeBy( x.length, x, 1, accessor );
114119
* // returns 18.0
115120
*/
116-
<T = unknown, U = unknown>( N: number, x: Collection<T>, stride: number, clbk: Callback<T, U>, thisArg?: ThisParameterType<Callback<T, U>> ): number;
121+
<T = unknown, U = unknown>( N: number, x: InputArray<T>, strideX: number, clbk: Callback<T, U>, thisArg?: ThisParameterType<Callback<T, U>> ): number;
117122

118123
/**
119-
* Calculates the range of a strided array via a callback function and using alternative indexing semantics.
124+
* Computes the range of a strided array via a callback function and using alternative indexing semantics.
120125
*
121126
* ## Notes
122127
*
@@ -131,8 +136,8 @@ interface Routine {
131136
*
132137
* @param N - number of indexed elements
133138
* @param x - input array
134-
* @param stride - stride length
135-
* @param offset - starting index
139+
* @param strideX - stride length
140+
* @param offsetX - starting index
136141
* @param clbk - callback
137142
* @param thisArg - execution context
138143
* @returns range
@@ -147,11 +152,11 @@ interface Routine {
147152
* var v = rangeBy.ndarray( x.length, x, 1, 0, accessor );
148153
* // returns 18.0
149154
*/
150-
ndarray<T = unknown, U = unknown>( N: number, x: Collection<T>, stride: number, offset: number, clbk: Callback<T, U>, thisArg?: ThisParameterType<Callback<T, U>> ): number;
155+
ndarray<T = unknown, U = unknown>( N: number, x: InputArray<T>, strideX: number, offsetX: number, clbk: Callback<T, U>, thisArg?: ThisParameterType<Callback<T, U>> ): number;
151156
}
152157

153158
/**
154-
* Calculates the range of a strided array via a callback function.
159+
* Computes the range of a strided array via a callback function.
155160
*
156161
* ## Notes
157162
*
@@ -166,7 +171,7 @@ interface Routine {
166171
*
167172
* @param N - number of indexed elements
168173
* @param x - input array
169-
* @param stride - stride length
174+
* @param strideX - stride length
170175
* @param clbk - callback
171176
* @param thisArg - execution context
172177
* @returns range

lib/node_modules/@stdlib/stats/base/range-by/docs/types/test.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* limitations under the License.
1717
*/
1818

19+
import AccessorArray = require( '@stdlib/array/base/accessor' );
1920
import rangeBy = require( './index' );
2021

2122
const accessor = (): number => {
@@ -30,7 +31,10 @@ const accessor = (): number => {
3031
const x = new Float64Array( 10 );
3132

3233
rangeBy( x.length, x, 1, accessor ); // $ExpectType number
34+
rangeBy( x.length, new AccessorArray ( x ), 1, accessor ); // $ExpectType number
35+
3336
rangeBy( x.length, x, 1, accessor, {} ); // $ExpectType number
37+
rangeBy( x.length, new AccessorArray ( x ), 1, accessor, {} ); // $ExpectType number
3438
}
3539

3640
// The compiler throws an error if the function is provided a first argument which is not a number...
@@ -70,7 +74,7 @@ const accessor = (): number => {
7074
rangeBy( x.length, x, undefined, accessor ); // $ExpectError
7175
rangeBy( x.length, x, [], accessor ); // $ExpectError
7276
rangeBy( x.length, x, {}, accessor ); // $ExpectError
73-
rangeBy( x.length, x, ( x: number, accessor ): number => x, accessor ); // $ExpectError
77+
rangeBy( x.length, x, ( x: number ): number => x, accessor ); // $ExpectError
7478
}
7579

7680
// The compiler throws an error if the function is provided a fourth argument which is not a function...
@@ -102,7 +106,10 @@ const accessor = (): number => {
102106
const x = new Float64Array( 10 );
103107

104108
rangeBy.ndarray( x.length, x, 1, 0, accessor ); // $ExpectType number
109+
rangeBy.ndarray( x.length, new AccessorArray ( x ), 1, 0, accessor ); // $ExpectType number
110+
105111
rangeBy.ndarray( x.length, x, 1, 0, accessor, {} ); // $ExpectType number
112+
rangeBy.ndarray( x.length, new AccessorArray ( x ), 1, 0, accessor, {} ); // $ExpectType number
106113
}
107114

108115
// The compiler throws an error if the `ndarray` method is provided a first argument which is not a number...

0 commit comments

Comments
 (0)