-
-
Notifications
You must be signed in to change notification settings - Fork 596
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
Conversation
There was a problem hiding this 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. 🏃 💨
Coverage Report
The above coverage report was generated for the changes in this PR. |
findIndex
method to array/fixed-endian-factory
@@ -338,6 +338,62 @@ v = arr.at( -100 ); | |||
// returns undefined | |||
``` | |||
|
|||
<a name="method-find-index"></a> | |||
|
|||
#### TypedArrayFE.prototype.findIndex( callbackFn\[, thisArg] ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#### TypedArrayFE.prototype.findIndex( callbackFn\[, thisArg] ) | |
#### TypedArrayFE.prototype.findIndex( predicate\[, thisArg] ) |
Renaming to ensure consistent documentation across array types.
Returns the index of the first element in a typed array that satisfies the provided testing function. | ||
If no element satisfies function, return -1. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
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 ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The invoked function is provided three arguments: | |
The `predicate` function is provided three arguments: |
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 ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 ); |
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(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* @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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* 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 | ||
*/ |
There was a problem hiding this comment.
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
.
for (i = 0; i < this._length; i++) { | ||
result = fcn.call(thisArg, buf[GETTER](i * BYTES_PER_ELEMENT, this._isLE), i, this); | ||
if (result) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
var result; |
return i; | ||
} | ||
} | ||
return -1; // Not found |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return -1; // Not found | |
return -1; |
t.end(); | ||
}); | ||
|
||
tape( 'attached to the prototype of the returned function is a `forEach` method', function test( t ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 ) { |
There was a problem hiding this 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.
@pranav-1720 Would you also mind resolving the merge conflict? Thanks! |
Resolves #3142.
Description
This pull request:
Related Issues
This pull request:
Questions
No.
Other
No.
Checklist
@stdlib-js/reviewers