Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add findIndex method to array/fixed-endian-factory #3239

Closed
wants to merge 0 commits into from

Conversation

pranav-1720
Copy link

Resolves #3142.

Description

This pull request:

  • Adds findIndex method to array/fixed-endian-factory
  • Add tests for the respective method
  • Add benchmarks files for the respective method
  • Updates README for fixed-endian-factory for the respective method

Related Issues

Does this pull request have any related issues?

This pull request:

Questions

Any questions for reviewers of this pull request?

No.

Other

Any other information relevant to this pull request? This may include screenshots, references, and/or implementation notes.

No.

Checklist

Please ensure the following tasks are completed before submitting this pull request.


@stdlib-js/reviewers

Copy link
Contributor

@stdlib-bot stdlib-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👋 Hi there! 👋

And thank you for opening your first pull request! We will review it shortly. 🏃 💨

@stdlib-bot
Copy link
Contributor

Coverage Report

Package Statements Branches Functions Lines
array/fixed-endian-factory $\color{red}768/1028$
$\color{green}+74.71\%$
$\color{red}43/53$
$\color{green}+81.13\%$
$\color{red}11/22$
$\color{green}+50.00\%$
$\color{red}768/1028$
$\color{green}+74.71\%$

The above coverage report was generated for the changes in this PR.

@kgryte kgryte changed the title feat: add findIndex method to array/fixed-endian-factory feat: add findIndex method to array/fixed-endian-factory Nov 24, 2024
@kgryte kgryte added Feature Issue or pull request for adding a new feature. Needs Review A pull request which needs code review. labels Nov 24, 2024
@@ -338,6 +338,62 @@ v = arr.at( -100 );
// returns undefined
```

<a name="method-find-index"></a>

#### TypedArrayFE.prototype.findIndex( callbackFn\[, thisArg] )
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#### TypedArrayFE.prototype.findIndex( callbackFn\[, thisArg] )
#### TypedArrayFE.prototype.findIndex( predicate\[, thisArg] )

Renaming to ensure consistent documentation across array types.

Comment on lines 345 to 346
Returns the index of the first element in a typed array that satisfies the provided testing function.
If no element satisfies function, return -1.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Returns the index of the first element in a typed array that satisfies the provided testing function.
If no element satisfies function, return -1.
Returns the index of the first element in a typed array for which a predicate function returns a truthy value.

To ensure consistent docs across array types.

Comment on lines 349 to 361
function greaterthantwo( v ) {
return ( v > 2.0 );
}

var Float64ArrayFE = fixedEndianFactory( 'float64' );

var arr = new Float64ArrayFE( 'little-endian', 3 );

arr.set( 1.5, 0 );
arr.set( 2.5, 1 );
arr.set( 3.5, 2 );

var idx = arr.findIndex( greaterthantwo );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
function greaterthantwo( v ) {
return ( v > 2.0 );
}
var Float64ArrayFE = fixedEndianFactory( 'float64' );
var arr = new Float64ArrayFE( 'little-endian', 3 );
arr.set( 1.5, 0 );
arr.set( 2.5, 1 );
arr.set( 3.5, 2 );
var idx = arr.findIndex( greaterthantwo );
function predicate( v ) {
return ( v > 2.0 );
}
var Float64ArrayFE = fixedEndianFactory( 'float64' );
var arr = new Float64ArrayFE( 'little-endian', 3 );
arr.set( 1.5, 0 );
arr.set( 2.5, 1 );
arr.set( 3.5, 2 );
var idx = arr.findIndex( predicate );

// returns 1
```

The invoked function is provided three arguments:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The invoked function is provided three arguments:
The `predicate` function is provided three arguments:

Comment on lines 374 to 391
function fcn( v, i ) {
this.count += 1;
return (v === 2.0);
}

var Float64ArrayFE = fixedEndianFactory( 'float64' );

var arr = new Float64ArrayFE( 'little-endian', 3 );

var context = {
'count': 0
};

arr.set( 1.0, 0 );
arr.set( 2.0, 1 );
arr.set( 3.0, 2 );

arr.findIndex( fcn, context );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
function fcn( v, i ) {
this.count += 1;
return (v === 2.0);
}
var Float64ArrayFE = fixedEndianFactory( 'float64' );
var arr = new Float64ArrayFE( 'little-endian', 3 );
var context = {
'count': 0
};
arr.set( 1.0, 0 );
arr.set( 2.0, 1 );
arr.set( 3.0, 2 );
arr.findIndex( fcn, context );
function predicate( v, i ) {
this.count += 1;
return ( v === 2.0 );
}
var Float64ArrayFE = fixedEndianFactory( 'float64' );
var arr = new Float64ArrayFE( 'little-endian', 3 );
var context = {
'count': 0
};
arr.set( 1.0, 0 );
arr.set( 2.0, 1 );
arr.set( 3.0, 2 );
arr.findIndex( predicate, context );

