Skip to content

feat: added accessor protocol to range-by #5914

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions lib/node_modules/@stdlib/stats/base/range-by/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

@license Apache-2.0

Copyright (c) 2020 The Stdlib Authors.
Copyright (c) 2025 The Stdlib Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -38,7 +38,7 @@ The [**range**][range] is defined as the difference between the maximum and mini
var rangeBy = require( '@stdlib/stats/base/range-by' );
```

#### rangeBy( N, x, stride, clbk\[, thisArg] )
#### rangeBy( N, x, strideX, clbk\[, thisArg] )

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

Expand All @@ -57,7 +57,7 @@ The function has the following parameters:

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

Expand Down Expand Up @@ -89,7 +89,7 @@ var cnt = context.count;
// returns 8
```

The `N` and `stride` parameters determine which elements in `x` are accessed at runtime. For example, to access every other element
The `N` and `strideX` parameters determine which elements in `x` are accessed at runtime. For example, to access every other element

```javascript
var floor = require( '@stdlib/math/base/special/floor' );
Expand Down Expand Up @@ -127,7 +127,7 @@ var v = rangeBy( N, x1, 2, accessor );
// returns 8.0
```

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

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

Expand Down
21 changes: 11 additions & 10 deletions lib/node_modules/@stdlib/stats/base/range-by/benchmark/benchmark.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2020 The Stdlib Authors.
* Copyright (c) 2025 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -21,11 +21,18 @@
// MODULES //

var bench = require( '@stdlib/bench' );
var randu = require( '@stdlib/random/base/randu' );
var uniform = require( '@stdlib/random/array/uniform' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var pow = require( '@stdlib/math/base/special/pow' );
var pkg = require( './../package.json' ).name;
var rangeBy = require( './../lib/range_by.js' );
var rangeBy = require( './../lib/main.js' );


// VARIABLES //

var options = {
'dtype': 'generic'
};


// FUNCTIONS //
Expand All @@ -49,13 +56,7 @@ function accessor( value ) {
* @returns {Function} benchmark function
*/
function createBenchmark( len ) {
var x;
var i;

x = [];
for ( i = 0; i < len; i++ ) {
x.push( ( randu()*20.0 ) - 10.0 );
}
var x = uniform( len, -10, 10, options );
return benchmark;

function benchmark( b ) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2020 The Stdlib Authors.
* Copyright (c) 2025 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
18 changes: 9 additions & 9 deletions lib/node_modules/@stdlib/stats/base/range-by/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

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

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

- value: array element.
- aidx: array index.
- sidx: strided index (offset + aidx*stride).
- sidx: strided index (offsetX + aidx*strideX).
- array: the input array.

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

stride: integer
strideX: integer
Index increment for `x`.

clbk: Function
Expand All @@ -53,7 +52,7 @@
> {{alias}}( x.length, x, 1, accessor )
18.0

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

{{alias}}.ndarray( N, x, stride, offset, clbk[, thisArg] )

{{alias}}.ndarray( N, x, strideX, offsetX, clbk[, thisArg] )
Calculates the range of a strided array via a callback function and using
alternative indexing semantics.

While typed array views mandate a view offset based on the underlying
buffer, the `offset` parameter supports indexing semantics based on a
buffer, the `offsetX` parameter supports indexing semantics based on a
starting index.

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

stride: integer
strideX: integer
Index increment for `x`.

offset: integer
offsetX: integer
Starting index of `x`.

clbk: Function
Expand Down
17 changes: 12 additions & 5 deletions lib/node_modules/@stdlib/stats/base/range-by/docs/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* @license Apache-2.0
*
* Copyright (c) 2020 The Stdlib Authors.
* Copyright (c) 2025 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -20,7 +20,14 @@

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

import { Collection } from '@stdlib/types/array';
// import { Collection } from '@stdlib/types/array';
import { NumericArray, Collection, AccessorArrayLike } from '@stdlib/types/array';


/**
* Input array.
*/
type InputArray = NumericArray | Collection | AccessorArrayLike<number>;

/**
* Returns an accessed value.
Expand Down Expand Up @@ -65,7 +72,7 @@ type Ternary<T, U> = ( this: U, value: T, aidx: number, sidx: number ) => number
* @param array - input array
* @returns accessed value
*/
type Quaternary<T, U> = ( this: U, value: T, aidx: number, sidx: number, array: Collection<T> ) => number | void;
type Quaternary<T, U> = ( this: U, value: T, aidx: number, sidx: number, array: InputArray ) => number | void;

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

/**
* Calculates the range of a strided array via a callback function and using alternative indexing semantics.
Expand Down Expand Up @@ -147,7 +154,7 @@ interface Routine {
* var v = rangeBy.ndarray( x.length, x, 1, 0, accessor );
* // returns 18.0
*/
ndarray<T = unknown, U = unknown>( N: number, x: Collection<T>, stride: number, offset: number, clbk: Callback<T, U>, thisArg?: ThisParameterType<Callback<T, U>> ): number;
ndarray<T = unknown, U = unknown>( N: number, x: InputArray, stride: number, offset: number, clbk: Callback<T, U>, thisArg?: ThisParameterType<Callback<T, U>> ): number;
}

/**
Expand Down
11 changes: 8 additions & 3 deletions lib/node_modules/@stdlib/stats/base/range-by/docs/types/test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* @license Apache-2.0
*
* Copyright (c) 2020 The Stdlib Authors.
* Copyright (c) 2025 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,7 +16,8 @@
* limitations under the License.
*/

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

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

rangeBy( x.length, x, 1, accessor ); // $ExpectType number
rangeBy( x.length, x, 1, accessor, {} ); // $ExpectType number
rangeBy( x.length, new AccessorArray ( x ), 1, accessor ); // $ExpectType number
rangeBy( x.length, new AccessorArray ( x ), 1, accessor, {} ); // $ExpectType number
}

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

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

rangeBy.ndarray( x.length, x, 1, 0, accessor ); // $ExpectType number
rangeBy.ndarray( x.length, x, 1, 0, accessor, {} ); // $ExpectType number
rangeBy.ndarray( x.length, new AccessorArray ( x ), 1, 0, accessor ); // $ExpectType number
rangeBy.ndarray( x.length, new AccessorArray ( x ), 1, 0, accessor, {} ); // $ExpectType number
}

