Skip to content

Commit cba0d92

Browse files
authored
feat: add find and findLast methods to array/bool
PR-URL: #2376 Ref: #2304 Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent 4c95c5a commit cba0d92

File tree

9 files changed

+976
-0
lines changed

9 files changed

+976
-0
lines changed

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

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,110 @@ var len = arr.length;
334334
// returns 4
335335
```
336336

337+
<a name="method-find"></a>
338+
339+
#### BooleanArray.prototype.find( predicate\[, thisArg] )
340+
341+
Returns the first element in an array for which a predicate function returns a truthy value.
342+
343+
```javascript
344+
function predicate( v ) {
345+
return v === true;
346+
}
347+
348+
var arr = new BooleanArray( 3 );
349+
350+
arr.set( true, 0 );
351+
arr.set( false, 1 );
352+
arr.set( true, 2 );
353+
354+
var v = arr.find( predicate );
355+
// returns true
356+
```
357+
358+
The `predicate` function is provided three arguments:
359+
360+
- **value**: current array element.
361+
- **index**: current array element index.
362+
- **arr**: the array on which this method was called.
363+
364+
To set the function execution context, provide a `thisArg`.
365+
366+
```javascript
367+
function predicate( v, i ) {
368+
this.count += 1;
369+
return ( v === true );
370+
}
371+
372+
var arr = new BooleanArray( 3 );
373+
374+
var context = {
375+
'count': 0
376+
};
377+
378+
arr.set( false, 0 );
379+
arr.set( false, 1 );
380+
arr.set( true, 2 );
381+
382+
var z = arr.find( predicate, context );
383+
// returns true
384+
385+
var count = context.count;
386+
// returns 3
387+
```
388+
389+
<a name="method-find-last"></a>
390+
391+
#### Complex64Array.prototype.findLast( predicate\[, thisArg] )
392+
393+
Returns the last element in an array for which a predicate function returns a truthy value.
394+
395+
```javascript
396+
function predicate( v ) {
397+
return v === true;
398+
}
399+
400+
var arr = new BooleanArray( 3 );
401+
402+
arr.set( true, 0 );
403+
arr.set( false, 1 );
404+
arr.set( true, 2 );
405+
406+
var v = arr.findLast( predicate );
407+
// returns true
408+
```
409+
410+
The `predicate` function is provided three arguments:
411+
412+
- **value**: current array element.
413+
- **index**: current array element index.
414+
- **arr**: the array on which this method was called.
415+
416+
To set the function execution context, provide a `thisArg`.
417+
418+
```javascript
419+
function predicate( v, i ) {
420+
this.count += 1;
421+
return ( v === true );
422+
}
423+
424+
var arr = new BooleanArray( 3 );
425+
426+
var context = {
427+
'count': 0
428+
};
429+
430+
arr.set( true, 0 );
431+
arr.set( false, 1 );
432+
arr.set( false, 2 );
433+
434+
var z = arr.findLast( predicate, context );
435+
// returns true
436+
437+
var count = context.count;
438+
// returns 3
439+
```
440+
337441
<a name="method-get"></a>
338442

339443
#### BooleanArray.prototype.get( i )
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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 isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
25+
var pkg = require( './../package.json' ).name;
26+
var BooleanArray = require( './../lib' );
27+
28+
29+
// MAIN //
30+
31+
bench( pkg+':find', function benchmark( b ) {
32+
var arr;
33+
var v;
34+
var i;
35+
36+
arr = new BooleanArray( [ true, false, false, true, true, false ] );
37+
38+
b.tic();
39+
for ( i = 0; i < b.iterations; i++ ) {
40+
v = arr.find( predicate );
41+
if ( typeof v !== 'boolean' ) {
42+
b.fail( 'should return a boolean' );
43+
}
44+
}
45+
b.toc();
46+
if ( !isBoolean( v ) ) {
47+
b.fail( 'should return a boolean' );
48+
}
49+
b.pass( 'benchmark finished' );
50+
b.end();
51+
52+
function predicate( v ) {
53+
return ( v === false );
54+
}
55+
});
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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 pow = require( '@stdlib/math/base/special/pow' );
25+
var Boolean = require( '@stdlib/boolean/ctor' );
26+
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
27+
var pkg = require( './../package.json' ).name;
28+
var BooleanArray = require( './../lib' );
29+
30+
31+
// FUNCTIONS //
32+
33+
/**
34+
* Predicate function.
35+
*
36+
* @private
37+
* @param {boolean} value - array element
38+
* @param {NonNegativeInteger} idx - array element index
39+
* @param {BooleanArray} arr - array instance
40+
* @returns {boolean} boolean indicating whether a value passes a test
41+
*/
42+
function predicate( value ) {
43+
return ( value === false );
44+
}
45+
46+
/**
47+
* Creates a benchmark function.
48+
*
49+
* @private
50+
* @param {PositiveInteger} len - array length
51+
* @returns {Function} benchmark function
52+
*/
53+
function createBenchmark( len ) {
54+
var arr;
55+
var i;
56+
57+
arr = [];
58+
for ( i = 0; i < len-1; i++ ) {
59+
arr.push( Boolean( 1 ) );
60+
}
61+
arr.push( Boolean( 0 ) );
62+
arr = new BooleanArray( arr );
63+
64+
return benchmark;
65+
66+
/**
67+
* Benchmark function.
68+
*
69+
* @private
70+
* @param {Benchmark} b - benchmark instance
71+
*/
72+
function benchmark( b ) {
73+
var v;
74+
var i;
75+
76+
b.tic();
77+
for ( i = 0; i < b.iterations; i++ ) {
78+
v = arr.find( predicate );
79+
if ( typeof v !== 'boolean' ) {
80+
b.fail( 'should return a boolean' );
81+
}
82+
}
83+
b.toc();
84+
if ( !isBoolean( v ) ) {
85+
b.fail( 'should return a boolean' );
86+
}
87+
b.pass( 'benchmark finished' );
88+
b.end();
89+
}
90+
}
91+
92+
93+
// MAIN //
94+
95+
/**
96+
* Main execution sequence.
97+
*
98+
* @private
99+
*/
100+
function main() {
101+
var len;
102+
var min;
103+
var max;
104+
var f;
105+
var i;
106+
107+
min = 1; // 10^min
108+
max = 6; // 10^max
109+
110+
for ( i = min; i <= max; i++ ) {
111+
len = pow( 10, i );
112+
f = createBenchmark( len );
113+
bench( pkg+':find:len='+len, f );
114+
}
115+
}
116+
117+
main();
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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 isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
25+
var pkg = require( './../package.json' ).name;
26+
var BooleanArray = require( './../lib' );
27+
28+
29+
// MAIN //
30+
31+
bench( pkg+':findLast', function benchmark( b ) {
32+
var arr;
33+
var v;
34+
var i;
35+
36+
arr = new BooleanArray( [ true, false, false, true, true, false ] );
37+
38+
b.tic();
39+
for ( i = 0; i < b.iterations; i++ ) {
40+
v = arr.findLast( predicate );
41+
if ( typeof v !== 'boolean' ) {
42+
b.fail( 'should return a boolean' );
43+
}
44+
}
45+
b.toc();
46+
if ( !isBoolean( v ) ) {
47+
b.fail( 'should return a boolean' );
48+
}
49+
b.pass( 'benchmark finished' );
50+
b.end();
51+
52+
function predicate( v ) {
53+
return ( v === false );
54+
}
55+
});

0 commit comments

Comments
 (0)