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..f1fd638ded6b 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.reduce( reducerFn\[, initialValue] ) + +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. + +```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.reduce( 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.reduce( reducer, 0 ); +// returns 2 +``` + #### TypedArrayFE.prototype.set( arr\[, offset] ) diff --git a/lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.reduce.js b/lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.reduce.js new file mode 100644 index 000000000000..9ad7fe329f45 --- /dev/null +++ b/lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.reduce.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+':reduce', 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.reduce( 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.length.js b/lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.reduce.length.js new file mode 100644 index 000000000000..0bd1ac04164a --- /dev/null +++ b/lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.reduce.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.reduce( 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+':reduce:len='+len, f ); + } +} + +main(); 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..e0f4d7a015ec 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 order, passing in the return value from the calculation on the preceding element and returning the accumulated result upon completion. + * + * @name reduce + * @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, 'reduce', function reduce( 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 = 0; + } 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 ]( 0 * BYTES_PER_ELEMENT, this._isLE ); + i = 1; + } + for ( ; i < len; 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.js b/lib/node_modules/@stdlib/array/fixed-endian-factory/test/test.reduce.js new file mode 100644 index 000000000000..7611a1748c63 --- /dev/null +++ b/lib/node_modules/@stdlib/array/fixed-endian-factory/test/test.reduce.js @@ -0,0 +1,216 @@ +/** +* @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 `reduce` method', function test( t ) { + var ctor = factory( 'float64' ); + t.strictEqual( hasOwnProp( ctor.prototype, 'reduce' ), true, 'returns expected value' ); + t.strictEqual( isFunction( ctor.prototype.reduce ), 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.reduce.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.reduce( 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.reduce( 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; + + ctor = factory( 'float64' ); + arr = new ctor( 'little-endian', [ 1.0, 0.0, 1.0, 0.0 ] ); + accArray = [ 1.0, 0.0, 0.0 ]; + valueArray = [ 0.0, 1.0, 0.0 ]; + expected = 0.0; + actual = arr.reduce( reducer ); + + t.strictEqual( actual, expected, 'returns expected value' ); + + t.end(); + + function reducer( acc, value, index ) { + ind = index-1; + 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; + + ctor = factory( 'float64' ); + arr = new ctor( 'little-endian', [ 1.0, 0.0, 0.0, 1.0 ] ); + accArray = [ 0.0, 1.0, 1.0, 1.0 ]; + valueArray = [ 1.0, 0.0, 0.0, 1.0 ]; + expected = 2; + actual = arr.reduce( reducer, 0 ); + + t.strictEqual( actual, expected, 'returns expected value' ); + t.end(); + + function reducer( acc, value, index ) { + t.strictEqual( acc, accArray[ index ], 'returns expected value' ); + t.strictEqual( value, valueArray[ index ], '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.reduce( reducer ); + + t.strictEqual( actual, expected, 'returns expected value' ); + t.end(); + + function reducer( acc, value ) { + return ( acc && value ); + } +});