Skip to content

Commit 5cd4a70

Browse files
Jaysukh-409kgryte
andauthored
feat: add findIndex and findLastIndex methods to array/bool
PR-URL: stdlib-js#2384 Ref: stdlib-js#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 fb8d458 commit 5cd4a70

File tree

9 files changed

+936
-2
lines changed

9 files changed

+936
-2
lines changed

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

Lines changed: 106 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ The `predicate` function is provided three arguments:
364364
To set the function execution context, provide a `thisArg`.
365365

366366
```javascript
367-
function predicate( v, i ) {
367+
function predicate( v ) {
368368
this.count += 1;
369369
return ( v === true );
370370
}
@@ -386,6 +386,58 @@ var count = context.count;
386386
// returns 3
387387
```
388388

389+
<a name="method-find-index"></a>
390+
391+
#### BooleanArray.prototype.findIndex( predicate\[, thisArg] )
392+
393+
Returns the index of the first 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.findIndex( predicate );
407+
// returns 0
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 ) {
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( false, 0 );
431+
arr.set( false, 1 );
432+
arr.set( true, 2 );
433+
434+
var z = arr.findIndex( predicate, context );
435+
// returns 2
436+
437+
var count = context.count;
438+
// returns 3
439+
```
440+
389441
<a name="method-find-last"></a>
390442

391443
#### Complex64Array.prototype.findLast( predicate\[, thisArg] )
@@ -416,7 +468,7 @@ The `predicate` function is provided three arguments:
416468
To set the function execution context, provide a `thisArg`.
417469

418470
```javascript
419-
function predicate( v, i ) {
471+
function predicate( v ) {
420472
this.count += 1;
421473
return ( v === true );
422474
}
@@ -438,6 +490,58 @@ var count = context.count;
438490
// returns 3
439491
```
440492

493+
<a name="method-find-last-index"></a>
494+
495+
#### BooleanArray.prototype.findLastIndex( predicate\[, thisArg] )
496+
497+
Returns the index of the last element in an array for which a predicate function returns a truthy value.
498+
499+
```javascript
500+
function predicate( v ) {
501+
return v === true;
502+
}
503+
504+
var arr = new BooleanArray( 3 );
505+
506+
arr.set( true, 0 );
507+
arr.set( false, 1 );
508+
arr.set( true, 2 );
509+
510+
var v = arr.findLastIndex( predicate );
511+
// returns 2
512+
```
513+
514+
The `predicate` function is provided three arguments:
515+
516+
- **value**: current array element.
517+
- **index**: current array element index.
518+
- **arr**: the array on which this method was called.
519+
520+
To set the function execution context, provide a `thisArg`.
521+
522+
```javascript
523+
function predicate( v ) {
524+
this.count += 1;
525+
return ( v === true );
526+
}
527+
528+
var arr = new BooleanArray( 3 );
529+
530+
var context = {
531+
'count': 0
532+
};
533+
534+
arr.set( true, 0 );
535+
arr.set( false, 1 );
536+
arr.set( false, 2 );
537+
538+
var z = arr.findLastIndex( predicate, context );
539+
// returns 0
540+
541+
var count = context.count;
542+
// returns 3
543+
```
544+
441545
<a name="method-get"></a>
442546

443547
#### 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 isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive;
25+
var pkg = require( './../package.json' ).name;
26+
var BooleanArray = require('./../lib');
27+
28+
29+
// MAIN //
30+
31+
bench( pkg+':findIndex', function benchmark( b ) {
32+
var arr;
33+
var idx;
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+
idx = arr.findIndex( predicate );
41+
if ( typeof idx !== 'number' ) {
42+
b.fail( 'should return an integer' );
43+
}
44+
}
45+
b.toc();
46+
if ( !isInteger( idx ) ) {
47+
b.fail( 'should return an integer' );
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 isInteger = require( '@stdlib/assert/is-integer' ).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 === true );
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( 0 ) );
60+
}
61+
arr.push( Boolean( 1 ) );
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 idx;
74+
var i;
75+
76+
b.tic();
77+
for ( i = 0; i < b.iterations; i++ ) {
78+
idx = arr.findIndex( predicate );
79+
if ( typeof idx !== 'number' ) {
80+
b.fail( 'should return an integer' );
81+
}
82+
}
83+
b.toc();
84+
if ( !isInteger( idx ) ) {
85+
b.fail( 'should return an integer' );
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+':findIndex: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 isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive;
25+
var pkg = require( './../package.json' ).name;
26+
var BooleanArray = require('./../lib');
27+
28+
29+
// MAIN //
30+
31+
bench( pkg+':findLastIndex', function benchmark( b ) {
32+
var arr;
33+
var idx;
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+
idx = arr.findLastIndex( predicate );
41+
if ( typeof idx !== 'number' ) {
42+
b.fail( 'should return an integer' );
43+
}
44+
}
45+
b.toc();
46+
if ( !isInteger( idx ) ) {
47+
b.fail( 'should return an integer' );
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)