Skip to content

Commit 0eb179b

Browse files
committed
refactor: update implementation and clean-up
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: na - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent 0707827 commit 0eb179b

5 files changed

Lines changed: 85 additions & 33 deletions

File tree

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,10 @@ The function has the following parameters:
6060
- **clbk**: callback function.
6161
- **thisArg**: callback execution context (_optional_).
6262

63-
The invoked callback is provided four arguments:
63+
The invoked callback is provided three arguments:
6464

65-
- **value**: array element.
66-
- **aidx**: array index.
67-
- **sidx**: strided index (`offset + aidx*stride`).
65+
- **value**: current array element.
66+
- **idx**: current array element index.
6867
- **array**: input ndarray.
6968

7069
To set the callback execution context, provide a `thisArg`.
@@ -99,6 +98,8 @@ var count = ctx.count;
9998
## Notes
10099

101100
- If provided an empty one-dimensional ndarray, the function returns `NaN`.
101+
- A provided callback function should return a numeric value.
102+
- If a provided callback function does not return any value (or equivalently, explicitly returns `undefined`), the value is **ignored**.
102103

103104
</section>
104105

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,17 @@
55

66
If provided an empty ndarray, the function returns `NaN`.
77

8+
The callback function is provided three arguments:
9+
10+
- value: current array element.
11+
- index: current array index.
12+
- array: the input ndarray.
13+
14+
The callback function should return a numeric value.
15+
16+
If the callback function does not return any value (or equivalently,
17+
explicitly returns `undefined`), the value is ignored.
18+
819
Parameters
920
----------
1021
arrays: ArrayLikeObject<ndarray>

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

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,63 +20,51 @@
2020

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

23-
import { ndarray, typedndarray } from '@stdlib/types/ndarray';
23+
import { typedndarray } from '@stdlib/types/ndarray';
2424

2525
/**
2626
* Returns the result of callback function.
2727
*
2828
* @returns result
2929
*/
30-
type Nullary<U> = ( this: U ) => number | void;
30+
type Nullary<ThisArg> = ( this: ThisArg ) => number | void;
3131

3232
/**
3333
* Returns the result of callback function.
3434
*
35-
* @param value - array element
35+
* @param value - current array element
3636
* @returns result
3737
*/
38-
type Unary<T, U> = ( this: U, value: T ) => number | void;
38+
type Unary<T, ThisArg> = ( this: ThisArg, value: T ) => number | void;
3939

4040
/**
4141
* Returns the result of callback function.
4242
*
43-
* @param value - array element
44-
* @param aidx - array index
43+
* @param value - current array element
44+
* @param index - current array element index
4545
* @returns result
4646
*/
47-
type Binary<T, U> = ( this: U, value: T, aidx: number ) => number | void;
47+
type Binary<T, ThisArg> = ( this: ThisArg, value: T, index: number ) => number | void;
4848

4949
/**
5050
* Returns the result of callback function.
5151
*
52-
* @param value - array element
53-
* @param aidx - array index
54-
* @param sidx - strided index (offset + aidx*stride)
55-
* @returns result
56-
*/
57-
type Ternary<T, U> = ( this: U, value: T, aidx: number, sidx: number ) => number | void;
58-
59-
/**
60-
* Returns the result of callback function.
61-
*
62-
* @param value - array element
63-
* @param aidx - array index
64-
* @param sidx - strided index (offset + aidx*stride)
52+
* @param value - current array element
53+
* @param index - current array element index
6554
* @param array - input ndarray
6655
* @returns result
6756
*/
68-
type Quaternary<T, U> = ( this: U, value: T, aidx: number, sidx: number, array: typedndarray<T> ) => number | void;
57+
type Ternary<T, U, ThisArg> = ( this: ThisArg, value: T, index: number, array: U ) => number | void;
6958

7059
/**
7160
* Returns the result of callback function.
7261
*
73-
* @param value - array element
74-
* @param aidx - array index
75-
* @param sidx - strided index (offset + aidx*stride)
62+
* @param value - current array element
63+
* @param index - current array element index
7664
* @param array - input ndarray
7765
* @returns result
7866
*/
79-
type Callback<T, U> = Nullary<U> | Unary<T, U> | Binary<T, U> | Ternary<T, U> | Quaternary<T, U>;
67+
type Callback<T, U, ThisArg> = Nullary<ThisArg> | Unary<T, ThisArg> | Binary<T, ThisArg> | Ternary<T, U, ThisArg>;
8068

