Skip to content

Commit 140bea7

Browse files
committed
refactor and add protocol support to stats/base/nanmax-by
1 parent 7ae0a0a commit 140bea7

File tree

12 files changed

+554
-129
lines changed

12 files changed

+554
-129
lines changed

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

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
@license Apache-2.0
44
5-
Copyright (c) 2020 The Stdlib Authors.
5+
Copyright (c) 2025 The Stdlib Authors.
66
77
Licensed under the Apache License, Version 2.0 (the "License");
88
you may not use this file except in compliance with the License.
@@ -30,7 +30,7 @@ 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

3535
Calculates the maximum value of strided array `x` via a callback function, ignoring `NaN` values.
3636

@@ -49,15 +49,15 @@ 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**: index increment.
5353
- **clbk**: callback function.
5454
- **thisArg**: execution context (_optional_).
5555

5656
The invoked callback is provided four arguments:
5757

5858
- **value**: array element.
5959
- **aidx**: array index.
60-
- **sidx**: strided index (`offset + aidx*stride`).
60+
- **sidx**: strided index (`offsetX + aidx*strideX`).
6161
- **array**: input array/collection.
6262

6363
To set the callback execution context, provide a `thisArg`.
@@ -81,7 +81,7 @@ 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 `strideX` parameters determine which elements in `x` are accessed at runtime. For example, to access every other element
8585

8686
```javascript
8787
var floor = require( '@stdlib/math/base/special/floor' );
@@ -119,7 +119,7 @@ var v = nanmaxBy( N, x1, 2, accessor );
119119
// returns -4.0
120120
```
121121

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

124124
Calculates the maximum value of strided array `x` via a callback function, ignoring `NaN` values and using alternative indexing semantics.
125125

@@ -136,9 +136,9 @@ var v = nanmaxBy.ndarray( x.length, x, 1, 0, accessor );
136136

137137
The function has the following additional parameters:
138138

139-
- **offset**: starting index.
139+
- **offsetX**: starting index.
140140

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`
141+
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying `buffer`, the `offsetX` parameter supports indexing semantics based on a starting index. For example, to access only the last three elements of `x`
142142

143143
```javascript
144144
function accessor( v ) {
@@ -160,6 +160,7 @@ var v = nanmaxBy.ndarray( 3, x, 1, x.length-3, accessor );
160160
## Notes
161161

162162
- If `N <= 0`, both functions return `NaN`.
163+
- 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]).
163164
- A provided callback function should return a numeric value.
164165
- If a provided callback function returns `NaN`, the value is ignored.
165166
- If a provided callback function does not return any value (or equivalently, explicitly returns `undefined`), the value is ignored.
@@ -176,23 +177,23 @@ var v = nanmaxBy.ndarray( 3, x, 1, x.length-3, accessor );
176177
<!-- eslint no-undef: "error" -->
177178

