From 47598add01ec5c38bc8ad3704757dae50ebf92d3 Mon Sep 17 00:00:00 2001 From: aayush0325 Date: Sat, 30 Nov 2024 08:57:13 +0530 Subject: [PATCH 1/4] feat: logic and tests added --- .../array/fixed-endian-factory/lib/main.js | 42 ++++ .../test/test.reduce_right.js | 222 ++++++++++++++++++ 2 files changed, 264 insertions(+) create mode 100644 lib/node_modules/@stdlib/array/fixed-endian-factory/test/test.reduce_right.js diff --git a/lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js b/lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js index 3cb7a82e6997..44adbd490019 100644 --- a/lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js +++ b/lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js @@ -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. * diff --git a/lib/node_modules/@stdlib/array/fixed-endian-factory/test/test.reduce_right.js b/lib/node_modules/@stdlib/array/fixed-endian-factory/test/test.reduce_right.js new file mode 100644 index 000000000000..79221a54aade --- /dev/null +++ b/lib/node_modules/@stdlib/array/fixed-endian-factory/test/test.reduce_right.js @@ -0,0 +1,222 @@ +/** +* @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 tape = require( 'tape' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var isFunction = require( '@stdlib/assert/is-function' ); +var factory = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof factory, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns a function', function test( t ) { + var ctor = factory( 'float64' ); + t.strictEqual( isFunction( ctor ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'attached to the prototype of the returned function is a `reduceRight` method', function test( t ) { + var ctor = factory( 'float64' ); + t.strictEqual( hasOwnProp( ctor.prototype, 'reduceRight' ), true, 'returns expected value' ); + t.strictEqual( isFunction( ctor.prototype.reduceRight ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the method throws an error if invoked with a `this` context which is not a typed array instance', function test( t ) { + var values; + var ctor; + var arr; + var i; + + ctor = factory( 'float64' ); + arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.reduceRight.call( value, reducer ); + }; + } + + function reducer( acc, value ) { + if ( value ) { + return acc + 1; + } + return acc; + } +}); + +tape( 'the method throws an error if provided a first argument which is not a function', function test( t ) { + var values; + var ctor; + var arr; + var i; + + ctor = factory( 'float64' ); + arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0 ] ); + + values = [ + '5', + 3.14, + NaN, + true, + false, + null, + void 0, + {}, + [] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.reduceRight( value ); + }; + } +}); + +tape( 'the method throws an error if not provided an initial value when operating on an empty array', function test( t ) { + var ctor; + var arr; + + ctor = factory( 'float64' ); + arr = new ctor( 'little-endian' ); + t.throws( foo, Error, 'throws an error' ); + t.end(); + + function foo() { + arr.reduceRight( reducer ); + } + + function reducer( acc, value ) { + if ( value ) { + return acc + 1; + } + return acc; + } +}); + +tape( 'the method uses the first element of the array as the initial value when an initial value is not provided', function test( t ) { + var valueArray; + var accArray; + var expected; + var actual; + var ctor; + var arr; + var ind; + var len; + + ctor = factory( 'float64' ); + arr = new ctor( 'little-endian', [ 1.0, 0.0, 1.0, 0.0 ] ); + len = arr.length; + accArray = [ 0.0, 0.0, 0.0 ]; + valueArray = [ 1.0, 0.0, 1.0 ]; + expected = 0.0; + actual = arr.reduceRight( reducer ); + + t.strictEqual( actual, expected, 'returns expected value' ); + + t.end(); + + function reducer( acc, value, index ) { + ind = ( len - index - 2 ); + t.strictEqual( acc, accArray[ ind ], 'returns expected value' ); + t.strictEqual( value, valueArray[ ind ], 'returns expected value' ); + return ( acc && value ); + } +}); + +tape( 'the method supports providing an initial value as the second argument', function test( t ) { + var valueArray; + var accArray; + var expected; + var actual; + var ctor; + var arr; + var ind; + var len; + + ctor = factory( 'float64' ); + arr = new ctor( 'little-endian', [ 1.0, 0.0, 0.0, 1.0 ] ); + len = arr.length; + accArray = [ 0.0, 1.0, 1.0, 1.0 ]; + valueArray = [ 1.0, 0.0, 0.0, 1.0 ]; + expected = 2; + actual = arr.reduceRight( reducer, 0 ); + + t.strictEqual( actual, expected, 'returns expected value' ); + t.end(); + + function reducer( acc, value, index ) { + ind = ( len - index - 1 ); + t.strictEqual( acc, accArray[ ind ], 'returns expected value' ); + t.strictEqual( value, valueArray[ ind ], 'returns expected value' ); + if ( value ) { + return acc + 1; + } + return acc; + } +}); + +tape( 'the method returns the accumulated result', function test( t ) { + var expected; + var actual; + var ctor; + var arr; + + ctor = factory( 'float64' ); + arr = new ctor( 'little-endian', [ 1.0, 0.0, 1.0, 1.0 ] ); + expected = 0.0; + actual = arr.reduceRight( reducer ); + + t.strictEqual( actual, expected, 'returns expected value' ); + t.end(); + + function reducer( acc, value ) { + return ( acc && value ); + } +}); From ceeaabc1fb19dcccfcef0663ac69ac21c289e245 Mon Sep 17 00:00:00 2001 From: aayush0325 Date: Sat, 30 Nov 2024 09:05:14 +0530 Subject: [PATCH 2/4] feat: benchmarks and readme added --- .../array/fixed-endian-factory/README.md | 46 +++++++ .../benchmark/benchmark.reduce_right.js | 75 +++++++++++ .../benchmark.reduce_right.length.js | 116 ++++++++++++++++++ 3 files changed, 237 insertions(+) create mode 100644 lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.reduce_right.js create mode 100644 lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.reduce_right.length.js diff --git a/lib/node_modules/@stdlib/array/fixed-endian-factory/README.md b/lib/node_modules/@stdlib/array/fixed-endian-factory/README.md index 46e322fe4eed..21d19d42ec5a 100644 --- a/lib/node_modules/@stdlib/array/fixed-endian-factory/README.md +++ b/lib/node_modules/@stdlib/array/fixed-endian-factory/README.md @@ -577,6 +577,52 @@ var count = context.count; // returns 3; ``` + + +#### 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 + +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 + +var out = arr.reduceRight( reducer, 0 ); +// returns 2 +``` + #### TypedArrayFE.prototype.set( arr\[, offset] ) diff --git a/lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.reduce_right.js b/lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.reduce_right.js new file mode 100644 index 000000000000..8507bd66c843 --- /dev/null +++ b/lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.reduce_right.js @@ -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(); +}); diff --git a/lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.reduce_right.length.js b/lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.reduce_right.length.js new file mode 100644 index 000000000000..a93d9b4cf6ca --- /dev/null +++ b/lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.reduce_right.length.js @@ -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(); From 0d08d28fc54da93926388ee4b50e31181b6fa58e Mon Sep 17 00:00:00 2001 From: Philipp Burckhardt Date: Sat, 30 Nov 2024 19:05:20 -0500 Subject: [PATCH 3/4] docs: update description to match README.md Signed-off-by: Philipp Burckhardt --- lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js b/lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js index 44adbd490019..bb10ecfe9106 100644 --- a/lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js +++ b/lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js @@ -725,7 +725,7 @@ function factory( dtype ) { // eslint-disable-line max-lines-per-function, stdli }); /** - * 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. + * 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. * * @name reduceRight * @memberof TypedArray.prototype From e26bc55e00346d5bc8e375e58fc12016e04f3ff6 Mon Sep 17 00:00:00 2001 From: Philipp Burckhardt Date: Sat, 30 Nov 2024 19:15:07 -0500 Subject: [PATCH 4/4] style: remove extra whitespace --- lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js b/lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js index 07c30092361a..c6a7154abf8a 100644 --- a/lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js +++ b/lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js @@ -768,7 +768,7 @@ 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 order, passing in the return value from the calculation on the preceding element and returning the accumulated result upon completion. *