From 0c0027f8a3abda70ce3e1f39fbccf998e582ba03 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Tue, 26 Nov 2024 04:21:59 +0000 Subject: [PATCH] Auto-generated commit --- CHANGELOG.md | 13 +- fixed-endian-factory/README.md | 50 ++++ .../benchmark/benchmark.some.js | 60 +++++ .../benchmark/benchmark.some.length.js | 113 +++++++++ fixed-endian-factory/lib/main.js | 31 +++ fixed-endian-factory/test/test.some.js | 214 ++++++++++++++++++ 6 files changed, 476 insertions(+), 5 deletions(-) create mode 100644 fixed-endian-factory/benchmark/benchmark.some.js create mode 100644 fixed-endian-factory/benchmark/benchmark.some.length.js create mode 100644 fixed-endian-factory/test/test.some.js diff --git a/CHANGELOG.md b/CHANGELOG.md index b88554cc..daffce00 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -480,6 +480,7 @@ This release closes the following issue: ##### Features +- [`2df0e9b`](https://github.com/stdlib-js/stdlib/commit/2df0e9bb08b8ad1c573b7c2383ef39e492dd5436) - add `some` method to `array/fixed-endian-factory` [(#3241)](https://github.com/stdlib-js/stdlib/pull/3241) - [`e3a2173`](https://github.com/stdlib-js/stdlib/commit/e3a2173a24bd8634f333cace626fc2d71740ebd3) - add `every` method to `array/fixed-endian-factory` [(#3200)](https://github.com/stdlib-js/stdlib/pull/3200) - [`b34732c`](https://github.com/stdlib-js/stdlib/commit/b34732cf655db60fbc798e12952f88c3edb07eaf) - add `at` method to `array/fixed-endian-factory` [(#3184)](https://github.com/stdlib-js/stdlib/pull/3184) - [`956a462`](https://github.com/stdlib-js/stdlib/commit/956a4624c788689b1bca285856b987ea3aa32eb6) - add `forEach` method @@ -492,9 +493,9 @@ This release closes the following issue: ##### Closed Issues -A total of 2 issues were closed in this release: +A total of 3 issues were closed in this release: -[#3135](https://github.com/stdlib-js/stdlib/issues/3135), [#3138](https://github.com/stdlib-js/stdlib/issues/3138) +[#3135](https://github.com/stdlib-js/stdlib/issues/3135), [#3138](https://github.com/stdlib-js/stdlib/issues/3138), [#3155](https://github.com/stdlib-js/stdlib/issues/3155) @@ -548,9 +549,9 @@ A total of 2 issues were closed in this release: ### Closed Issues -A total of 9 issues were closed in this release: +A total of 10 issues were closed in this release: -[#3135](https://github.com/stdlib-js/stdlib/issues/3135), [#3138](https://github.com/stdlib-js/stdlib/issues/3138), [#3164](https://github.com/stdlib-js/stdlib/issues/3164), [#3165](https://github.com/stdlib-js/stdlib/issues/3165), [#3176](https://github.com/stdlib-js/stdlib/issues/3176), [#3177](https://github.com/stdlib-js/stdlib/issues/3177), [#3178](https://github.com/stdlib-js/stdlib/issues/3178), [#3179](https://github.com/stdlib-js/stdlib/issues/3179), [#3180](https://github.com/stdlib-js/stdlib/issues/3180) +[#3135](https://github.com/stdlib-js/stdlib/issues/3135), [#3138](https://github.com/stdlib-js/stdlib/issues/3138), [#3155](https://github.com/stdlib-js/stdlib/issues/3155), [#3164](https://github.com/stdlib-js/stdlib/issues/3164), [#3165](https://github.com/stdlib-js/stdlib/issues/3165), [#3176](https://github.com/stdlib-js/stdlib/issues/3176), [#3177](https://github.com/stdlib-js/stdlib/issues/3177), [#3178](https://github.com/stdlib-js/stdlib/issues/3178), [#3179](https://github.com/stdlib-js/stdlib/issues/3179), [#3180](https://github.com/stdlib-js/stdlib/issues/3180) @@ -560,11 +561,12 @@ A total of 9 issues were closed in this release: ### Contributors -A total of 6 people contributed to this release. Thank you to the following contributors: +A total of 7 people contributed to this release. Thank you to the following contributors: - Aayush Khanna - Abhijit - Athan Reines +- Kshitij-Dale - Philipp Burckhardt - Rylan Yang - Vinit Pandit @@ -579,6 +581,7 @@ A total of 6 people contributed to this release. Thank you to the following cont
+- [`2df0e9b`](https://github.com/stdlib-js/stdlib/commit/2df0e9bb08b8ad1c573b7c2383ef39e492dd5436) - **feat:** add `some` method to `array/fixed-endian-factory` [(#3241)](https://github.com/stdlib-js/stdlib/pull/3241) _(by Kshitij-Dale, Athan Reines)_ - [`abb0dc3`](https://github.com/stdlib-js/stdlib/commit/abb0dc38783210623e67f19a5bb95b3998f75ff7) - **docs:** update examples and descriptions _(by Athan Reines)_ - [`95d0bbc`](https://github.com/stdlib-js/stdlib/commit/95d0bbc4b76c57dba4b4edd343dcd046490d4a51) - **bench:** test for primitive value _(by Athan Reines)_ - [`c712a8e`](https://github.com/stdlib-js/stdlib/commit/c712a8ec07db672a01355ddfca2192be25fe85ca) - **feat:** add `array/base/broadcasted-ternary4d` [(#3232)](https://github.com/stdlib-js/stdlib/pull/3232) _(by Vinit Pandit, Athan Reines)_ diff --git a/fixed-endian-factory/README.md b/fixed-endian-factory/README.md index 481f350b..85452ab8 100644 --- a/fixed-endian-factory/README.md +++ b/fixed-endian-factory/README.md @@ -535,6 +535,56 @@ A few notes: - If a target array cannot accommodate all values (i.e., the length of source array plus `i` exceeds the target array length), the method throws an error. - If provided a typed array which shares an [`ArrayBuffer`][@stdlib/array/buffer] with the target array, the method will intelligently copy the source range to the destination range. + + +#### TypedArrayFE.prototype.some( predicate\[, thisArg] ) + +Tests whether at least one element in an array passes a test implemented by a predicate function. + +```javascript +function isPositive( v ) { + return v > 0; +} + +var Float64ArrayFE = fixedEndianFactory( 'float64' ); + +var arr = new Float64ArrayFE( 'little-endian', [ -1.0, 2.0, -3.0, -4.0 ] ); +// returns + +var bool = arr.some( isPositive ); +// returns true +``` + +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 +function isPositive( v, i ) { + this.count += 1; + return v > 0; +} + +var Float64ArrayFE = fixedEndianFactory( 'float64' ); + +var arr = new Float64ArrayFE( 'little-endian', [ -1.0, -2.0, -3.0 ] ); +// returns + +var context = { + 'count': 0 +}; + +var bool = arr.some( isPositive, context ); +// returns false + +var count = context.count; +// returns 3 +``` + #### TypedArrayFE.prototype.toString() diff --git a/fixed-endian-factory/benchmark/benchmark.some.js b/fixed-endian-factory/benchmark/benchmark.some.js new file mode 100644 index 00000000..c0494cdf --- /dev/null +++ b/fixed-endian-factory/benchmark/benchmark.some.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 isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var factory = require( './../lib' ); + + +// VARIABLES // + +var Float64ArrayFE = factory( 'float64' ); + + +// MAIN // + +bench( pkg+':some', function benchmark( b ) { + var bool; + 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++ ) { + bool = arr.some( predicate ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); + + function predicate( v ) { + return v > 0; + } +}); diff --git a/fixed-endian-factory/benchmark/benchmark.some.length.js b/fixed-endian-factory/benchmark/benchmark.some.length.js new file mode 100644 index 00000000..6cb1b10b --- /dev/null +++ b/fixed-endian-factory/benchmark/benchmark.some.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( './../../zero-to' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).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 bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = arr.some( predicate ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + 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+':some:len='+len, f ); + } +} + +main(); diff --git a/fixed-endian-factory/lib/main.js b/fixed-endian-factory/lib/main.js index 64708668..ec0050c4 100644 --- a/fixed-endian-factory/lib/main.js +++ b/fixed-endian-factory/lib/main.js @@ -729,6 +729,37 @@ function factory( dtype ) { // eslint-disable-line max-lines-per-function, stdli buf[ SETTER ]( idx*BYTES_PER_ELEMENT, value, this._isLE ); }); + /** + * Tests whether at least one element in the typed array passes a test implemented by a predicate function. + * + * @name some + * @memberof TypedArray.prototype + * @type {Function} + * @param {Function} predicate - predicate function + * @param {*} [thisArg] - predicate function execution context + * @throws {TypeError} `this` must be a typed array instance + * @throws {TypeError} first argument must be a function + * @returns {boolean} boolean indicating whether at least one element passes a test + */ + setReadOnly( TypedArray.prototype, 'some', function some( predicate, thisArg ) { + var buf; + 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 = 0; i < this._length; i++ ) { + if ( predicate.call( thisArg, buf[ GETTER ]( i * BYTES_PER_ELEMENT, this._isLE ), i, this ) ) { + return true; + } + } + return false; + }); + /** * Serializes an array as a string. * diff --git a/fixed-endian-factory/test/test.some.js b/fixed-endian-factory/test/test.some.js new file mode 100644 index 00000000..8e0b118b --- /dev/null +++ b/fixed-endian-factory/test/test.some.js @@ -0,0 +1,214 @@ +/** +* @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 a `some` method', function test( t ) { + var ctor = factory( 'float64' ); + t.strictEqual( hasOwnProp( ctor.prototype, 'some' ), true, 'returns expected value' ); + t.strictEqual( isFunction( ctor.prototype.some ), 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', [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); + + 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.some.call( value, predicate ); + }; + } + function predicate( v ) { + return v < 0; + } +}); + +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', [ 1.0, 2.0, 3.0, 4.0 ] ); + + 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.some( value ); + }; + } +}); + +tape( 'the method returns `false` if operating on an empty array', function test( t ) { + var bool; + var ctor; + var arr; + + ctor = factory( 'float64' ); + arr = new ctor( 'little-endian' ); + bool = arr.some( predicate ); + + t.strictEqual( bool, false, 'returns expected value' ); + t.end(); + + function predicate() { + t.fail( 'should not be invoked' ); + } +}); + +tape( 'the method returns `true` if at least one element passes a test', function test( t ) { + var bool; + var ctor; + var arr; + + ctor = factory( 'float64' ); + arr = new ctor( 'little-endian', [ -1.0, -2.0, 3.0, -4.0, 5.0 ] ); + bool = arr.some( predicate ); + + t.strictEqual( bool, true, 'returns expected value' ); + t.end(); + + function predicate( v ) { + return v > 0; + } +}); + +tape( 'the method returns `false` if all elements fail a test', function test( t ) { + var bool; + var ctor; + var arr; + + ctor = factory( 'float64' ); + arr = new ctor( 'little-endian', [ -1.0, -2.0, -3.0, -4.0, -5.0 ] ); + bool = arr.some( predicate ); + + t.strictEqual( bool, false, 'returns expected value' ); + t.end(); + + function predicate( v ) { + return v > 0; + } +}); + +tape( 'the method supports providing an execution context', function test( t ) { + var bool; + var ctor; + var arr; + var ctx; + + ctor = factory( 'float64' ); + arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); + ctx = { + 'threshold': 3 + }; + bool = arr.some( predicate, ctx ); + + t.strictEqual( bool, true, 'returns expected value' ); + + t.end(); + + function predicate( v ) { + return v > this.threshold; // eslint-disable-line no-invalid-this + } +}); + +tape( 'the method stops executing upon encountering the first element which passes a test', function test( t ) { + var bool; + var ctor; + var arr; + var ctx; + + ctx = { + 'count': 0 + }; + + ctor = factory( 'float64' ); + arr = new ctor( 'little-endian', [ -1.0, -2.0, -3.0, 4.0, -5.0 ] ); + bool = arr.some( predicate, ctx ); + + t.strictEqual( bool, true, 'returns expected value' ); + t.strictEqual( ctx.count, 4, 'returns expected value' ); + + t.end(); + + function predicate( v ) { + this.count += 1; // eslint-disable-line no-invalid-this + return v > 0; + } +});