Comment on lines 41 to 57
function predicate( v ) {
return ( v === 3.0 );
}

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
idx = arr.findIndex( predicate );
if ( typeof idx !== 'number' ) {
b.fail( 'should return an integer' );
}
}
b.toc();
if ( !isInteger( idx ) ) {
b.fail( 'should return an integer' );
}
b.pass( 'benchmark finished' );
b.end();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
function predicate( v ) {
return ( v === 3.0 );
}
b.tic();
for ( i = 0; i < b.iterations; i++ ) {
idx = arr.findIndex( predicate );
if ( typeof idx !== 'number' ) {
b.fail( 'should return an integer' );
}
}
b.toc();
if ( !isInteger( idx ) ) {
b.fail( 'should return an integer' );
}
b.pass( 'benchmark finished' );
b.end();
b.tic();
for ( i = 0; i < b.iterations; i++ ) {
idx = arr.findIndex( predicate );
if ( typeof idx !== 'number' ) {
b.fail( 'should return an integer' );
}
}
b.toc();
if ( !isInteger( idx ) ) {
b.fail( 'should return an integer' );
}
b.pass( 'benchmark finished' );
b.end();
function predicate( v ) {
return ( v > 3.0 );
}

We typically place function declarations at the end of their scope.

* @returns {Function} benchmark function
*/
function createBenchmark( len ) {
var arr = new Float64ArrayFE( 'little-endian', zeroTo(len));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var arr = new Float64ArrayFE( 'little-endian', zeroTo(len));
var arr = new Float64ArrayFE( 'little-endian', zeroTo( len ) );


b.tic();
for ( i = 0; i < b.iterations; i++ ) {
idx=arr.findIndex( callback );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
idx=arr.findIndex( callback );
idx = arr.findIndex( callback );

for ( i = min; i <= max; i++ ) {
len = pow( 10, i );
f = createBenchmark( len );
bench( pkg+':forEach:len='+len, f );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
bench( pkg+':forEach:len='+len, f );
bench( pkg+':findIndex:len='+len, f );

* @name findIndex
* @memberof TypedArray.prototype
* @type {Function}
* @param {Function} callbackFn - function to invoke
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @param {Function} callbackFn - function to invoke
* @param {Function} predicate - predicate function

I suggest renaming here and below.

@@ -538,6 +538,39 @@ function factory( dtype ) { // eslint-disable-line max-lines-per-function, stdli
*/
setReadOnly( TypedArray.prototype, 'BYTES_PER_ELEMENT', TypedArray.BYTES_PER_ELEMENT );

/**
* Invokes a function once for each array element.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Invokes a function once for each array element.
* Returns the index of the first element in an array for which a predicate function returns a truthy value.

* @param {*} [thisArg] - function invocation context
* @throws {TypeError} `this` must be a typed array instance
* @throws {TypeError} first argument must be a function
*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing @returns documentation. See array/bool.

Comment on lines 565 to 567
for (i = 0; i < this._length; i++) {
result = fcn.call(thisArg, buf[GETTER](i * BYTES_PER_ELEMENT, this._isLE), i, this);
if (result) {
Copy link
Member

@kgryte kgryte Nov 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for (i = 0; i < this._length; i++) {
result = fcn.call(thisArg, buf[GETTER](i * BYTES_PER_ELEMENT, this._isLE), i, this);
if (result) {
for ( i = 0; i < this._length; i++ ) {
if ( fcn.call( thisArg, buf[ GETTER ] ( i * BYTES_PER_ELEMENT, this._isLE ), i, this ) ) {

Use consistent spacing.

* @throws {TypeError} first argument must be a function
*/
setReadOnly( TypedArray.prototype, 'findIndex', function findIndex( fcn, thisArg ) {
var result;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var result;

return i;
}
}
return -1; // Not found
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return -1; // Not found
return -1;

t.end();
});

tape( 'attached to the prototype of the returned function is a `forEach` method', function test( t ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
tape( 'attached to the prototype of the returned function is a `forEach` method', function test( t ) {
tape( 'attached to the prototype of the returned function is a `findIndex` method', function test( t ) {

Copy link
Member

@kgryte kgryte left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this, @pranav-1720. Left an initial round of comments.

@kgryte
Copy link
Member

kgryte commented Nov 25, 2024

@pranav-1720 Would you also mind resolving the merge conflict? Thanks!

@kgryte kgryte added Needs Changes Pull request which needs changes before being merged. and removed Needs Review A pull request which needs code review. labels Nov 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature Issue or pull request for adding a new feature. Needs Changes Pull request which needs changes before being merged.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[RFC]: add findIndex method to array/fixed-endian-factory
3 participants