Skip to content

Commit d13caae

Browse files
Kaushikgtmkgrytestdlib-botgururaj1512
authored
feat: refactor and add protocol support to stats/base/nanmax-by
PR-URL: #6469 Closes: #5653 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 631ef8c commit d13caae

File tree

13 files changed

+538
-141
lines changed

13 files changed

+538
-141
lines changed

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

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ limitations under the License.
3030
var nanmaxBy = require( '@stdlib/stats/base/nanmax-by' );
3131
```
3232

33-
#### nanmaxBy( N, x, stride, clbk\[, thisArg] )
33+
#### nanmaxBy( N, x, strideX, clbk\[, thisArg] )
3434

35-
Calculates the maximum value of strided array `x` via a callback function, ignoring `NaN` values.
35+
Computes the maximum value of a strided array via a callback function, ignoring `NaN` values.
3636

3737
```javascript
3838
function accessor( v ) {
@@ -49,7 +49,7 @@ The function has the following parameters:
4949

5050
- **N**: number of indexed elements.
5151
- **x**: input [`Array`][mdn-array], [`typed array`][mdn-typed-array], or an array-like object (excluding strings and functions).
52-
- **stride**: index increment.
52+
- **strideX**: stride length.
5353
- **clbk**: callback function.
5454
- **thisArg**: execution context (_optional_).
5555

@@ -81,27 +81,23 @@ var cnt = context.count;
8181
// returns 10
8282
```
8383

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

8686
```javascript
87-
var floor = require( '@stdlib/math/base/special/floor' );
88-
8987
function accessor( v ) {
9088
return v * 2.0;
9189
}
9290

9391
var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0, NaN, NaN ];
94-
var N = floor( x.length / 2 );
9592

96-
var v = nanmaxBy( N, x, 2, accessor );
93+
var v = nanmaxBy( 5, x, 2, accessor );
9794
// returns 8.0
9895
```
9996

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

10299
```javascript
103100
var Float64Array = require( '@stdlib/array/float64' );
104-
var floor = require( '@stdlib/math/base/special/floor' );
105101

106102
function accessor( v ) {
107103
return v * 2.0;
@@ -112,16 +108,15 @@ var x0 = new Float64Array( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ] );
112108

113109
// Create an offset view...
114110
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
115-
var N = floor( x0.length/2 );
116111

