Skip to content

Commit 4a6be43

Browse files
Jaysukh-409kgryte
andauthored
feat: add reduce and reduceRight methods to array/bool
PR-URL: #2509 Ref: #2304 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Signed-off-by: Athan Reines <kgryte@gmail.com>
1 parent 457662c commit 4a6be43

File tree

10 files changed

+1179
-0
lines changed

10 files changed

+1179
-0
lines changed

lib/node_modules/@stdlib/array/bool/README.md

+96
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,102 @@ var count = context.count;
779779
// returns 3;
780780
```
781781

782+
<a name="method-reduce"></a>
783+
784+
#### BooleanArray.prototype.reduce( reducerFn\[, initialValue] )
785+
786+
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.
787+
788+
```javascript
789+
function reducer( acc, v ) {
790+
return ( acc && v );
791+
}
792+
793+
var arr = new BooleanArray( 3 );
794+
795+
arr.set( true, 0 );
796+
arr.set( false, 1 );
797+
arr.set( true, 2 );
798+
799+
var out = arr.reduce( reducer );
800+
// returns false
801+
```
802+
803+
The reducer function is provided four arguments:
804+
805+
- **acc**: accumulated result.
806+
- **value**: current array element.
807+
- **index**: current array element index.
808+
- **arr**: the array on which this method was called.
809+
810+
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.
811+
812+
```javascript
813+
function reducer( acc, v ) {
814+
if ( v ) {
815+
return acc + 1;
816+
}
817+
return acc;
818+
}
819+
820+
var arr = new BooleanArray( 3 );
821+
822+
arr.set( true, 0 );
823+
arr.set( false, 1 );
824+
arr.set( true, 2 );
825+
826+
var out = arr.reduce( reducer, 0 );
827+
// returns 2
828+
```
829+
830+
<a name="method-reduce-right"></a>
831+
832+
#### Complex64Array.prototype.reduceRight( reducerFn\[, initialValue] )
833+
834+
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.
835+
836+
```javascript
837+
function reducer( acc, v ) {
838+
return ( acc && v );
839+
}
840+
841+
var arr = new BooleanArray( 3 );
842+
843+
arr.set( true, 0 );
844+
arr.set( false, 1 );
845+
arr.set( true, 2 );
846+
847+
var out = arr.reduceRight( reducer );
848+
// returns false
849+
```
850+
851+
The reducer function is provided four arguments:
852+
853+
- **acc**: accumulated result.
854+
- **value**: current array element.
855+
- **index**: current array element index.
856+
- **arr**: the array on which this method was called.
857+
858+
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.
859+
860+
```javascript
861+
function reducer( acc, v ) {
862+
if ( v ) {
863+
return acc + 1;
864+
}
865+
return acc;
866+
}
867+
868+
var arr = new BooleanArray( 3 );
869+
870+
arr.set( true, 0 );
871+
arr.set( false, 1 );
872+
arr.set( true, 2 );
873+
874+
var out = arr.reduceRight( reducer, 0 );
875+
// returns 2
876+
```
877+
782878
<a name="method-reverse"></a>
783879

784880
#### BooleanArray.prototype.reverse()
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' );
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+
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+
});
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' );
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();
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' );
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)