Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
46 changes: 46 additions & 0 deletions lib/node_modules/@stdlib/array/fixed-endian-factory/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,52 @@ var count = context.count;
// returns 3;
```

<a name="method-reduce-right"></a>

#### TypedArray.prototype.reduceRight( reducerFn\[, initialValue] )

Applies a provided callback function to each element of the array, in reverse order, passing in the return value from the calculation on the following element and returning the accumulated result upon completion.

```javascript
function reducer( acc, v ) {
return ( acc && v );
}

var Float64ArrayFE = fixedEndianFactory( 'float64' );

var arr = new Float64ArrayFE( 'little-endian', [ 1.0, 0.0, 1.0 ] );
// returns <Float64ArrayFE>

var out = arr.reduceRight( reducer );
// returns 0.0
```

The reducer function is provided four arguments:

- **acc**: accumulated result.
- **value**: current array element.
- **index**: current array element index.
- **arr**: the array on which this method was called.

By default, the function initializes the accumulated result to the first element in the array and passes the second array element as `value` during the first invocation of the provided callback. To begin accumulation from a different starting value and pass in the first array element as `value` during the first invocation of the provided callback, provide an `initialValue` argument.

```javascript
function reducer( acc, v ) {
if ( v ) {
return acc + 1;
}
return acc;
}

var Float64ArrayFE = fixedEndianFactory( 'float64' );

var arr = new Float64ArrayFE( 'little-endian', [ 1.0, 0.0, 1.0 ] );
// returns <Float64ArrayFE>

var out = arr.reduceRight( reducer, 0 );
// returns 2
```

<a name="method-set"></a>

#### TypedArrayFE.prototype.set( arr\[, offset] )
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2024 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 bench = require( '@stdlib/bench' );
var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive;
var pkg = require( './../package.json' ).name;
var factory = require( './../lib' );


// VARIABLES //

var Float64ArrayFE = factory( 'float64' );


// FUNCTIONS //

/**
* Reducer function.
*
* @private
* @param {integer} acc - accumulated value
* @param {number} value - current array element
* @param {integer} index - current array index
* @returns {integer} accumulated value
*/
function reducer( acc, value ) {
if ( value ) {
return acc + 1;
}
return acc;
}


// MAIN //

bench( pkg+':reduceRight', function benchmark( b ) {
var out;
var arr;
var i;

arr = new Float64ArrayFE( 'little-endian', [ 1.0, 0.0, 0.0, 1.0 ] );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = arr.reduceRight( reducer, 0 );
if ( typeof out !== 'number' ) {
b.fail( 'should return a number' );
}
}
b.toc();
if ( !isInteger( out ) ) {
b.fail( 'should return an integer' );
}
b.pass( 'benchmark finished' );
b.end();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2024 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 bench = require( '@stdlib/bench' );
var pow = require( '@stdlib/math/base/special/pow' );
var zeroTo = require( '@stdlib/array/zero-to' );
var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive;
var pkg = require( './../package.json' ).name;
var factory = require( './../lib' );


// VARIABLES //

var Float64ArrayFE = factory( 'float64' );


// FUNCTIONS //

/**
* Reducer function.
*
* @private
* @param {integer} acc - accumulated value
* @param {number} value - current array element
* @param {integer} index - current array index
* @returns {integer} accumulated value
*/
function reducer( acc, value ) {
if ( value ) {
return acc + 1;
}
return acc;
}

/**
* Creates a benchmark function.
*
* @private
* @param {PositiveInteger} len - array length
* @returns {Function} benchmark function
*/
function createBenchmark( len ) {
var arr = new Float64ArrayFE( 'little-endian', zeroTo( len ) );
return benchmark;

/**
* Benchmark function.
*
* @private
* @param {Benchmark} b - benchmark instance
*/
function benchmark( b ) {
var out;
var i;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = arr.reduceRight( reducer );
if ( typeof out !== 'number' ) {
b.fail( 'should return a number' );
}
}
b.toc();
if ( !isInteger( out ) ) {
b.fail( 'should return an integer' );
}
b.pass( 'benchmark finished' );
b.end();
}
}


// MAIN //

/**
* Main execution sequence.
*
* @private
*/
function main() {
var len;
var min;
var max;
var f;
var i;

min = 1; // 10^min
max = 6; // 10^max

for ( i = min; i <= max; i++ ) {
len = pow( 10, i );
f = createBenchmark( len );
bench( pkg+':reduceRight:len='+len, f );
}
}

main();
42 changes: 42 additions & 0 deletions lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,48 @@ function factory( dtype ) { // eslint-disable-line max-lines-per-function, stdli
return out;
});

/**
* Applies a provided callback function to each element of the array, in reverse order, passing in the return value from the calculation on the preceding element and returning the accumulated result upon completion.
*
* @name reduceRight
* @memberof TypedArray.prototype
* @type {Function}
* @param {Function} reducer - callback function
* @param {*} [initialValue] - initial value
* @throws {TypeError} `this` must be a typed array
* @throws {Error} if not provided an initial value, the array must have at least one element
* @returns {*} accumulated result
*/
setReadOnly( TypedArray.prototype, 'reduceRight', function reduceRight( reducer, initialValue ) {
var buf;
var len;
var acc;
var i;

if ( !isTypedArray( this ) ) {
throw new TypeError( format( 'invalid invocation. `this` is not %s %s.', CHAR2ARTICLE[ dtype[0] ], CTOR_NAME ) );
}
if ( !isFunction( reducer ) ) {
throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', reducer ) );
}
buf = this._buffer;
len = this._length;
if ( arguments.length > 1 ) {
acc = initialValue;
i = len - 1;
} else {
if ( len === 0 ) {
throw new Error( 'invalid operation. If not provided an initial value, an array must contain at least one element.' );
}
acc = buf[ GETTER ]( ( len - 1 ) * BYTES_PER_ELEMENT, this._isLE );
i = len - 2;
}
for ( ; i >= 0; i-- ) {
acc = reducer( acc, buf[ GETTER ]( i * BYTES_PER_ELEMENT, this._isLE ), i, this );
}
return acc;
});

/**
* Sets an array element.
*
Expand Down
Loading