Skip to content

Commit abe8ab4

Browse files
feat: add find method to array/fixed-endian-factory
1 parent 6ae3c11 commit abe8ab4

File tree

5 files changed

+456
-0
lines changed

5 files changed

+456
-0
lines changed

lib/node_modules/@stdlib/array/fixed-endian-factory/README.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,69 @@ var count = context.count;
386386
// returns 3
387387
```
388388

389+
<a name="method-find"></a>
390+
391+
#### TypedArrayFE.prototype.find( predicate\[, thisArg] )
392+
393+
Returns the first element in the array that satisfies the provided testing function (predicate). Otherwise, returns `undefined` if no element satisfies the condition.
394+
395+
```javascript
396+
function isNegative( v ) {
397+
return v < 0;
398+
}
399+
400+
var Float64ArrayFE = fixedEndianFactory( 'float64' );
401+
402+
var arr = new Float64ArrayFE( 'little-endian', [ -1.0, 2.0, 3.0, -4.0 ] );
403+
404+
var value = arr.find( isNegative );
405+
// returns -1.0 (the first negative value)
406+
```
407+
408+
The `predicate` function is provided three arguments:
409+
410+
- **value**: current array element.
411+
- **index**: current array element index.
412+
- **arr**: the array on which this method was called.
413+
414+
To set the function execution context, provide a thisArg.
415+
416+
```javascript
417+
function isEven( v, i ) {
418+
this.count += 1;
419+
return v % 2 === 0;
420+
}
421+
422+
var Float64ArrayFE = fixedEndianFactory( 'float64' );
423+
424+
var arr = new Float64ArrayFE( 'little-endian', [ 1.0, 2.0, 3.0, 4.0 ] );
425+
426+
var context = {
427+
'count': 0
428+
};
429+
430+
var value = arr.find( isEven, context );
431+
// returns 2.0
432+
433+
var count = context.count;
434+
// returns 2
435+
```
436+
437+
If no element in the array satisfies the predicate, the method returns undefined.
438+
439+
```javascript
440+
function isGreaterThan( v ) {
441+
return v > 10;
442+
}
443+
444+
var Float64ArrayFE = fixedEndianFactory( 'float64' );
445+
446+
var arr = new Float64ArrayFE( 'little-endian', [ 1.0, 2.0, 3.0 ] );
447+
448+
var value = arr.find( isGreaterThan );
449+
// returns undefined
450+
```
451+
389452
<a name="method-for-each"></a>
390453

391454
#### TypedArrayFE.prototype.forEach( callbackFn\[, thisArg] )
@@ -542,6 +605,8 @@ var str = arr.toString();
542605
// returns '1,2,3'
543606
```
544607

608+
609+
545610
</section>
546611

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

lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,35 @@ function factory( dtype ) { // eslint-disable-line max-lines-per-function, stdli
569569
return true;
570570
});
571571

572+
/**
573+
* Finds the first element in the array that satisfies the provided testing function.
574+
*
575+
* @private
576+
* @name find
577+
* @memberof TypedArray.prototype
578+
* @type {Function}
579+
* @param {Function} predicate - Function to test each element of the array.
580+
* @param {*} [thisArg] - predicate function execution context
581+
* @throws {TypeError} `this` must be a typed array instance
582+
* @returns {*} The value of the first element in the array that satisfies the testing function. Otherwise, undefined.
583+
*/
584+
setReadOnly(TypedArray.prototype, 'find', function find(predicate, thisArg) {
585+
var value;
586+
var i;
587+
if (!isTypedArray(this)) {
588+
throw new TypeError('invalid invocation. `this` is not a typed array instance.');
589+
}
590+
if (typeof predicate !== 'function') {
591+
throw new TypeError(format( 'invalid argument. First argument must be a function. Value: `%s`.', predicate ) );
592+
}
593+
for (i = 0; i < this._length; i++) {
594+
value = this._buffer[GETTER](i * BYTES_PER_ELEMENT, this._isLE);
595+
if (predicate.call(thisArg, value, i, this)) {
596+
return value;
597+
}
598+
}
599+
});
600+
572601
/**
573602
* Invokes a function once for each array element.
574603
*

0 commit comments

Comments
 (0)