Skip to content

Commit e2d5087

Browse files
committed
feat: added accessor protocol to range-by
--- 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: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - 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: passed - task: lint_license_headers status: passed ---
1 parent 3c63b7b commit e2d5087

File tree

15 files changed

+320
-115
lines changed

15 files changed

+320
-115
lines changed

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

Lines changed: 6 additions & 6 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.
@@ -38,7 +38,7 @@ 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

4343
Calculates the [range][range] of strided array `x` via a callback function.
4444

@@ -56,8 +56,8 @@ var v = rangeBy( x.length, x, 1, accessor );
5656
The function has the following parameters:
5757

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

@@ -89,7 +89,7 @@ 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 `strideX` parameters determine which elements in `x` are accessed at runtime. For example, to access every other element
9393

9494
```javascript
9595
var floor = require( '@stdlib/math/base/special/floor' );
@@ -127,7 +127,7 @@ var v = rangeBy( N, x1, 2, accessor );
127127
// returns 8.0
128128
```
129129

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

132132
Calculates the [range][range] of strided array `x` via a callback function and using alternative indexing semantics.
133133

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

Lines changed: 11 additions & 10 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,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: 1 addition & 1 deletion
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.

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
2-
{{alias}}( N, x, stride, clbk[, thisArg] )
1+
{{alias}}( N, x, strideX, clbk[, thisArg] )
32
Calculates the range of a strided array via a callback function.
43

54
The `N` and `stride` parameters determine which elements in `x` are accessed
@@ -14,7 +13,7 @@
1413

1514
- value: array element.
1615
- aidx: array index.
17-
- sidx: strided index (offset + aidx*stride).
16+
- sidx: strided index (offsetX + aidx*strideX).
1817
- array: the input array.
1918

2019
The callback function should return a numeric value.
@@ -31,7 +30,7 @@
3130
Input array/collection. If provided an object, the object must be array-
3231
like (excluding strings and functions).
3332

34-
stride: integer
33+
strideX: integer
3534
Index increment for `x`.
3635

3736
clbk: Function
@@ -53,7 +52,7 @@
5352
> {{alias}}( x.length, x, 1, accessor )
5453
18.0
5554

56-
// Using `N` and `stride` parameters:
55+
// Using `N` and `strideX` parameters:
5756
> x = [ -2.0, 1.0, 3.0, -5.0, 4.0, -1.0, -3.0 ];
5857
> var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 );
5958
> {{alias}}( N, x, 2, accessor )
@@ -66,12 +65,13 @@
6665
> {{alias}}( N, x1, 2, accessor )
6766
8.0
6867

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

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

7777
Parameters
@@ -83,10 +83,10 @@
8383
Input array/collection. If provided an object, the object must be array-
8484
like (excluding strings and functions).
8585

86-
stride: integer
86+
strideX: integer
8787
Index increment for `x`.
8888

89-
offset: integer
89+
offsetX: integer
9090
Starting index of `x`.
9191

9292
clbk: Function

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

Lines changed: 12 additions & 5 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,14 @@
2020

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

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

2532
/**
2633
* Returns an accessed value.
@@ -65,7 +72,7 @@ type Ternary<T, U> = ( this: U, value: T, aidx: number, sidx: number ) => number
6572
* @param array - input array
6673
* @returns accessed value
6774
*/
68-
type Quaternary<T, U> = ( this: U, value: T, aidx: number, sidx: number, array: Collection<T> ) => number | void;
75+
type Quaternary<T, U> = ( this: U, value: T, aidx: number, sidx: number, array: InputArray ) => number | void;
6976

7077
/**
7178
* Returns an accessed value.
@@ -113,7 +120,7 @@ interface Routine {
113120
* var v = rangeBy( x.length, x, 1, accessor );
114121
* // returns 18.0
115122
*/
116-
<T = unknown, U = unknown>( N: number, x: Collection<T>, stride: number, clbk: Callback<T, U>, thisArg?: ThisParameterType<Callback<T, U>> ): number;
123+
<T = unknown, U = unknown>( N: number, x: InputArray, stride: number, clbk: Callback<T, U>, thisArg?: ThisParameterType<Callback<T, U>> ): number;
117124

118125
/**
119126
* Calculates the range of a strided array via a callback function and using alternative indexing semantics.
@@ -147,7 +154,7 @@ interface Routine {
147154
* var v = rangeBy.ndarray( x.length, x, 1, 0, accessor );
148155
* // returns 18.0
149156
*/
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;
157+
ndarray<T = unknown, U = unknown>( N: number, x: InputArray, stride: number, offset: number, clbk: Callback<T, U>, thisArg?: ThisParameterType<Callback<T, U>> ): number;
151158
}
152159

153160
/**

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

Lines changed: 8 additions & 3 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.
@@ -16,7 +16,8 @@
1616
* limitations under the License.
1717
*/
1818