178179
```javascript
179-
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
180-
var randu = require( '@stdlib/random/base/randu' );
180+
var uniform = require( '@stdlib/random/base/uniform' );
181181
var filledarrayBy = require( '@stdlib/array/filled-by' );
182+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
182183
var nanmaxBy = require( '@stdlib/stats/base/nanmax-by' );
183184

184-
function fill() {
185-
if ( randu() < 0.2 ) {
185+
function rand() {
186+
if ( bernoulli( 0.8 )< 0.2 ) {
186187
return NaN;
187188
}
188-
return discreteUniform( -50, 50 );
189+
return uniform( -50, 50 );
189190
}
190191

191192
function accessor( v ) {
192193
return v * 2.0;
193194
}
194195

195-
var x = filledarrayBy( 10, 'float64', fill );
196+
var x = filledarrayBy( 10, 'float64', rand );
196197
console.log( x );
197198

198199
var v = nanmaxBy( x.length, x, 1, accessor );

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

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @license Apache-2.0
33
*
4-
* Copyright (c) 2020 The Stdlib Authors.
4+
* Copyright (c) 2025 The Stdlib Authors.
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.
@@ -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;
@@ -37,6 +39,12 @@ var nanmaxBy = require( './../lib/nanmax_by.js' );
3739
* @param {number} value - array element
3840
* @returns {number} accessed value
3941
*/
42+
function rand() {
43+
if( bernoulli( 0.8 ) < 1 ) {
44+
return NaN;
45+
}
46+
return uniform( -50.0, 50.0 );
47+
}
4048
function accessor( value ) {
4149
return value * 2.0;
4250
}
@@ -49,17 +57,7 @@ function accessor( value ) {
4957
* @returns {Function} benchmark function
5058
*/
5159
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-
}
60+
var x = filledarrayBy( len, "float64", rand );
6361
return benchmark;
6462

6563
function benchmark( b ) {

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

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @license Apache-2.0
33
*
4-
* Copyright (c) 2020 The Stdlib Authors.
4+
* Copyright (c) 2025 The Stdlib Authors.
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.
@@ -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;
@@ -37,6 +39,12 @@ var nanmaxBy = require( './../lib/ndarray.js' );
3739
* @param {number} value - array element
3840
* @returns {number} accessed value
3941
*/
42+
function rand() {
43+
if( bernoulli( 0.8 ) < 1 ) {
44+
return NaN;
45+
}
46+
return uniform( -50.0, 50.0 );
47+
}
4048
function accessor( value ) {
4149
return value * 2.0;
4250
}
@@ -49,17 +57,7 @@ function accessor( value ) {
4957
* @returns {Function} benchmark function
5058
*/
5159
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-
}
60+
var x = filledarrayBy( len, "float64", rand );
6361
return benchmark;
6462

6563
function benchmark( b ) {

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

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

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

6-
The `N` and `stride` parameters determine which elements in `x` are accessed
6+
The `N` and `strideX` parameters determine which elements in `x` are accessed
77
at runtime.
88

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

37-
stride: integer
37+
strideX: integer
3838
Index increment for `x`.
3939

4040
clbk: Function
@@ -69,12 +69,12 @@
6969
> {{alias}}( N, x1, 2, accessor )
7070
-4.0
7171

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

7676
While typed array views mandate a view offset based on the underlying
77-
buffer, the `offset` parameter supports indexing semantics based on a
77+
buffer, the `offsetX` parameter supports indexing semantics based on a
7878
starting index.
7979

8080
Parameters
@@ -86,10 +86,10 @@
8686
Input array/collection. If provided an object, the object must be array-
8787
like (excluding strings and functions).
8888

89-
stride: integer
89+
strideX: integer
9090
Index increment for `x`.
9191

92-
offset: integer
92+
offsetX: integer
9393
Starting index of `x`.
9494

9595
clbk: Function

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

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* @license Apache-2.0
33
*
4-
* Copyright (c) 2020 The Stdlib Authors.
4+
* Copyright (c) 2025 The Stdlib Authors.
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.
@@ -20,7 +20,11 @@
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+
* Input array.
26+
*/
27+
type InputArray = NumericArray | Collection<number> | AccessorArrayLike<number>;
2428

2529
/**
2630
* Returns an accessed value.
@@ -51,7 +55,7 @@ type Binary<T, U> = ( this: U, value: T, aidx: number ) => number | void;
5155
*
5256
* @param value - array element
5357
* @param aidx - array index
54-
* @param sidx - strided index (offset + aidx*stride)
58+
* @param sidx - strided index (offsetX + aidx*strideX)
5559
* @returns accessed value
5660
*/
5761
type Ternary<T, U> = ( this: U, value: T, aidx: number, sidx: number ) => number | void;
@@ -61,7 +65,7 @@ type Ternary<T, U> = ( this: U, value: T, aidx: number, sidx: number ) => number
6165
*
6266
* @param value - array element
6367
* @param aidx - array index
64-
* @param sidx - strided index (offset + aidx*stride)
68+
* @param sidx - strided index (offsetX + aidx*strideX)
6569
* @param array - input array
6670
* @returns accessed value
6771
*/
@@ -91,7 +95,7 @@ interface Routine {
9195
*
9296
* - `value`: array element
9397
* - `aidx`: array index
94-
* - `sidx`: strided index (offset + aidx*stride)
98+
* - `sidx`: strided index (offsetX + aidx*strideX)
9599
* - `array`: input array
96100
*
97101
* - The callback function should return a numeric value.
@@ -102,7 +106,7 @@ interface Routine {
102106
*
103107
* @param N - number of indexed elements
104108
* @param x - input array
105-
* @param stride - stride length
109+
* @param strideX - stride length
106110
* @param clbk - callback
107111
* @param thisArg - execution context
108112
* @returns maximum value
@@ -117,7 +121,7 @@ interface Routine {
117121
* var v = nanmaxBy( x.length, x, 1, accessor );
118122
* // returns 8.0
119123
*/
120-
<T = unknown, U = unknown>( N: number, x: Collection<T>, stride: number, clbk: Callback<T, U>, thisArg?: ThisParameterType<Callback<T, U>> ): number;
124+
<T = unknown, U = unknown>( N: number, x: InputArray, strideX: number, clbk: Callback<T, U>, thisArg?: ThisParameterType<Callback<T, U>> ): number;
121125

122126
/**
123127
* Calculates the maximum value of a strided array via a callback function, ignoring `NaN` values and using alternative indexing semantics.
@@ -128,7 +132,7 @@ interface Routine {
128132
*
129133
* - `value`: array element
130134
* - `aidx`: array index
131-
* - `sidx`: strided index (offset + aidx*stride)
135+
* - `sidx`: strided index (offsetX + aidx*strideX)
132136
* - `array`: input array
133137
*
134138
* - The callback function should return a numeric value.
@@ -139,8 +143,8 @@ interface Routine {
139143
*
140144
* @param N - number of indexed elements
141145
* @param x - input array
142-
* @param stride - stride length
143-
* @param offset - starting index
146+
* @param strideX - stride length
147+
* @param offsetX - starting index
144148
* @param clbk - callback
145149
* @param thisArg - execution context
146150
* @returns maximum value
@@ -155,7 +159,7 @@ interface Routine {
155159
* var v = nanmaxBy.ndarray( x.length, x, 1, 0, accessor );
156160
* // returns 8.0
157161
*/
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;
162+
ndarray<T = unknown, U = unknown>( N: number, x: InputArray, strideX: number, offsetX: number, clbk: Callback<T, U>, thisArg?: ThisParameterType<Callback<T, U>> ): number;
159163
}
160164

161165
/**
@@ -167,7 +171,7 @@ interface Routine {
167171
*
168172
* - `value`: array element
169173
* - `aidx`: array index
170-
* - `sidx`: strided index (offset + aidx*stride)
174+
* - `sidx`: strided index (offsetX + aidx*strideX)
171175
* - `array`: input array
172176
*
173177
* - The callback function should return a numeric value.
@@ -178,7 +182,7 @@ interface Routine {
178182
*
179183
* @param N - number of indexed elements
180184
* @param x - input array
181-
* @param stride - stride length
185+
* @param strideX - stride length
182186
* @param clbk - callback
183187
* @param thisArg - execution context
184188
* @returns maximum value

0 commit comments

Comments
 (0)