8000 Auto-generated commit · stdlib-js/array-bool@b03d413 · GitHub
[go: up one dir, main page]

Skip to content

Commit b03d413

Browse files
committed
Auto-generated commit
1 parent 1233f60 commit b03d413

13 files changed

+1186
-5
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
55
<section class="release" id="unreleased">
66

7-
## Unreleased (2024-06-29)
7+
## Unreleased (2024-07-05)
88

99
<section class="features">
1010

1111
### Features
1212

13+
- [`4a6be43`](https://github.com/stdlib-js/stdlib/commit/4a6be430830868fb181bfa0b8923f37dabaffe8a) - add `reduce` and `reduceRight` methods to `array/bool` [(#2509)](https://github.com/stdlib-js/stdlib/pull/2509)
1314
- [`29615af`](https://github.com/stdlib-js/stdlib/commit/29615af970796a43f65f4b00d29bd23a122f2208) - add `slice` and `subarray` methods to `array/bool` [(#2472)](https://github.com/stdlib-js/stdlib/pull/2472)
1415
- [`fbc42b4`](https://github.com/stdlib-js/stdlib/commit/fbc42b4c66cf695c6c114f64bf3eff65186026f0) - add `includes` method to `array/bool` [(#2441)](https://github.com/stdlib-js/stdlib/pull/2441)
1516
- [`c58e9e4`](https://github.com/stdlib-js/stdlib/commit/c58e9e4dce4361b4ae7454eca926b0e00afb15aa) - add `indexOf` and `lastIndexOf` methods to `array/bool` [(#2432)](https://github.com/stdlib-js/stdlib/pull/2432)
@@ -26,6 +27,7 @@
2627

2728
<details>
2829

30+
- [`4a6be43`](https://github.com/stdlib-js/stdlib/commit/4a6be430830868fb181bfa0b8923f37dabaffe8a) - **feat:** add `reduce` and `reduceRight` methods to `array/bool` [(#2509)](https://github.com/stdlib-js/stdlib/pull/2509) _(by Jaysukh Makvana, Athan Reines)_
2931
- [`29615af`](https://github.com/stdlib-js/stdlib/commit/29615af970796a43f65f4b00d29bd23a122f2208) - **feat:** add `slice` and `subarray` methods to `array/bool` [(#2472)](https://github.com/stdlib-js/stdlib/pull/2472) _(by Jaysukh Makvana, Athan Reines)_
3032
- [`fbc42b4`](https://github.com/stdlib-js/stdlib/commit/fbc42b4c66cf695c6c114f64bf3eff65186026f0) - **feat:** add `includes` method to `array/bool` [(#2441)](https://github.com/stdlib-js/stdlib/pull/2441) _(by Jaysukh Makvana)_
3133
- [`c58e9e4`](https://github.com/stdlib-js/stdlib/commit/c58e9e4dce4361b4ae7454eca926b0e00afb15aa) - **feat:** add `indexOf` and `lastIndexOf` methods to `array/bool` [(#2432)](https://github.com/stdlib-js/stdlib/pull/2432) _(by Jaysukh Makvana, Athan Reines)_

README.md

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,102 @@ var count = context.count;
812812
// returns 3;
813813
```
814814

815+
<a name="method-reduce"></a>
816+
817+
#### BooleanArray.prototype.reduce( reducerFn\[, initialValue] )
818+
819+
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.
820+
821+
```javascript
822+
function reducer( acc, v ) {
823+
return ( acc && v );
824+
}
825+
826+
var arr = new BooleanArray( 3 );
827+
828+
arr.set( true, 0 );
829+
arr.set( false, 1 );
830+
arr.set( true, 2 );
831+
832+
var out = arr.reduce( reducer );
833+
// returns false
834+
```
835+
836+
The reducer function is provided four arguments:
837+
838+
- **acc**: accumulated result.
839+
- **value**: current array element.
840+
- **index**: current array element index.
841+
- **arr**: the array on which this method was called.
842+
843+
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.
844+
845+
```javascript
846+
function reducer( acc, v ) {
847+
if ( v ) {
848+
return acc + 1;
849+
}
850+
return acc;
851+
}
852+
853+
var arr = new BooleanArray( 3 );
854+
855+
arr.set( true, 0 );
856+
arr.set( false, 1 );
857+
arr.set( true, 2 );
858+
859+
var out = arr.reduce( reducer, 0 );
860+
// returns 2
861+
```
862+
863+
<a name="method-reduce-right"></a>
864+
865+
#### Complex64Array.prototype.reduceRight( reducerFn\[, initialValue] )
866+
867+
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.
868+
869+
```javascript
870+
function reducer( acc, v ) {
871+
return ( acc && v );
872+
}
873+
874+
var arr = new BooleanArray( 3 );
875+
876+
arr.set( true, 0 );
877+
arr.set( false, 1 );
878+
arr.set( true, 2 );
879+
880+
var out = arr.reduceRight( reducer );
881+
// returns false
882+
```
883+
884+
The reducer function is provided four arguments:
885+
886+
- **acc**: accumulated result.
887+
- **value**: current array element.
888+
- **index**: current array element index.
889+
- **arr**: the array on which this method was called.
890+
891+
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.
892+
893+
```javascript
894+
function reducer( acc, v ) {
895+
if ( v ) {
896+
return acc + 1;
897+
}
898+
return acc;
899+
}
900+
901+
var arr = new BooleanArray( 3 );
902+
903+
arr.set( true, 0 );
904+
arr.set( false, 1 );
905+
arr.set( true, 2 );
906+
907+
var out = arr.reduceRight( reducer, 0 );
908+
// returns 2
909+
```
910+
815911
<a name="method-reverse"></a>
816912

817913
#### BooleanArray.prototype.reverse()

benchmark/benchmark.reduce.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench-harness' );
24+
var isInteger = require( '@stdlib/assert-is-integer' ).isPrimitive;
25+
var pkg = require( './../package.json' ).name;
26+
var BooleanArray = require( './../lib' );
27+
28+
29+
// FUNCTIONS //
30+
31+
/**
32+
* Reducer function.
33+
*
34+
* @private
35+
* @param {integer} acc - accumulated value
36+
* @param {boolean} value - current array element
37+
* @param {integer} index - current array index
38+
* @returns {integer} accumulated value
39+
*/
40+
function reducer( acc, value ) {
41+
if ( value ) {
42+
return acc + 1;
43+
}
44+
return acc;
45+
}
46+
47+
48+
// MAIN //
49+
50+
bench( pkg+':reduce', function benchmark( b ) {
51+
var out;
52+
var arr;
53+
var i;
54+
55+
arr = new BooleanArray( [ true, false, false, true ] );
56+
57+
b.tic();
58+
for ( i = 0; i < b.iterations; i++ ) {
59+
out = arr.reduce( reducer, 0 );
60+
if ( typeof out !== 'number' ) {
61+
F438 b.fail( 'should return an integer' );
62+
}
63+
}
64+
b.toc();
65+
if ( !isInteger( out ) ) {
66+
b.fail( 'should return an integer' );
67+
}
68+
b.pass( 'benchmark finished' );
69+
b.end();
70+
});

benchmark/benchmark.reduce.length.js

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench-harness' );
24+
var pow = require( '@stdlib/math-base-special-pow' );
25+
var isInteger = require( '@stdlib/assert-is-integer' ).isPrimitive;
26+
var Boolean = require( '@stdlib/boolean-ctor' );
27+
var pkg = require( './../package.json' ).name;
28+
var BooleanArray = require( './../lib' );
29+
30+
31+
// FUNCTIONS //
32+
33+
/**
34+
* Reducer function.
35+
*
36+
* @private
37+
* @param {integer} acc - accumulated value
38+
* @param {boolean} value - current array element
39+
* @param {integer} index - current array index
40+
* @returns {integer} accumulated value
41+
*/
42+
function reducer( acc, value ) {
43+
if ( value ) {
44+
return acc + 1;
45+
}
46+
return acc;
47+
}
48+
49+
/**
50+
* Creates a benchmark function.
51+
*
52+
* @private
53+
* @param {PositiveInteger} len - array length
54+
* @returns {Function} benchmark function
55+
*/
56+
function createBenchmark( len ) {
57+
var arr;
58+
var i;
59+
60+
arr = [];
61+
for ( i = 0; i < len; i++ ) {
62+
arr.push( Boolean( i%2 ) );
63+
}
64+
arr = new BooleanArray( arr );
65+
66+
return benchmark;
67+
68+
/**
69+
* Benchmark function.
70+
*
71+
* @private
72+
* @param {Benchmark} b - benchmark instance
73+
*/
74+
function benchmark( b ) {
75+
var out;
76+
var i;
77+
78+
b.tic();
79+
for ( i = 0; i < b.iterations; i++ ) {
80+
out = arr.reduce( reducer, 0 );
81+
if ( typeof out !== 'number' ) {
82+
b.fail( 'should return an integer' );
83+
}
84+
}
85+
b.toc();
86+
if ( !isInteger( out ) ) {
87+
b.fail( 'should return an integer' );
88+
}
89+
b.pass( 'benchmark finished' );
90+
b.end();
91+
}
92+
}
93+
94+
95+
// MAIN //
96+
97+
/**
98+
* Main execution sequence.
99+
*
100+
* @private
101+
*/
102+
function main() {
103+
var len;
104+
var min;
105+
var max;
106+
var f;
107+
var i;
108+
109+
min = 1; // 10^min
110+
max = 6; // 10^max
111+
112+
for ( i = min; i <= max; i++ ) {
113+
len = pow( 10, i );
114+
f = createBenchmark( len );
115+
bench( pkg+':reduce:len='+len, f );
116+
}
117+
}
118+
119+
main();

benchmark/benchmark.reduce_right.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench-harness' );
24+
var isInteger = require( '@stdlib/assert-is-integer' ).isPrimitive;
25+
var pkg = require( './../package.json' ).name;
26+
var BooleanArray = require( './../lib' );
27+
28+
29+
// FUNCTIONS //
30+
31+
/**
32+
* Reducer function.
33+
*
34+
* @private
35+
* @param {integer} acc - accumulated value
36+
* @param {boolean} value - current array element
37+
* @param {integer} index - current array index
38+
* @returns {integer} accumulated value
39+
*/
40+
function reducer( acc, value ) {
41+
if ( value ) {
42+
return acc + 1;
43+
}
44+
return acc;
45+
}
46+
47+
48+
// MAIN //
49+
50+
bench( pkg+':reduceRight', function benchmark( b ) {
51+
var out;
52+
var arr;
53+
var i;
54+
55+
arr = new BooleanArray( [ true, false, false, true ] );
56+
57+
b.tic();
58+
for ( i = 0; i < b.iterations; i++ ) {
59+
out = arr.reduceRight( reducer, 0 );
60+
if ( typeof out !== 'number' ) {
61+
b.fail( 'should return an integer' );
62+
}
63+
}
64+
b.toc();
65+
if ( !isInteger( out ) ) {
66+
b.fail( 'should return an integer' );
67+
}
68+
b.pass( 'benchmark finished' );
69+
b.end();
70+
});

0 commit comments

Comments
 (0)
0