8169
/**
8270
* Computes the maximum value of a one-dimensional ndarray via a callback function.
@@ -99,7 +87,7 @@ type Callback<T, U> = Nullary<U> | Unary<T, U> | Binary<T, U> | Ternary<T, U> |
9987
* var v = maxBy( [ x ], clbk );
10088
* // returns 8.0
10189
*/
102-
declare function maxBy<T = unknown, U = unknown, V extends ndarray = ndarray>( arrays: [ V ], clbk: Callback<T, U>, thisArg?: ThisParameterType<Callback<T, U>> ): number;
90+
declare function maxBy<T = unknown, U extends typedndarray<T> = typedndarray<T>, ThisArg = unknown>( arrays: [ U ], clbk: Callback<T, U, ThisArg>, thisArg?: ThisParameterType<Callback<T, U, ThisArg>> ): number;
10391

10492

10593
// EXPORTS //

lib/node_modules/@stdlib/stats/base/ndarray/max-by/lib/main.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,21 @@ var strided = require( '@stdlib/stats/base/max-by' ).ndarray;
5252
*/
5353
function maxBy( arrays, clbk, thisArg ) {
5454
var x = arrays[ 0 ];
55-
return strided( numelDimension( x, 0 ), getData( x ), getStride( x, 0 ), getOffset( x ), clbk, thisArg ); // eslint-disable-line max-len
55+
return strided( numelDimension( x, 0 ), getData( x ), getStride( x, 0 ), getOffset( x ), wrapper, null ); // eslint-disable-line max-len
56+
57+
/**
58+
* Invokes a provided callback.
59+
*
60+
* @private
61+
* @param {*} value - current array element
62+
* @param {NonNegativeInteger} aidx - current array element index
63+
* @param {NonNegativeInteger} sidx - current strided array element index
64+
* @param {Collection} arr - input array
65+
* @returns {*} result
66+
*/
67+
function wrapper( value, aidx ) {
68+
return clbk.call( thisArg, value, aidx, x );
69+
}
5670
}
5771

5872

lib/node_modules/@stdlib/stats/base/ndarray/max-by/test/test.js

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,12 @@ tape( 'the function supports one-dimensional ndarrays having non-zero offsets',
180180
});
181181

182182
tape( 'the function supports providing an execution context', function test( t ) {
183+
var expected;
184+
var indices;
185+
var values;
186+
var arrays;
183187
var ctx;
188+
var arr;
184189
var x;
185190
var v;
186191

@@ -198,14 +203,47 @@ tape( 'the function supports providing an execution context', function test( t )
198203
'count': 0
199204
};
200205

201-
v = maxBy( [ vector( x, 4, 2, 0 ) ], clbk, ctx );
206+
indices = [];
207+
values = [];
208+
arrays = [];
209+
210+
arr = vector( x, 4, 2, 0 );
211+
v = maxBy( [ arr ], clbk, ctx );
202212

203213
t.strictEqual( v, 8.0, 'returns expected value' );
204214
t.strictEqual( ctx.count, 4, 'returns expected value' );
215+
216+
expected = [
217+
1.0,
218+
2.0,
219+
-2.0,
220+
4.0
221+
];
222+
t.deepEqual( values, expected, 'returns expected value' );
223+
224+
expected = [
225+
0,
226+
1,
227+
2,
228+
3
229+
];
230+
t.deepEqual( indices, expected, 'returns expected value' );
231+
232+
expected = [
233+
arr,
234+
arr,
235+
arr,
236+
arr
237+
];
238+
t.deepEqual( arrays, expected, 'returns expected value' );
239+
205240
t.end();
206241

207-
function clbk( v ) {
242+
function clbk( v, idx, arr ) {
208243
this.count += 1; // eslint-disable-line no-invalid-this
244+
values.push( v );
245+
indices.push( idx );
246+
arrays.push( arr );
209247
return v * 2.0;
210248
}
211249
});

0 commit comments

Comments
 (0)