19-
import rangeBy = require( './index' );
19+
import rangeBy = require( '@stdlib/stats/base/range-by' );
20+
import AccessorArray = require( '@stdlib/array/base/accessor' );
2021

2122
const accessor = (): number => {
2223
return 5.0;
@@ -31,6 +32,8 @@ const accessor = (): number => {
3132

3233
rangeBy( x.length, x, 1, accessor ); // $ExpectType number
3334
rangeBy( x.length, x, 1, accessor, {} ); // $ExpectType number
35+
rangeBy( x.length, new AccessorArray ( x ), 1, accessor ); // $ExpectType number
36+
rangeBy( x.length, new AccessorArray ( x ), 1, accessor, {} ); // $ExpectType number
3437
}
3538

3639
// The compiler throws an error if the function is provided a first argument which is not a number...
@@ -70,7 +73,7 @@ const accessor = (): number => {
7073
rangeBy( x.length, x, undefined, accessor ); // $ExpectError
7174
rangeBy( x.length, x, [], accessor ); // $ExpectError
7275
rangeBy( x.length, x, {}, accessor ); // $ExpectError
73-
rangeBy( x.length, x, ( x: number, accessor ): number => x, accessor ); // $ExpectError
76+
rangeBy( x.length, x, ( x: number ): number => x, accessor ); // $ExpectError
7477
}
7578

7679
// The compiler throws an error if the function is provided a fourth argument which is not a function...
@@ -103,6 +106,8 @@ const accessor = (): number => {
103106

104107
rangeBy.ndarray( x.length, x, 1, 0, accessor ); // $ExpectType number
105108
rangeBy.ndarray( x.length, x, 1, 0, accessor, {} ); // $ExpectType number
109+
rangeBy.ndarray( x.length, new AccessorArray ( x ), 1, 0, accessor ); // $ExpectType number
110+
rangeBy.ndarray( x.length, new AccessorArray ( x ), 1, 0, accessor, {} ); // $ExpectType number
106111
}
107112

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

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

Lines changed: 1 addition & 1 deletion
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.
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
24+
25+
26+
// MAIN //
27+
28+
/**
29+
* Calculates the range of a strided array via a callback function.
30+
*
31+
* @param {PositiveInteger} N - number of indexed elements
32+
* @param {NumericArray|Collection|AccessorArrayLike<number>} x - input array/collection
33+
* @param {integer} strideX - stride length for `x`
34+
* @param {NonNegativeInteger} offsetX - starting index
35+
* @param {Callback} clbk - callback
36+
* @param {*} [thisArg] - execution context
37+
* @returns {number} range
38+
*
39+
* @example
40+
* var arraylike2object = require( '@stdlib/array/base/arraylike2object' );
41+
*
42+
* var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ];
43+
*
44+
* function accessor( v ) {
45+
* if ( v === void 0 ) {
46+
* return;
47+
* }
48+
* return v * 2.0;
49+
* }
50+
*
51+
* var v = rangeBy( x.length, arraylike2object(x), 1, 0, accessor );
52+
* // returns 18.0
53+
*/
54+
function rangeBy( N, x, strideX, offsetX, clbk, thisArg) {
55+
var xbuf;
56+
var get;
57+
var max;
58+
var min;
59+
var ix;
60+
var v;
61+
var i;
62+
63+
xbuf = x.data;
64+
65+
get = x.accessors[ 0 ];
66+
67+
if ( N === 1 || strideX === 0 ) {
68+
v = clbk.call( thisArg, get(xbuf, offsetX), 0, 0, x );
69+
if ( v === void 0 || isnan( v ) ) {
70+
return NaN;
71+
}
72+
return 0.0;
73+
}
74+
75+
ix = offsetX;
76+
for ( i = 0; i < N; i++ ) {
77+
min = clbk.call( thisArg, get(xbuf, ix), i, ix, x );
78+
if ( min !== void 0 ) {
79+
break;
80+
}
81+
ix += strideX;
82+
}
83+
if ( i === N ) {
84+
return NaN;
85+
}
86+
max = min;
87+
i += 1;
88+
for ( i; i < N; i++ ) {
89+
ix += strideX;
90+
v = clbk.call( thisArg, get(xbuf, ix), i, ix, x );
91+
if ( v === void 0 ) {
92+
continue;
93+
}
94+
if ( isnan( v ) ) {
95+
return v;
96+
}
97+
if ( v < min ) {
98+
min = v;
99+
} else if ( v > max ) {
100+
max = v;
101+
}
102+
}
103+
return max - min;
104+
}
105+
106+
107+
// EXPORTS //
108+
109+
module.exports = rangeBy;

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

Lines changed: 1 addition & 1 deletion
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.

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

Lines changed: 1 addition & 1 deletion
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.

0 commit comments

Comments
 (0)