Skip to content

feat: add reduce and reduceRight methods to array/bool #2509

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

Merged
merged 5 commits into from
Jul 5, 2024
Merged
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
96 changes: 96 additions & 0 deletions lib/node_modules/@stdlib/array/bool/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,102 @@ var count = context.count;
// returns 3;
```

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

#### BooleanArray.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 arr = new BooleanArray( 3 );

arr.set( true, 0 );
arr.set( false, 1 );
arr.set( true, 2 );

var out = arr.reduce( reducer );
// returns false
```

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 arr = new BooleanArray( 3 );

arr.set( true, 0 );
arr.set( false, 1 );
arr.set( true, 2 );

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

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

#### Complex64Array.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 arr = new BooleanArray( 3 );

arr.set( true, 0 );
arr.set( false, 1 );
arr.set( true, 2 );

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

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 last element in the array and passes the second-last array element as `value` during the first invocation of the provided callback. To begin accumulation from a different starting value and pass in the last 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 arr = new BooleanArray( 3 );

arr.set( true, 0 );
arr.set( false, 1 );
arr.set( true, 2 );

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

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

#### BooleanArray.prototype.reverse()
Expand Down
70 changes: 70 additions & 0 deletions lib/node_modules/@stdlib/array/bool/benchmark/benchmark.reduce.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
* @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 BooleanArray = require( './../lib' );


// FUNCTIONS //

/**
* Reducer function.
*
* @private
* @param {integer} acc - accumulated value
* @param {boolean} 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 BooleanArray( [ true, false, false, true ] );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = arr.reduce( reducer, 0 );
if ( typeof out !== 'number' ) {
b.fail( 'should return an integer' );
}
}
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,119 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2023 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 isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive;
var Boolean = require( '@stdlib/boolean/ctor' );
var pkg = require( './../package.json' ).name;
var BooleanArray = require( './../lib' );


// FUNCTIONS //

/**
* Reducer function.
*
* @private
* @param {integer} acc - accumulated value
* @param {boolean} 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;
var i;

arr = [];
for ( i = 0; i < len; i++ ) {
arr.push( Boolean( i%2 ) );
}
arr = new BooleanArray( arr );

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, 0 );
if ( typeof out !== 'number' ) {
b.fail( 'should return an integer' );
}
}
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();
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
* @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 BooleanArray = require( './../lib' );


// FUNCTIONS //

/**
* Reducer function.
*
* @private
* @param {integer} acc - accumulated value
* @param {boolean} 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 BooleanArray( [ true, false, false, true ] );

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