// The compiler throws an error if the `ndarray` method is provided a first argument which is not a number...
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2020 The Stdlib Authors.
* Copyright (c) 2025 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
109 changes: 109 additions & 0 deletions lib/node_modules/@stdlib/stats/base/range-by/lib/accessors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2025 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var isnan = require( '@stdlib/math/base/assert/is-nan' );


// MAIN //

/**
* Calculates the range of a strided array via a callback function.
*
* @param {PositiveInteger} N - number of indexed elements
* @param {NumericArray|Collection|AccessorArrayLike<number>} x - input array/collection
* @param {integer} strideX - stride length for `x`
* @param {NonNegativeInteger} offsetX - starting index
* @param {Callback} clbk - callback
* @param {*} [thisArg] - execution context
* @returns {number} range
*
* @example
* var arraylike2object = require( '@stdlib/array/base/arraylike2object' );
*
* var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ];
*
* function accessor( v ) {
* if ( v === void 0 ) {
* return;
* }
* return v * 2.0;
* }
*
* var v = rangeBy( x.length, arraylike2object(x), 1, 0, accessor );
* // returns 18.0
*/
function rangeBy( N, x, strideX, offsetX, clbk, thisArg) {
var xbuf;
var get;
var max;
var min;
var ix;
var v;
var i;

xbuf = x.data;

get = x.accessors[ 0 ];

if ( N === 1 || strideX === 0 ) {
v = clbk.call( thisArg, get(xbuf, offsetX), 0, 0, x );
if ( v === void 0 || isnan( v ) ) {
return NaN;
}
return 0.0;
}

ix = offsetX;
for ( i = 0; i < N; i++ ) {
min = clbk.call( thisArg, get(xbuf, ix), i, ix, x );
if ( min !== void 0 ) {
break;
}
ix += strideX;
}
if ( i === N ) {
return NaN;
}
max = min;
i += 1;
for ( i; i < N; i++ ) {
ix += strideX;
v = clbk.call( thisArg, get(xbuf, ix), i, ix, x );
if ( v === void 0 ) {
continue;
}
if ( isnan( v ) ) {
return v;
}
if ( v < min ) {
min = v;
} else if ( v > max ) {
max = v;
}
}
return max - min;
}


// EXPORTS //

module.exports = rangeBy;
2 changes: 1 addition & 1 deletion lib/node_modules/@stdlib/stats/base/range-by/lib/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2020 The Stdlib Authors.
* Copyright (c) 2025 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion lib/node_modules/@stdlib/stats/base/range-by/lib/main.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2020 The Stdlib Authors.
* Copyright (c) 2025 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Loading