From 8fb6a86917a144992d04186e581b94d005b5619b Mon Sep 17 00:00:00 2001 From: ahmad-kashkoush Date: Sun, 1 Dec 2024 19:05:01 +0200 Subject: [PATCH 01/10] feat: add logic, tests, and benchmarks for findLast method --- .../benchmark/benchmark.find_last.js | 60 ++++++ .../benchmark/benchmark.find_last.length.js | 113 +++++++++++ .../array/fixed-endian-factory/lib/main.js | 33 +++ .../test/test.find_last.js | 191 ++++++++++++++++++ 4 files changed, 397 insertions(+) create mode 100644 lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.find_last.js create mode 100644 lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.find_last.length.js create mode 100644 lib/node_modules/@stdlib/array/fixed-endian-factory/test/test.find_last.js diff --git a/lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.find_last.js b/lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.find_last.js new file mode 100644 index 000000000000..5055dc15cbae --- /dev/null +++ b/lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.find_last.js @@ -0,0 +1,60 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var factory = require( './../lib' ); + + +// VARIABLES // + +var Float64ArrayFE = factory( 'float64' ); + + +// MAIN // + +bench( pkg+':findLast', function benchmark( b ) { + var out; + var arr; + var i; + + arr = new Float64ArrayFE( 'little-endian', [ 1.0, 2.0, 2.0, 1.0 ] ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = arr.every( predicate ); + if ( typeof out !== 'number' ) { + b.fail( 'should return a number' ); + } + } + b.toc(); + if ( !isNumber( out ) ) { + b.fail( 'should return a number' ); + } + b.pass( 'benchmark finished' ); + b.end(); + + function predicate( v ) { + return v > 0; + } +}); diff --git a/lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.find_last.length.js b/lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.find_last.length.js new file mode 100644 index 000000000000..7861446be1ba --- /dev/null +++ b/lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.find_last.length.js @@ -0,0 +1,113 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var zeroTo = require( '@stdlib/array/zero-to' ); +var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var factory = require( './../lib' ); + + +// VARIABLES // + +var Float64ArrayFE = factory( 'float64' ); + + +// FUNCTIONS // + +/** +* Predicate function. +* +* @private +* @param {number} value - array element +* @param {NonNegativeInteger} idx - array element index +* @param {TypedArray} arr - array instance +* @returns {boolean} boolean indicating whether a value passes a test +*/ +function predicate( value ) { + return value > 0; +} + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var arr = new Float64ArrayFE( 'little-endian', zeroTo( len ) ); + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var out; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = arr.findLast( predicate ); + if ( typeof out !== 'number' ) { + b.fail( 'should return a number' ); + } + } + b.toc(); + if ( !isNumber( out ) ) { + b.fail( 'should return a number' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( pkg+':findLast:len='+len, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js b/lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js index a8dbe8ea235b..e1677db8450c 100644 --- a/lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js +++ b/lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js @@ -1039,6 +1039,39 @@ function factory( dtype ) { // eslint-disable-line max-lines-per-function, stdli return out.join( ',' ); }); + /** + * Returns the last element in an array for which a predicate function returns a truthy value. + * + * @private + * @name findLast + * @memberof TypedArray.prototype + * @type {Function} + * @param {Function} predicate - test function + * @param {*} [thisArg] - predicate function execution context + * @throws {TypeError} `this` must be a typed array + * @throws {TypeError} first argument must be a function + * @returns {(*|void)} array element or undefined + */ + setReadOnly( TypedArray.prototype, 'findLast', function findLast( predicate, thisArg ) { + var buf; + var v; + var i; + + if ( !isTypedArray( this ) ) { + throw new TypeError( format( 'invalid invocation. `this` is not %s %s.', CHAR2ARTICLE[ dtype[0] ], CTOR_NAME ) ); + } + if ( !isFunction( predicate ) ) { + throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', predicate ) ); + } + buf = this._buffer; + for ( i = this._length-1; i >= 0; i-- ) { + v = buf[ GETTER ]( i * BYTES_PER_ELEMENT, this._isLE ); + if ( predicate.call( thisArg, v, i, this ) ) { + return v; + } + } + }); + /** * Serializes the array elements into a string, with elements separated by the specified `separator`. * diff --git a/lib/node_modules/@stdlib/array/fixed-endian-factory/test/test.find_last.js b/lib/node_modules/@stdlib/array/fixed-endian-factory/test/test.find_last.js new file mode 100644 index 000000000000..1a1363071d38 --- /dev/null +++ b/lib/node_modules/@stdlib/array/fixed-endian-factory/test/test.find_last.js @@ -0,0 +1,191 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var isFunction = require( '@stdlib/assert/is-function' ); +var factory = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof factory, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns a function', function test( t ) { + var ctor = factory( 'float64' ); + t.strictEqual( isFunction( ctor ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'attached to the prototype of the returned function is an `findLast` method', function test( t ) { + var ctor = factory( 'float64' ); + t.strictEqual( hasOwnProp( ctor.prototype, 'findLast' ), true, 'returns expected value' ); + t.strictEqual( isFunction( ctor.prototype.findLast ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the method throws an error if invoked with a `this` context which is not a typed array instance', function test( t ) { + var values; + var ctor; + var arr; + var i; + + ctor = factory( 'float64' ); + arr = new ctor( 'little-endian', 5 ); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.findLast.call( value, predicate ); + }; + } + + function predicate( v ) { + return ( v === false ); + } +}); + +tape( 'the method throws an error if provided a first argument which is not a function', function test( t ) { + var values; + var ctor; + var arr; + var i; + + ctor = factory( 'float64' ); + arr = new ctor( 'little-endian', 5 ); + + values = [ + '5', + 3.14, + NaN, + true, + false, + null, + void 0, + {}, + [] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.findLast( value ); + }; + } +}); + +tape( 'the method returns `undefined` if operating on an empty typed array', function test( t ) { + var ctor; + var arr; + var v; + + ctor = factory( 'float64' ); + arr = new ctor( 'little-endian' ); + v = arr.findLast( predicate ); + + t.strictEqual( v, void 0, 'returns expected value' ); + t.end(); + + function predicate() { + t.fail( 'should not be invoked' ); + } +}); + +tape( 'the method returns the last element which passes a test', function test( t ) { + var ctor; + var arr; + var v; + ctor = factory( 'float64' ); + arr = new ctor( 'little-endian', [ -1.0, -2.0, -3.0, 1.0, 2.0, 3.0 ] ); + v = arr.findLast( predicate ); + + t.strictEqual( v, -3.0, 'returns expected value' ); + t.end(); + + function predicate( v ) { + return ( v < 0 ); + } +}); + +tape( 'the method returns `undefined` if all elements fail a test', function test( t ) { + var ctor; + var arr; + var v; + + ctor = factory( 'float64' ); + arr = new ctor( 'little-endian', [ -1.0, -2.0, -3.0 ] ); + v = arr.findLast( predicate ); + + t.strictEqual( v, void 0, 'returns expected value' ); + t.end(); + + function predicate( v ) { + return ( v > 0 ); + } +}); + +tape( 'the method supports providing an execution context', function test( t ) { + var ctor; + var ctx; + var arr; + var v; + + ctx = { + 'count': 0 + }; + ctor = factory( 'float64' ); + arr = new ctor( 'little-endian', [ -1.0, 1.0, 2.0 ] ); + v = arr.findLast( predicate, ctx ); + + t.strictEqual( v, -1.0, 'returns expected value' ); + t.strictEqual( ctx.count, 3, 'returns expected value' ); + + t.end(); + + function predicate( v ) { + this.count += 1; // eslint-disable-line no-invalid-this + return ( v < 0 ); + } +}); From f6c252842eb7933860543aa2473ca053148dd0b4 Mon Sep 17 00:00:00 2001 From: ahmad-kashkoush Date: Sun, 1 Dec 2024 19:08:03 +0200 Subject: [PATCH 02/10] docs: add documentation for findLast method --- .../array/fixed-endian-factory/README.md | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/lib/node_modules/@stdlib/array/fixed-endian-factory/README.md b/lib/node_modules/@stdlib/array/fixed-endian-factory/README.md index c1824d76f4f2..0cf3e6885543 100644 --- a/lib/node_modules/@stdlib/array/fixed-endian-factory/README.md +++ b/lib/node_modules/@stdlib/array/fixed-endian-factory/README.md @@ -908,6 +908,52 @@ var str = arr.join( 0 ); // returns '10203' ``` + + +#### TypedArray.prototype.findLast( predicate\[, thisArg] ) + +Returns the last element in an array for which a predicate function returns a truthy value. + +```javascript +var Float64ArrayFE = fixedEndianFactory( 'float64' ); +function predicate( element ) { + return element < 0; +} + +var arr = new Float64ArrayFE( 'little-endian', [ -1.0, 2.0, -4.0, 3.0 ] ); + +var res = arr.findLast( predicate ); +// returns -4.0 +``` + +The `predicate` function is provided three arguments: + +- **value**: current array element. +- **index**: current array element index. +- **arr**: the array on which this method was called. + +To set the function execution context, provide a `thisArg` + +```javascript +var Float64ArrayFE = fixedEndianFactory( 'float64' ); +function predicate( v ) { + this.count += 1; + return ( v < 0 ); +} + +var arr = new Float64ArrayFE( 'little-endian', [ -1.0, 2.0, 3.0 ] ); + +var context = { + 'count': 0 +}; + +var z = arr.findLast( predicate, context ); +// returns -1.0 + +var count = context.count; +// returns 3 +``` + From 2990ae034634c15e2c365bebecbce3bf824d6780 Mon Sep 17 00:00:00 2001 From: Ahmed_Kashkoush <89735230+ahmad-kashkoush@users.noreply.github.com> Date: Wed, 4 Dec 2024 11:00:24 +0200 Subject: [PATCH 03/10] docs: update Readme Co-authored-by: Athan Signed-off-by: Ahmed_Kashkoush <89735230+ahmad-kashkoush@users.noreply.github.com> --- lib/node_modules/@stdlib/array/fixed-endian-factory/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/node_modules/@stdlib/array/fixed-endian-factory/README.md b/lib/node_modules/@stdlib/array/fixed-endian-factory/README.md index 0cf3e6885543..1e2d7a7da7e2 100644 --- a/lib/node_modules/@stdlib/array/fixed-endian-factory/README.md +++ b/lib/node_modules/@stdlib/array/fixed-endian-factory/README.md @@ -916,6 +916,7 @@ Returns the last element in an array for which a predicate function returns a tr ```javascript var Float64ArrayFE = fixedEndianFactory( 'float64' ); + function predicate( element ) { return element < 0; } From 16ba57824f0b7268a8b9236d35abade3ae88b5e5 Mon Sep 17 00:00:00 2001 From: Ahmed_Kashkoush <89735230+ahmad-kashkoush@users.noreply.github.com> Date: Wed, 4 Dec 2024 11:00:59 +0200 Subject: [PATCH 04/10] docs: update Readme Co-authored-by: Athan Signed-off-by: Ahmed_Kashkoush <89735230+ahmad-kashkoush@users.noreply.github.com> --- lib/node_modules/@stdlib/array/fixed-endian-factory/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/node_modules/@stdlib/array/fixed-endian-factory/README.md b/lib/node_modules/@stdlib/array/fixed-endian-factory/README.md index 1e2d7a7da7e2..6e408992a544 100644 --- a/lib/node_modules/@stdlib/array/fixed-endian-factory/README.md +++ b/lib/node_modules/@stdlib/array/fixed-endian-factory/README.md @@ -937,6 +937,7 @@ To set the function execution context, provide a `thisArg` ```javascript var Float64ArrayFE = fixedEndianFactory( 'float64' ); + function predicate( v ) { this.count += 1; return ( v < 0 ); From be75be493c123b44915932a40af093f6c9fc5b27 Mon Sep 17 00:00:00 2001 From: Ahmed_Kashkoush <89735230+ahmad-kashkoush@users.noreply.github.com> Date: Wed, 4 Dec 2024 11:01:30 +0200 Subject: [PATCH 05/10] docs: update Readme Co-authored-by: Athan Signed-off-by: Ahmed_Kashkoush <89735230+ahmad-kashkoush@users.noreply.github.com> --- lib/node_modules/@stdlib/array/fixed-endian-factory/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/array/fixed-endian-factory/README.md b/lib/node_modules/@stdlib/array/fixed-endian-factory/README.md index 6e408992a544..b5de0031075d 100644 --- a/lib/node_modules/@stdlib/array/fixed-endian-factory/README.md +++ b/lib/node_modules/@stdlib/array/fixed-endian-factory/README.md @@ -918,7 +918,7 @@ Returns the last element in an array for which a predicate function returns a tr var Float64ArrayFE = fixedEndianFactory( 'float64' ); function predicate( element ) { - return element < 0; + return element < 0.0; } var arr = new Float64ArrayFE( 'little-endian', [ -1.0, 2.0, -4.0, 3.0 ] ); From f30faa8f07042ac29606ef4d7327c1f71ecfe156 Mon Sep 17 00:00:00 2001 From: Ahmed_Kashkoush <89735230+ahmad-kashkoush@users.noreply.github.com> Date: Wed, 4 Dec 2024 11:01:46 +0200 Subject: [PATCH 06/10] docs: update Readme Co-authored-by: Athan Signed-off-by: Ahmed_Kashkoush <89735230+ahmad-kashkoush@users.noreply.github.com> --- lib/node_modules/@stdlib/array/fixed-endian-factory/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/array/fixed-endian-factory/README.md b/lib/node_modules/@stdlib/array/fixed-endian-factory/README.md index b5de0031075d..d20e25554b9b 100644 --- a/lib/node_modules/@stdlib/array/fixed-endian-factory/README.md +++ b/lib/node_modules/@stdlib/array/fixed-endian-factory/README.md @@ -940,7 +940,7 @@ var Float64ArrayFE = fixedEndianFactory( 'float64' ); function predicate( v ) { this.count += 1; - return ( v < 0 ); + return ( v < 0.0 ); } var arr = new Float64ArrayFE( 'little-endian', [ -1.0, 2.0, 3.0 ] ); From 054914423c1f8e6ede2b327e3a66212c0cc91486 Mon Sep 17 00:00:00 2001 From: Ahmed_Kashkoush <89735230+ahmad-kashkoush@users.noreply.github.com> Date: Wed, 4 Dec 2024 11:02:10 +0200 Subject: [PATCH 07/10] docs: update Readme Co-authored-by: Athan Signed-off-by: Ahmed_Kashkoush <89735230+ahmad-kashkoush@users.noreply.github.com> --- .../array/fixed-endian-factory/benchmark/benchmark.find_last.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.find_last.js b/lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.find_last.js index 5055dc15cbae..157c16ee5b24 100644 --- a/lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.find_last.js +++ b/lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.find_last.js @@ -55,6 +55,6 @@ bench( pkg+':findLast', function benchmark( b ) { b.end(); function predicate( v ) { - return v > 0; + return v > 0.0; } }); From e5b01b15bf653a8db2c035449e01e6ca0a4ac910 Mon Sep 17 00:00:00 2001 From: Ahmed_Kashkoush <89735230+ahmad-kashkoush@users.noreply.github.com> Date: Wed, 4 Dec 2024 11:02:46 +0200 Subject: [PATCH 08/10] refactor: update benchmark predicate function Co-authored-by: Athan Signed-off-by: Ahmed_Kashkoush <89735230+ahmad-kashkoush@users.noreply.github.com> --- .../benchmark/benchmark.find_last.length.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.find_last.length.js b/lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.find_last.length.js index 7861446be1ba..e0a997652adb 100644 --- a/lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.find_last.length.js +++ b/lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.find_last.length.js @@ -45,7 +45,7 @@ var Float64ArrayFE = factory( 'float64' ); * @returns {boolean} boolean indicating whether a value passes a test */ function predicate( value ) { - return value > 0; + return value < 0.0; } /** From e8f9e7c1d927b5d649aa566cfa865e7af54fe439 Mon Sep 17 00:00:00 2001 From: ahmad-kashkoush Date: Wed, 4 Dec 2024 11:04:49 +0200 Subject: [PATCH 09/10] fix: correct benchmark method name --- .../array/fixed-endian-factory/benchmark/benchmark.find_last.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.find_last.js b/lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.find_last.js index 157c16ee5b24..f427a757616c 100644 --- a/lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.find_last.js +++ b/lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.find_last.js @@ -42,7 +42,7 @@ bench( pkg+':findLast', function benchmark( b ) { b.tic(); for ( i = 0; i < b.iterations; i++ ) { - out = arr.every( predicate ); + out = arr.findLast( predicate ); if ( typeof out !== 'number' ) { b.fail( 'should return a number' ); } From 5ab8479ad34fad7d56f721089b23ea0c51955cb3 Mon Sep 17 00:00:00 2001 From: ahmad-kashkoush Date: Wed, 4 Dec 2024 12:52:50 +0200 Subject: [PATCH 10/10] refactor: put findLast function into correct order --- .../array/fixed-endian-factory/README.md | 98 +++++++++---------- .../array/fixed-endian-factory/lib/main.js | 66 ++++++------- 2 files changed, 82 insertions(+), 82 deletions(-) diff --git a/lib/node_modules/@stdlib/array/fixed-endian-factory/README.md b/lib/node_modules/@stdlib/array/fixed-endian-factory/README.md index 427cce366c0d..ba64846221fe 100644 --- a/lib/node_modules/@stdlib/array/fixed-endian-factory/README.md +++ b/lib/node_modules/@stdlib/array/fixed-endian-factory/README.md @@ -450,6 +450,54 @@ var count = context.count; // returns 4 ``` + + +#### TypedArray.prototype.findLast( predicate\[, thisArg] ) + +Returns the last element in an array for which a predicate function returns a truthy value. + +```javascript +var Float64ArrayFE = fixedEndianFactory( 'float64' ); + +function predicate( element ) { + return element < 0.0; +} + +var arr = new Float64ArrayFE( 'little-endian', [ -1.0, 2.0, -4.0, 3.0 ] ); + +var res = arr.findLast( predicate ); +// returns -4.0 +``` + +The `predicate` function is provided three arguments: + +- **value**: current array element. +- **index**: current array element index. +- **arr**: the array on which this method was called. + +To set the function execution context, provide a `thisArg` + +```javascript +var Float64ArrayFE = fixedEndianFactory( 'float64' ); + +function predicate( v ) { + this.count += 1; + return ( v < 0.0 ); +} + +var arr = new Float64ArrayFE( 'little-endian', [ -1.0, 2.0, 3.0 ] ); + +var context = { + 'count': 0 +}; + +var z = arr.findLast( predicate, context ); +// returns -1.0 + +var count = context.count; +// returns 3 +``` + #### TypedArrayFE.prototype.forEach( callbackFn\[, thisArg] ) @@ -919,59 +967,11 @@ var arr = new Float64ArrayFE( 'little-endian', [ 1.0, 2.0, 3.0 ] ); var out = arr.with( 0, 0.0 ); // returns - + var v = out.get( 0 ); // returns 0.0 ``` - - -#### TypedArray.prototype.findLast( predicate\[, thisArg] ) - -Returns the last element in an array for which a predicate function returns a truthy value. - -```javascript -var Float64ArrayFE = fixedEndianFactory( 'float64' ); - -function predicate( element ) { - return element < 0.0; -} - -var arr = new Float64ArrayFE( 'little-endian', [ -1.0, 2.0, -4.0, 3.0 ] ); - -var res = arr.findLast( predicate ); -// returns -4.0 -``` - -The `predicate` function is provided three arguments: - -- **value**: current array element. -- **index**: current array element index. -- **arr**: the array on which this method was called. - -To set the function execution context, provide a `thisArg` - -```javascript -var Float64ArrayFE = fixedEndianFactory( 'float64' ); - -function predicate( v ) { - this.count += 1; - return ( v < 0.0 ); -} - -var arr = new Float64ArrayFE( 'little-endian', [ -1.0, 2.0, 3.0 ] ); - -var context = { - 'count': 0 -}; - -var z = arr.findLast( predicate, context ); -// returns -1.0 - -var count = context.count; -// returns 3 -``` - diff --git a/lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js b/lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js index 82bf03991552..6e7e415c35d8 100644 --- a/lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js +++ b/lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js @@ -617,6 +617,39 @@ function factory( dtype ) { // eslint-disable-line max-lines-per-function, stdli return new this.constructor( flag2byteOrder( this._isLE ), out ); }); + /** + * Returns the last element in an array for which a predicate function returns a truthy value. + * + * @private + * @name findLast + * @memberof TypedArray.prototype + * @type {Function} + * @param {Function} predicate - test function + * @param {*} [thisArg] - predicate function execution context + * @throws {TypeError} `this` must be a typed array + * @throws {TypeError} first argument must be a function + * @returns {(*|void)} array element or undefined + */ + setReadOnly( TypedArray.prototype, 'findLast', function findLast( predicate, thisArg ) { + var buf; + var v; + var i; + + if ( !isTypedArray( this ) ) { + throw new TypeError( format( 'invalid invocation. `this` is not %s %s.', CHAR2ARTICLE[ dtype[0] ], CTOR_NAME ) ); + } + if ( !isFunction( predicate ) ) { + throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', predicate ) ); + } + buf = this._buffer; + for ( i = this._length-1; i >= 0; i-- ) { + v = buf[ GETTER ]( i * BYTES_PER_ELEMENT, this._isLE ); + if ( predicate.call( thisArg, v, i, this ) ) { + return v; + } + } + }); + /** * Invokes a function once for each array element. * @@ -1072,39 +1105,6 @@ function factory( dtype ) { // eslint-disable-line max-lines-per-function, stdli return out.join( ',' ); }); - /** - * Returns the last element in an array for which a predicate function returns a truthy value. - * - * @private - * @name findLast - * @memberof TypedArray.prototype - * @type {Function} - * @param {Function} predicate - test function - * @param {*} [thisArg] - predicate function execution context - * @throws {TypeError} `this` must be a typed array - * @throws {TypeError} first argument must be a function - * @returns {(*|void)} array element or undefined - */ - setReadOnly( TypedArray.prototype, 'findLast', function findLast( predicate, thisArg ) { - var buf; - var v; - var i; - - if ( !isTypedArray( this ) ) { - throw new TypeError( format( 'invalid invocation. `this` is not %s %s.', CHAR2ARTICLE[ dtype[0] ], CTOR_NAME ) ); - } - if ( !isFunction( predicate ) ) { - throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', predicate ) ); - } - buf = this._buffer; - for ( i = this._length-1; i >= 0; i-- ) { - v = buf[ GETTER ]( i * BYTES_PER_ELEMENT, this._isLE ); - if ( predicate.call( thisArg, v, i, this ) ) { - return v; - } - } - }); - /** * Returns a new typed array with the element at a provided index replaced with a provided value. *