117112
// Access every other element...
118-
var v = nanmaxBy( N, x1, 2, accessor );
113+
var v = nanmaxBy( 3, x1, 2, accessor );
119114
// returns -4.0
120115
```
121116

122-
#### nanmaxBy.ndarray( N, x, stride, offset, clbk\[, thisArg] )
117+
#### nanmaxBy.ndarray( N, x, strideX, offsetX, clbk\[, thisArg] )
123118

124-
Calculates the maximum value of strided array `x` via a callback function, ignoring `NaN` values and using alternative indexing semantics.
119+
Computes the maximum value of a strided array via a callback function, ignoring `NaN` values and using alternative indexing semantics.
125120

126121
```javascript
127122
function accessor( v ) {
@@ -136,9 +131,9 @@ var v = nanmaxBy.ndarray( x.length, x, 1, 0, accessor );
136131

137132
The function has the following additional parameters:
138133

139-
- **offset**: starting index.
134+
- **offsetX**: starting index.
140135

141-
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`
136+
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`
142137

143138
```javascript
144139
function accessor( v ) {
@@ -164,6 +159,7 @@ var v = nanmaxBy.ndarray( 3, x, 1, x.length-3, accessor );
164159
- If a provided callback function returns `NaN`, the value is ignored.
165160
- If a provided callback function does not return any value (or equivalently, explicitly returns `undefined`), the value is ignored.
166161
- When possible, prefer using [`dnanmax`][@stdlib/stats/strided/dnanmax], [`snanmax`][@stdlib/stats/strided/snanmax], and/or [`nanmax`][@stdlib/stats/base/nanmax], as, depending on the environment, these interfaces are likely to be significantly more performant.
162+
- 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]).
167163

168164
</section>
169165

@@ -176,23 +172,23 @@ var v = nanmaxBy.ndarray( 3, x, 1, x.length-3, accessor );
176172
<!-- eslint no-undef: "error" -->
177173

178174
```javascript
179-
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
180-
var randu = require( '@stdlib/random/base/randu' );
175+
var uniform = require( '@stdlib/random/base/uniform' );
181176
var filledarrayBy = require( '@stdlib/array/filled-by' );
177+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
182178
var nanmaxBy = require( '@stdlib/stats/base/nanmax-by' );
183179

184-
function fill() {
185-
if ( randu() < 0.2 ) {
180+
function rand() {
181+
if ( bernoulli( 0.8 )< 0.2 ) {
186182
return NaN;
187183
}
188-
return discreteUniform( -50, 50 );
184+
return uniform( -50, 50 );
189185
}
190186

191187
function accessor( v ) {
192188
return v * 2.0;
193189
}
194190

195-
var x = filledarrayBy( 10, 'float64', fill );
191+
var x = filledarrayBy( 10, 'float64', rand );
196192
console.log( x );
197193

198194
var v = nanmaxBy( x.length, x, 1, accessor );
@@ -229,6 +225,8 @@ console.log( v );
229225

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

228+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
229+
232230
<!-- <related-links> -->
233231

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

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

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

2323
var bench = require( '@stdlib/bench' );
24-
var randu = require( '@stdlib/random/base/randu' );
24+
var uniform = require( '@stdlib/random/base/uniform' );
25+
var filledarrayBy = require( '@stdlib/array/filled-by' );
26+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
2527
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2628
var pow = require( '@stdlib/math/base/special/pow' );
2729
var pkg = require( './../package.json' ).name;
28-
var nanmaxBy = require( './../lib/nanmax_by.js' );
30+
var nanmaxBy = require( './../lib/main.js' );
2931

3032

3133
// FUNCTIONS //
@@ -41,6 +43,19 @@ function accessor( value ) {
4143
return value * 2.0;
4244
}
4345

46+
/**
47+
* Returns a random number.
48+
*
49+
* @private
50+
* @returns {number} random number
51+
*/
52+
function rand() {
53+
if ( bernoulli( 0.8 ) < 1 ) {
54+
return NaN;
55+
}
56+
return uniform( -50.0, 50.0 );
57+
}
58+
4459
/**
4560
* Create a benchmark function.
4661
*
@@ -49,17 +64,7 @@ function accessor( value ) {
4964
* @returns {Function} benchmark function
5065
*/
5166
function createBenchmark( len ) {
52-
var x;
53-
var i;
54-
55-
x = [];
56-
for ( i = 0; i < len; i++ ) {
57-
if ( randu() < 0.2 ) {
58-
x.push( NaN );
59-
} else {
60-
x.push( ( randu()*20.0 ) - 10.0 );
61-
}
62-
}
67+
var x = filledarrayBy( len, 'generic', rand );
6368
return benchmark;
6469

6570
function benchmark( b ) {

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

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
// MODULES //
2222

2323
var bench = require( '@stdlib/bench' );
24-
var randu = require( '@stdlib/random/base/randu' );
24+
var uniform = require( '@stdlib/random/base/uniform' );
25+
var filledarrayBy = require( '@stdlib/array/filled-by' );
26+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
2527
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2628
var pow = require( '@stdlib/math/base/special/pow' );
2729
var pkg = require( './../package.json' ).name;
@@ -41,6 +43,19 @@ function accessor( value ) {
4143
return value * 2.0;
4244
}
4345

46+
/**
47+
* Returns a random number.
48+
*
49+
* @private
50+
* @returns {number} random number
51+
*/
52+
function rand() {
53+
if ( bernoulli( 0.8 ) < 1 ) {
54+
return NaN;
55+
}
56+
return uniform( -50.0, 50.0 );
57+
}
58+
4459
/**
4560
* Create a benchmark function.
4661
*
@@ -49,17 +64,7 @@ function accessor( value ) {
4964
* @returns {Function} benchmark function
5065
*/
5166
function createBenchmark( len ) {
52-
var x;
53-
var i;
54-
55-
x = [];
56-
for ( i = 0; i < len; i++ ) {
57-
if ( randu() < 0.2 ) {
58-
x.push( NaN );
59-
} else {
60-
x.push( ( randu()*20.0 ) - 10.0 );
61-
}
62-
}
67+
var x = filledarrayBy( len, 'generic', rand );
6368
return benchmark;
6469

6570
function benchmark( b ) {

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

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

2-
{{alias}}( N, x, stride, clbk[, thisArg] )
3-
Calculates the maximum value of a strided array via a callback function,
2+
{{alias}}( N, x, strideX, clbk[, thisArg] )
3+
Computes the maximum value of strided array via a callback function,
44
ignoring `NaN` values.
55

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

99
Indexing is relative to the first index. To introduce an offset, use typed
1010
array views.
@@ -34,8 +34,8 @@
3434
Input array/collection. If provided an object, the object must be array-
3535
like (excluding strings and functions).
3636

37-
stride: integer
38-
Index increment for `x`.
37+
strideX: integer
38+
Stride length.
3939

4040
clbk: Function
4141
Callback function.
@@ -56,25 +56,24 @@
5656
> {{alias}}( x.length, x, 1, accessor )
5757
8.0
5858

59-
// Using `N` and `stride` parameters:
59+
// Using `N` and stride parameters:
6060
> x = [ -2.0, 1.0, 3.0, -5.0, 4.0, -1.0, -3.0 ];
61-
> var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 );
62-
> {{alias}}( N, x, 2, accessor )
61+
> {{alias}}( 3, x, 2, accessor )
6362
8.0
6463

6564
// Using view offsets:
6665
> var x0 = new {{alias:@stdlib/array/float64}}( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ] );
6766
> var x1 = new {{alias:@stdlib/array/float64}}( x0.buffer, x0.BYTES_PER_ELEMENT*1 );
68-
> N = {{alias:@stdlib/math/base/special/floor}}( x0.length / 2 );
69-
> {{alias}}( N, x1, 2, accessor )
67+
> {{alias}}( 3, x1, 2, accessor )
7068
-4.0
7169

72-
{{alias}}.ndarray( N, x, stride, offset, clbk[, thisArg] )
73-
Calculates the maximum value of a strided array via a callback function,
70+
71+
{{alias}}.ndarray( N, x, strideX, offsetX, clbk[, thisArg] )
72+
Computes the maximum value of a strided array via a callback function,
7473
ignoring `NaN` values and using alternative indexing semantics.
7574

7675
While typed array views mandate a view offset based on the underlying
77-
buffer, the `offset` parameter supports indexing semantics based on a
76+
buffer, the offset parameter supports indexing semantics based on a
7877
starting index.
7978

8079
Parameters
@@ -86,11 +85,11 @@
8685
Input array/collection. If provided an object, the object must be array-
8786
like (excluding strings and functions).
8887

89-
stride: integer
90-
Index increment for `x`.
88+
strideX: integer
89+
Stride length.
9190

92-
offset: integer
93-
Starting index of `x`.
91+
offsetX: integer
92+
Starting index.
9493

9594
clbk: Function
9695
Callback function.
@@ -113,8 +112,7 @@
113112

114113
// Using an index offset:
115114
> x = [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ];
116-
> var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 );
117-
> {{alias}}.ndarray( N, x, 2, 1, accessor )
115+
> {{alias}}.ndarray( 3, x, 2, 1, accessor )
118116
-4.0
119117

120118
See Also

lib/node_modules/@stdlib/stats/base/nanmax-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 { NumericArray, Collection, AccessorArrayLike } from '@stdlib/types/array';
24+
25+
/**
26+
* Input array.
27+
*/
28+
type InputArray = NumericArray | Collection<number> | AccessorArrayLike<number>;
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 maximum value of a strided array via a callback function, ignoring `NaN` values.
91+
* Computes the maximum value of a strided array via a callback function, ignoring `NaN` values.
8792
*
8893
* ## Notes
8994
*
@@ -102,7 +107,7 @@ interface Routine {
102107
*
103108
* @param N - number of indexed elements
104109
* @param x - input array
105-
* @param stride - stride length
110+
* @param strideX - stride length
106111
* @param clbk - callback
107112
* @param thisArg - execution context
108113
* @returns maximum value
@@ -117,10 +122,10 @@ interface Routine {
117122
* var v = nanmaxBy( x.length, x, 1, accessor );
118123
* // returns 8.0
119124
*/
120-
<T = unknown, U = unknown>( N: number, x: Collection<T>, stride: number, clbk: Callback<T, U>, thisArg?: ThisParameterType<Callback<T, U>> ): number;
125+
<T = unknown, U = unknown>( N: number, x: InputArray, strideX: number, clbk: Callback<T, U>, thisArg?: ThisParameterType<Callback<T, U>> ): number;
121126

122127
/**
123-
* Calculates the maximum value of a strided array via a callback function, ignoring `NaN` values and using alternative indexing semantics.
128+
* Computes the maximum value of a strided array via a callback function, ignoring `NaN` values and using alternative indexing semantics.
124129
*
125130
* ## Notes
126131
*
@@ -139,8 +144,8 @@ interface Routine {
139144
*
140145
* @param N - number of indexed elements
141146
* @param x - input array
142-
* @param stride - stride length
143-
* @param offset - starting index
147+
* @param strideX - stride length
148+
* @param offsetX - starting index
144149
* @param clbk - callback
145150
* @param thisArg - execution context
146151
* @returns maximum value
@@ -155,11 +160,11 @@ interface Routine {
155160
* var v = nanmaxBy.ndarray( x.length, x, 1, 0, accessor );
156161
* // returns 8.0
157162
*/
158-
ndarray<T = unknown, U = unknown>( N: number, x: Collection<T>, stride: number, offset: number, clbk: Callback<T, U>, thisArg?: ThisParameterType<Callback<T, U>> ): number;
163+
ndarray<T = unknown, U = unknown>( N: number, x: InputArray, strideX: number, offsetX: number, clbk: Callback<T, U>, thisArg?: ThisParameterType<Callback<T, U>> ): number;
159164
}
160165

161166
/**
162-
* Calculates the maximum value of a strided array via a callback function, ignoring `NaN` values.
167+
* Computes the maximum value of a strided array via a callback function, ignoring `NaN` values.
163168
*
164169
* ## Notes
165170
*
@@ -178,7 +183,7 @@ interface Routine {
178183
*
179184
* @param N - number of indexed elements
180185
* @param x - input array
181-
* @param stride - stride length
186+
* @param strideX - stride length
182187
* @param clbk - callback
183188
* @param thisArg - execution context
184189
* @returns maximum value

0 commit comments

Comments
 (0)