From d7ca701c62352917bb6ede139e16a6f5a3bc4787 Mon Sep 17 00:00:00 2001 From: Shubham Singh Date: Tue, 27 Feb 2024 19:54:32 +0530 Subject: [PATCH 1/7] feat: add @stdlib/iter/cartesian-product --- .../@stdlib/iter/cartesian-product/README.md | 127 ++++++++++ .../cartesian-product/benchmark/benchmark.js | 51 ++++ .../benchmark/benchmark.length.js | 96 ++++++++ .../iter/cartesian-product/docs/repl.txt | 39 +++ .../cartesian-product/docs/types/index.d.ts | 60 +++++ .../iter/cartesian-product/docs/types/test.ts | 52 ++++ .../iter/cartesian-product/examples/index.js | 37 +++ .../iter/cartesian-product/lib/index.js | 51 ++++ .../iter/cartesian-product/lib/main.js | 137 +++++++++++ .../iter/cartesian-product/package.json | 66 +++++ .../iter/cartesian-product/test/test.js | 225 ++++++++++++++++++ 11 files changed, 941 insertions(+) create mode 100644 lib/node_modules/@stdlib/iter/cartesian-product/README.md create mode 100644 lib/node_modules/@stdlib/iter/cartesian-product/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/iter/cartesian-product/benchmark/benchmark.length.js create mode 100644 lib/node_modules/@stdlib/iter/cartesian-product/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/iter/cartesian-product/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/iter/cartesian-product/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/iter/cartesian-product/examples/index.js create mode 100644 lib/node_modules/@stdlib/iter/cartesian-product/lib/index.js create mode 100644 lib/node_modules/@stdlib/iter/cartesian-product/lib/main.js create mode 100644 lib/node_modules/@stdlib/iter/cartesian-product/package.json create mode 100644 lib/node_modules/@stdlib/iter/cartesian-product/test/test.js diff --git a/lib/node_modules/@stdlib/iter/cartesian-product/README.md b/lib/node_modules/@stdlib/iter/cartesian-product/README.md new file mode 100644 index 000000000000..930464f88b87 --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cartesian-product/README.md @@ -0,0 +1,127 @@ + + +# iterCartesianProduct + +> Return the [Cartesian product iterator][cartesian-product]. + +
+ +## Usage + +```javascript +var iterCartesianProduct = require('@stdlib/iter/cartesian-product'); +``` + +#### iterCartesianProduct( x1, x2 ) + +Returns the [Cartesian product][cartesian-product]. + +```javascript +var iterCartesianProduct = require('@stdlib/iter/cartesian-product'); +var x1 = [1, 2]; +var x2 = [3, 4]; + +var iter = iterCartesianProduct(x1, x2); + +// Perform manual iteration... +var v; +while ( true ) { + v = iter.next(); + if ( v.done ) { + break; + } + console.log( v.value ); +} +``` + +If provided one or more empty arrays, the function returns an empty array. + +```javascript +var iterCartesianProduct = require('@stdlib/iter/cartesian-product'); +var x1 = [1, 2, 3, 4]; +var x2 = []; + +var iter = iterCartesianProduct(x1, x2); +var v = iter.next().value; +// returns undefined +``` + +
+ + + +
+ +
+ + + +
+ +## Examples + + + +```javascript +var linspace = require('@stdlib/array/linspace'); +var iterCartesianProduct = require('@stdlib/iter/cartesian-product'); + +var x1 = linspace(0, 5, 6); +var x2 = linspace(10, 15, 6); + +var iter = iterCartesianProduct(x1, x2); +var v = iter.next().value; +// returns [ 0, 10 ] +v = iter.next().value; +// returns [ 1, 11 ].... +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/iter/cartesian-product/benchmark/benchmark.js b/lib/node_modules/@stdlib/iter/cartesian-product/benchmark/benchmark.js new file mode 100644 index 000000000000..3b1463fbd16c --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cartesian-product/benchmark/benchmark.js @@ -0,0 +1,51 @@ +/** +* @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 zeroTo = require( '@stdlib/array/zero-to' ); +var isIteratorLike = require( '@stdlib/assert/is-iterator-like' ); +var pkg = require( './../package.json' ).name; +var iterCartesianProduct = require( './../lib' ); + + +// MAIN // + +bench( pkg+':len=100', function benchmark( b ) { + var x; + var i; + var v; + + x = zeroTo( 100 ); + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = iterCartesianProduct( x, x ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( !isIteratorLike( v ) ) { + b.fail( 'should return an iterator protocol-compliant object' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/iter/cartesian-product/benchmark/benchmark.length.js b/lib/node_modules/@stdlib/iter/cartesian-product/benchmark/benchmark.length.js new file mode 100644 index 000000000000..58ab643862b7 --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cartesian-product/benchmark/benchmark.length.js @@ -0,0 +1,96 @@ +/** +* @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 discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var isIteratorLike = require( '@stdlib/assert/is-iterator-like' ); +var pkg = require( './../package.json' ).name; +var iterCartesianProduct = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var x = discreteUniform( len/2, 0, 3 ); + var y = [ 1, 2 ]; + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var v; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = iterCartesianProduct( x, y ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( !isIteratorLike( v ) ) { + b.fail( 'should return an iterator protocol-compliant object' ); + } + 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+':len='+len, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/iter/cartesian-product/docs/repl.txt b/lib/node_modules/@stdlib/iter/cartesian-product/docs/repl.txt new file mode 100644 index 000000000000..67d39e1fae9c --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cartesian-product/docs/repl.txt @@ -0,0 +1,39 @@ + +{{alias}}( x1, x2 ) + Returns an iterator which returns the cartesian product of the input + arrays. + + If an environment supports Symbol.iterator, the returned iterator is + iterable. + + Parameters + ---------- + x1: Collection + First input array. + + x2: Collection + Second input array. + + Returns + ------- + iterator: Object + Iterator. + + iterator.next(): Function + Returns an iterator protocol-compliant object containing the next + iterated value (if one exists) and a boolean flag indicating whether the + iterator is finished. + + iterator.return( [value] ): Function + Finishes an iterator and returns a provided value. + + Examples + -------- + > var it = {{alias}}( [0, 1], [2, 3]); + > var v = it.next().value + [0, 2] + > v = it.next().value + [0, 3] + + See Also + -------- diff --git a/lib/node_modules/@stdlib/iter/cartesian-product/docs/types/index.d.ts b/lib/node_modules/@stdlib/iter/cartesian-product/docs/types/index.d.ts new file mode 100644 index 000000000000..69a315174ed3 --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cartesian-product/docs/types/index.d.ts @@ -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. +*/ + +/* eslint-disable @typescript-eslint/no-explicit-any */ + +// TypeScript Version: 4.1 + +/// + +import { Iterator as Iter, IterableIterator } from '@stdlib/types/iter'; + +// Define a union type representing both iterable and non-iterable iterators: +type Iterator = Iter | IterableIterator; + +/** + * Returns an iterator which generates all combinations of elements from the input arrays. + * + * ## Notes + * + * - If an environment supports `Symbol.iterator`, the returned iterator is iterable. + * + * @param x1 - first input array + * @param x2 - second input array + * @returns iterator + * + * @example + * var iter = iterCartesianProduct( [ 1, 2 ], [ 3, 4 ] ); + * + * var v = iter.next().value; + * // returns [ 1, 3 ] + * + * v = iter.next().value; + * // returns [ 1, 4 ] + * + * v = iter.next().value; + * // returns [ 2, 3 ] + * + * // ... + */ +declare function iterCartesianProduct( x1: Array, x2: Array ): Iterator; + + +// EXPORTS // + +export = iterCartesianProduct; diff --git a/lib/node_modules/@stdlib/iter/cartesian-product/docs/types/test.ts b/lib/node_modules/@stdlib/iter/cartesian-product/docs/types/test.ts new file mode 100644 index 000000000000..8e110c84c4b7 --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cartesian-product/docs/types/test.ts @@ -0,0 +1,52 @@ +/* +* @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. +*/ + +import iterCartesianProduct = require( './index' ); + + +// TESTS // + +// The function returns an iterator... +{ + iterCartesianProduct( [ 1, 2, 3 ], [ 4, 5, 6 ] ); // $ExpectType Iterator +} + +// The compiler throws an error if the function is provided a first argument which is not array-like... +{ + iterCartesianProduct( 2, [ 4, 5, 6 ] ); // $ExpectError + iterCartesianProduct( false, [ 4, 5, 6 ] ); // $ExpectError + iterCartesianProduct( true, [ 4, 5, 6 ] ); // $ExpectError + iterCartesianProduct( {}, [ 4, 5, 6 ] ); // $ExpectError + iterCartesianProduct( ( x: number ): number => x, [ 4, 5, 6 ] ); // $ExpectError +} + +// The compiler throws an error if the function is provided a last argument which is not array-like... +{ + iterCartesianProduct( [ 1, 2, 3 ], 2 ); // $ExpectError + iterCartesianProduct( [ 1, 2, 3 ], false ); // $ExpectError + iterCartesianProduct( [ 1, 2, 3 ], true ); // $ExpectError + iterCartesianProduct( [ 1, 2, 3 ], {} ); // $ExpectError + iterCartesianProduct( [ 1, 2, 3 ], ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + iterCartesianProduct(); // $ExpectError + iterCartesianProduct( [ 1, 2, 3 ] ); // $ExpectError + iterCartesianProduct( [ 1, 2, 3 ], [ 4, 5, 6 ], 2 ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/iter/cartesian-product/examples/index.js b/lib/node_modules/@stdlib/iter/cartesian-product/examples/index.js new file mode 100644 index 000000000000..7147dfc2160a --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cartesian-product/examples/index.js @@ -0,0 +1,37 @@ +/** +* @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'; + +var linspace = require('@stdlib/array/linspace'); +var iterCartesianProduct = require('./../lib'); + +var x1 = linspace(0, 5, 6); +var x2 = linspace(10, 15, 6); + +var iter = iterCartesianProduct(x1, x2); + +// Perform manual iteration... +var v; +while (true) { + v = iter.next(); + if (v.done) { + break; + } + console.log(v.value); +} diff --git a/lib/node_modules/@stdlib/iter/cartesian-product/lib/index.js b/lib/node_modules/@stdlib/iter/cartesian-product/lib/index.js new file mode 100644 index 000000000000..18ec9a7a5360 --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cartesian-product/lib/index.js @@ -0,0 +1,51 @@ +/** +* @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'; + +/** +* Create an iterator which generates a Cartesian product of input arrays. +* +* @module @stdlib/iter/cartesian-product +* +* @example +* var iterCartesianProduct = require( '@stdlib/iter/cartesian-product' ); +* +* var iter = iterCartesianProduct([1, 2], [3, 4]); +* +* var v = iter.next().value; +* // returns [1,3] +* +* v = iter.next().value; +* // returns [1,4] +* +* v = iter.next().value; +* // returns [2,3] +* +* v = iter.next().value; +* // returns [2,4] +*/ + +// MODULES // + +var main = require('./main.js'); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/iter/cartesian-product/lib/main.js b/lib/node_modules/@stdlib/iter/cartesian-product/lib/main.js new file mode 100644 index 000000000000..0b0c0999f2c9 --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cartesian-product/lib/main.js @@ -0,0 +1,137 @@ +/** +* @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 setReadOnly = require('@stdlib/utils/define-nonenumerable-read-only-property'); +var isCollection = require('@stdlib/assert/is-collection'); +var base = require('@stdlib/array/base/cartesian-product'); +var iteratorSymbol = require('@stdlib/symbol/iterator'); +var format = require('@stdlib/string/format'); + + +// MAIN // + +/** +* Returns an Iterator of Cartesian product of arrays. +* +* @param {Collection} x1 - first input array +* @param {Collection} x2 - second input array +* @throws {TypeError} first argument must be a collection +* @throws {TypeError} second argument must be a collection +* @returns {Iterator} iterator +* +* @example +* var iter = iterCartesianProduct([1, 2], [3, 4]); +* +* var v = iter.next().value; +* // returns [1,3] +* +* v = iter.next().value; +* // returns [1,4] +* +* v = iter.next().value; +* // returns [2,3] +* +* v = iter.next().value; +* // returns [2,4] +*/ +function iterCartesianProduct(x1, x2) { + var value; + var iter; + var prod; + var i; + + if (!isCollection(x1)) { + throw new TypeError(format('invalid argument. First argument must be a collection. Value: `' + x1 + '`.')); + } + if (!isCollection(x2)) { + throw new TypeError(format('invalid argument. Second argument must be a collection. Value: `' + x2 +'`.')); + } + + prod = base(x1, x2); + + // Create an iterator protocol-compliant object: + iter = {}; + i = 0; + setReadOnly(iter, 'next', next); + setReadOnly(iter, 'return', end); + + // If an environment supports `Symbol.iterator`, make the iterator iterable: + if (iteratorSymbol) { + setReadOnly(iter, iteratorSymbol, factory); + } + return iter; + + /** + * Returns an iterator protocol-compliant object containing the next iterated value. + * + * @private + * @returns {Object} iterator protocol-compliant object + */ + function next() { + if (i >= prod.length) { + return { + 'done': true + }; + } + value = prod[i]; + i += 1; + return { + 'value': value, + 'done': false + }; + } + + /** + * Finishes an iterator. + * + * @private + * @param {*} [value] - value to return + * @returns {Object} iterator protocol-compliant object + */ + function end(value) { + i = prod.length; + if (arguments.length) { + return { + 'value': value, + 'done': true + }; + } + return { + 'done': true + }; + } + + /** + * Returns a new iterator. + * + * @private + * @returns {Iterator} iterator + */ + function factory() { + return iterCartesianProduct(x1, x2); + } +} + + +// EXPORTS // + +module.exports = iterCartesianProduct; diff --git a/lib/node_modules/@stdlib/iter/cartesian-product/package.json b/lib/node_modules/@stdlib/iter/cartesian-product/package.json new file mode 100644 index 000000000000..27207e8beace --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cartesian-product/package.json @@ -0,0 +1,66 @@ +{ + "name": "@stdlib/iter/cartesian-product", + "version": "0.0.0", + "description": "Return the Cartesian product iterator.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "utilities", + "utils", + "generic", + "array", + "cartesian", + "product", + "iterator", + "sets", + "math", + "mathematics", + "ordered" + ] + } + \ No newline at end of file diff --git a/lib/node_modules/@stdlib/iter/cartesian-product/test/test.js b/lib/node_modules/@stdlib/iter/cartesian-product/test/test.js new file mode 100644 index 000000000000..013f6d812a62 --- /dev/null +++ b/lib/node_modules/@stdlib/iter/cartesian-product/test/test.js @@ -0,0 +1,225 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2019 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 toAccessorArray = require('@stdlib/array/base/to-accessor-array'); +var iterCartesianProduct = require('./../lib'); + + +// TESTS // + +tape('main export is a function', function test(t) { + t.ok(true, __filename); + t.strictEqual(typeof iterCartesianProduct, 'function', 'main export is a function'); + t.end(); +}); + +tape('the function throws an error if provided a first argument which is not a collection', function test(t) { + var values; + var i; + + 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() { + iterCartesianProduct(value, [1, 2]); + }; + } +}); + +tape('the function throws an error if provided a second argument which is not a collection', function test(t) { + var values; + var i; + + 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() { + iterCartesianProduct([1, 2], value); + }; + } +}); + +tape('the function returns the Cartesian product iterator (indexed)', function test(t) { + var iterator; + var expected; + var actual; + var i; + + iterator = iterCartesianProduct([1, 2], [3, 4]); + + expected = [ + { + 'value': [1, 3], + 'done': false + }, + { + 'value': [1, 4], + 'done': false + }, + { + 'value': [2, 3], + 'done': false + }, + { + 'value': [2, 4], + 'done': false + }, + { + 'done': true + } + ]; + + actual = []; + + for (i = 0; i < expected.length; i++) { + actual.push(iterator.next()); + } + + t.deepEqual(actual, expected, 'returns expected value'); + t.end(); +}); + +tape('the function returns a empty iterator if either of the input iterators are empty (indexed)', function test(t) { + var iterator; + var expected; + var actual; + var i; + + iterator = iterCartesianProduct([], [3, 4]); + expected = [{ + 'done': true + }]; + actual = []; + for (i = 0; i < expected.length; i++) { + actual.push(iterator.next()); + } + t.deepEqual(actual, expected, 'returns expected value'); + + iterator = iterCartesianProduct([1, 2], []); + expected = [{ + 'done': true + }]; + actual = []; + for (i = 0; i < expected.length; i++) { + actual.push(iterator.next()); + } + t.deepEqual(actual, expected, 'returns expected value'); + + iterator = iterCartesianProduct([], []); + expected = [{ + 'done': true + }]; + actual = []; + for (i = 0; i < expected.length; i++) { + actual.push(iterator.next()); + } + t.deepEqual(actual, expected, 'returns expected value'); + t.end(); +}); + +tape('the function returns the Cartesian product iterator (accessors)', function test(t) { + var expected; + var actual; + var it; + var i; + + it = iterCartesianProduct(toAccessorArray([1, 2]), toAccessorArray([3, 4])); + + expected = [ + { + 'value': [1, 3], + 'done': false + }, + { + 'value': [1, 4], + 'done': false + }, + { + 'value': [2, 3], + 'done': false + }, + { + 'value': [2, 4], + 'done': false + }, + { + 'done': true + } + ]; + + actual = []; + + for (i = 0; i < expected.length; i++) { + actual.push(it.next()); + } + + t.deepEqual(actual, expected, 'returns expected value'); + t.end(); +}); + +tape('the function returns a empty iterator if either of the input iterators are empty (accessors)', function test(t) { + var expected; + var actual; + var it; + var i; + + it = iterCartesianProduct(toAccessorArray([]), toAccessorArray([3, 4])); + expected = [{ + 'done': true + }]; + actual = []; + + for (i = 0; i < expected.length; i++) { + actual.push(it.next()); + } + t.deepEqual(actual, expected, 'returns expected value'); + + it = iterCartesianProduct(toAccessorArray([1, 2]), toAccessorArray([])); + expected = [{ + 'done': true + }]; + actual = []; + for (i = 0; i < expected.length; i++) { + actual.push(it.next()); + } + t.deepEqual(actual, expected, 'returns expected value'); + + it = iterCartesianProduct(toAccessorArray([]), toAccessorArray([])); + expected = [{ + 'done': true + }]; + actual = []; + for (i = 0; i < expected.length; i++) { + actual.push(it.next()); + } + t.deepEqual(actual, expected, 'returns expected value'); + t.end(); +}); From 061e26d62a89598c95d660e76f21cb623199deaa Mon Sep 17 00:00:00 2001 From: Shubham Singh Date: Thu, 29 Feb 2024 02:39:19 +0530 Subject: [PATCH 2/7] feat: added space before parenthesis, removed benchmark.length.js --- .../benchmark/benchmark.length.js | 96 ------------------- .../iter/cartesian-product/lib/index.js | 2 +- .../iter/cartesian-product/lib/main.js | 28 +++--- .../iter/cartesian-product/test/test.js | 18 +++- 4 files changed, 29 insertions(+), 115 deletions(-) delete mode 100644 lib/node_modules/@stdlib/iter/cartesian-product/benchmark/benchmark.length.js diff --git a/lib/node_modules/@stdlib/iter/cartesian-product/benchmark/benchmark.length.js b/lib/node_modules/@stdlib/iter/cartesian-product/benchmark/benchmark.length.js deleted file mode 100644 index 58ab643862b7..000000000000 --- a/lib/node_modules/@stdlib/iter/cartesian-product/benchmark/benchmark.length.js +++ /dev/null @@ -1,96 +0,0 @@ -/** -* @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 discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); -var isIteratorLike = require( '@stdlib/assert/is-iterator-like' ); -var pkg = require( './../package.json' ).name; -var iterCartesianProduct = require( './../lib' ); - - -// FUNCTIONS // - -/** -* Creates a benchmark function. -* -* @private -* @param {PositiveInteger} len - array length -* @returns {Function} benchmark function -*/ -function createBenchmark( len ) { - var x = discreteUniform( len/2, 0, 3 ); - var y = [ 1, 2 ]; - return benchmark; - - /** - * Benchmark function. - * - * @private - * @param {Benchmark} b - benchmark instance - */ - function benchmark( b ) { - var v; - var i; - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - v = iterCartesianProduct( x, y ); - if ( typeof v !== 'object' ) { - b.fail( 'should return an object' ); - } - } - b.toc(); - if ( !isIteratorLike( v ) ) { - b.fail( 'should return an iterator protocol-compliant object' ); - } - 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+':len='+len, f ); - } -} - -main(); diff --git a/lib/node_modules/@stdlib/iter/cartesian-product/lib/index.js b/lib/node_modules/@stdlib/iter/cartesian-product/lib/index.js index 18ec9a7a5360..1e2a4ceb48ee 100644 --- a/lib/node_modules/@stdlib/iter/cartesian-product/lib/index.js +++ b/lib/node_modules/@stdlib/iter/cartesian-product/lib/index.js @@ -43,7 +43,7 @@ // MODULES // -var main = require('./main.js'); +var main = require( './main.js' ); // EXPORTS // diff --git a/lib/node_modules/@stdlib/iter/cartesian-product/lib/main.js b/lib/node_modules/@stdlib/iter/cartesian-product/lib/main.js index 0b0c0999f2c9..5460d70065fe 100644 --- a/lib/node_modules/@stdlib/iter/cartesian-product/lib/main.js +++ b/lib/node_modules/@stdlib/iter/cartesian-product/lib/main.js @@ -53,30 +53,30 @@ var format = require('@stdlib/string/format'); * v = iter.next().value; * // returns [2,4] */ -function iterCartesianProduct(x1, x2) { +function iterCartesianProduct( x1, x2 ) { var value; var iter; var prod; var i; - if (!isCollection(x1)) { - throw new TypeError(format('invalid argument. First argument must be a collection. Value: `' + x1 + '`.')); + if ( !isCollection( x1 ) ) { + throw new TypeError( format( 'invalid argument. First argument must be a collection. Value: `' + x1 + '`.' ) ); } - if (!isCollection(x2)) { - throw new TypeError(format('invalid argument. Second argument must be a collection. Value: `' + x2 +'`.')); + if ( !isCollection( x2 ) ) { + throw new TypeError( format( 'invalid argument. Second argument must be a collection. Value: `' + x2 +'`.' ) ); } - prod = base(x1, x2); + prod = base( x1, x2 ); // Create an iterator protocol-compliant object: iter = {}; i = 0; - setReadOnly(iter, 'next', next); - setReadOnly(iter, 'return', end); + setReadOnly( iter, 'next', next ); + setReadOnly( iter, 'return', end ); // If an environment supports `Symbol.iterator`, make the iterator iterable: - if (iteratorSymbol) { - setReadOnly(iter, iteratorSymbol, factory); + if ( iteratorSymbol ) { + setReadOnly( iter, iteratorSymbol, factory ); } return iter; @@ -87,7 +87,7 @@ function iterCartesianProduct(x1, x2) { * @returns {Object} iterator protocol-compliant object */ function next() { - if (i >= prod.length) { + if ( i >= prod.length ) { return { 'done': true }; @@ -107,9 +107,9 @@ function iterCartesianProduct(x1, x2) { * @param {*} [value] - value to return * @returns {Object} iterator protocol-compliant object */ - function end(value) { + function end( value ) { i = prod.length; - if (arguments.length) { + if ( arguments.length ) { return { 'value': value, 'done': true @@ -127,7 +127,7 @@ function iterCartesianProduct(x1, x2) { * @returns {Iterator} iterator */ function factory() { - return iterCartesianProduct(x1, x2); + return iterCartesianProduct( x1, x2 ); } } diff --git a/lib/node_modules/@stdlib/iter/cartesian-product/test/test.js b/lib/node_modules/@stdlib/iter/cartesian-product/test/test.js index 013f6d812a62..dd64c2867a05 100644 --- a/lib/node_modules/@stdlib/iter/cartesian-product/test/test.js +++ b/lib/node_modules/@stdlib/iter/cartesian-product/test/test.js @@ -20,9 +20,9 @@ // MODULES // -var tape = require('tape'); -var toAccessorArray = require('@stdlib/array/base/to-accessor-array'); -var iterCartesianProduct = require('./../lib'); +var tape = require( 'tape' ); +var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +var iterCartesianProduct = require( './../lib' ); // TESTS // @@ -54,7 +54,17 @@ tape('the function throws an error if provided a second argument which is not a var values; var i; - values = ['5', 5, NaN, true, false, null, void 0, {}, function noop() {}]; + 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]); } From 16d7e169ec7324d8d9ebb294debb82b026d69ca5 Mon Sep 17 00:00:00 2001 From: Shubham Singh Date: Thu, 29 Feb 2024 02:53:37 +0530 Subject: [PATCH 3/7] feat: new benchmark added --- .../cartesian-product/benchmark/benchmark.js | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/iter/cartesian-product/benchmark/benchmark.js b/lib/node_modules/@stdlib/iter/cartesian-product/benchmark/benchmark.js index 3b1463fbd16c..468e70b27d9b 100644 --- a/lib/node_modules/@stdlib/iter/cartesian-product/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/iter/cartesian-product/benchmark/benchmark.js @@ -22,6 +22,7 @@ var bench = require( '@stdlib/bench' ); var zeroTo = require( '@stdlib/array/zero-to' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); var isIteratorLike = require( '@stdlib/assert/is-iterator-like' ); var pkg = require( './../package.json' ).name; var iterCartesianProduct = require( './../lib' ); @@ -29,7 +30,7 @@ var iterCartesianProduct = require( './../lib' ); // MAIN // -bench( pkg+':len=100', function benchmark( b ) { +bench( pkg, function benchmark( b ) { var x; var i; var v; @@ -49,3 +50,28 @@ bench( pkg+':len=100', function benchmark( b ) { b.pass( 'benchmark finished' ); b.end(); }); + +bench( pkg+'::iterator', function benchmark( b ) { + var x; + var i; + var j; + var v; + + x = zeroTo( 100 ); + + v = iterCartesianProduct( x, x ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + j = v.next().value; + if ( isnan( j ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( !isIteratorLike( v ) ) { + b.fail( 'should return an iterator protocol-compliant object' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); From 2903e7da60481774fe3b47e2f7f370a928ece8a2 Mon Sep 17 00:00:00 2001 From: Shubham Singh Date: Thu, 29 Feb 2024 03:05:01 +0530 Subject: [PATCH 4/7] feat: corrected readme according to iter packages --- .../@stdlib/iter/cartesian-product/README.md | 30 +++++++++++++------ 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/lib/node_modules/@stdlib/iter/cartesian-product/README.md b/lib/node_modules/@stdlib/iter/cartesian-product/README.md index 930464f88b87..118d38adf053 100644 --- a/lib/node_modules/@stdlib/iter/cartesian-product/README.md +++ b/lib/node_modules/@stdlib/iter/cartesian-product/README.md @@ -41,15 +41,16 @@ var x2 = [3, 4]; var iter = iterCartesianProduct(x1, x2); -// Perform manual iteration... -var v; -while ( true ) { - v = iter.next(); - if ( v.done ) { - break; - } - console.log( v.value ); -} +var v = iter.next().value; +// returns [ 1, 3 ] + +v = iter.next().value; +// returns [ 1, 4 ] + +v = iter.next().value; +// returns [ 2, 3 ] + +// ... ``` If provided one or more empty arrays, the function returns an empty array. @@ -64,12 +65,21 @@ var v = iter.next().value; // returns undefined ``` +The returned [iterator][mdn-iterator-protocol] protocol-compliant object has the following properties: + +- **next**: function which returns an [iterator][mdn-iterator-protocol] protocol-compliant object containing the next iterated value (if one exists) assigned to a `value` property and a `done` property having a `boolean` value indicating whether the [iterator][mdn-iterator-protocol] is finished. +- **return**: function which closes an [iterator][mdn-iterator-protocol] and returns a single (optional) argument in an [iterator][mdn-iterator-protocol] protocol-compliant object. +
+## Notes + +- If an environment supports `Symbol.iterator` **and** a provided [iterator][mdn-iterator-protocol] is iterable, the returned [iterator][mdn-iterator-protocol] is iterable. +
@@ -116,6 +126,8 @@ v = iter.next().value; [cartesian-product]: https://en.wikipedia.org/wiki/Cartesian_product +[mdn-iterator-protocol]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol + [@stdlib/array/cartesian-product]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/cartesian-product From 26d777688fb0cdfcf5e34c4cdc1d0c4ec3e7bbd6 Mon Sep 17 00:00:00 2001 From: Shubham Singh Date: Thu, 29 Feb 2024 03:18:03 +0530 Subject: [PATCH 5/7] feat: updated doc test --- .../@stdlib/iter/cartesian-product/docs/types/test.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/node_modules/@stdlib/iter/cartesian-product/docs/types/test.ts b/lib/node_modules/@stdlib/iter/cartesian-product/docs/types/test.ts index 8e110c84c4b7..5d3b2910c6fc 100644 --- a/lib/node_modules/@stdlib/iter/cartesian-product/docs/types/test.ts +++ b/lib/node_modules/@stdlib/iter/cartesian-product/docs/types/test.ts @@ -24,6 +24,10 @@ import iterCartesianProduct = require( './index' ); // The function returns an iterator... { iterCartesianProduct( [ 1, 2, 3 ], [ 4, 5, 6 ] ); // $ExpectType Iterator + iterCartesianProduct( [ 'a', 'b' ], [ 'c', 'd' ] ); // $ExpectType Iterator + iterCartesianProduct( [ true, false ], [ true, false ] ); // $ExpectType Iterator + iterCartesianProduct( [ null, null ], [ null, null ] ); // $ExpectType Iterator + iterCartesianProduct( [ undefined, undefined ], [ undefined, undefined ] ); // $ExpectType Iterator } // The compiler throws an error if the function is provided a first argument which is not array-like... From 61854c40422013d094dc55bbffb511d34bb4f803 Mon Sep 17 00:00:00 2001 From: Shubham Date: Thu, 7 Mar 2024 01:44:59 +0530 Subject: [PATCH 6/7] Update lib/node_modules/@stdlib/iter/cartesian-product/test/test.js Co-authored-by: Stephannie Jimenez Gacha Signed-off-by: Shubham --- .../@stdlib/iter/cartesian-product/test/test.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/iter/cartesian-product/test/test.js b/lib/node_modules/@stdlib/iter/cartesian-product/test/test.js index dd64c2867a05..62789acf6c1c 100644 --- a/lib/node_modules/@stdlib/iter/cartesian-product/test/test.js +++ b/lib/node_modules/@stdlib/iter/cartesian-product/test/test.js @@ -37,7 +37,17 @@ tape('the function throws an error if provided a first argument which is not a c var values; var i; - values = ['5', 5, NaN, true, false, null, void 0, {}, function noop() {}]; + 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]); } From 1b6904973f0f4131545750ac8a1b6c04386bc7f0 Mon Sep 17 00:00:00 2001 From: Shubham Singh Date: Thu, 7 Mar 2024 02:06:38 +0530 Subject: [PATCH 7/7] feat: fixed tests and lint --- .../iter/cartesian-product/docs/types/test.ts | 8 ++++---- .../iter/cartesian-product/test/test.js | 18 +++++++++--------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/lib/node_modules/@stdlib/iter/cartesian-product/docs/types/test.ts b/lib/node_modules/@stdlib/iter/cartesian-product/docs/types/test.ts index 5d3b2910c6fc..2ef8eab92fc4 100644 --- a/lib/node_modules/@stdlib/iter/cartesian-product/docs/types/test.ts +++ b/lib/node_modules/@stdlib/iter/cartesian-product/docs/types/test.ts @@ -24,10 +24,10 @@ import iterCartesianProduct = require( './index' ); // The function returns an iterator... { iterCartesianProduct( [ 1, 2, 3 ], [ 4, 5, 6 ] ); // $ExpectType Iterator - iterCartesianProduct( [ 'a', 'b' ], [ 'c', 'd' ] ); // $ExpectType Iterator - iterCartesianProduct( [ true, false ], [ true, false ] ); // $ExpectType Iterator - iterCartesianProduct( [ null, null ], [ null, null ] ); // $ExpectType Iterator - iterCartesianProduct( [ undefined, undefined ], [ undefined, undefined ] ); // $ExpectType Iterator + iterCartesianProduct( 'a', 'c' ); // $ExpectError + iterCartesianProduct( undefined, undefined ); // $ExpectError + iterCartesianProduct( null, null ); // $ExpectError + iterCartesianProduct( true, true ); // $ExpectError } // The compiler throws an error if the function is provided a first argument which is not array-like... diff --git a/lib/node_modules/@stdlib/iter/cartesian-product/test/test.js b/lib/node_modules/@stdlib/iter/cartesian-product/test/test.js index 62789acf6c1c..1e5e49b4131e 100644 --- a/lib/node_modules/@stdlib/iter/cartesian-product/test/test.js +++ b/lib/node_modules/@stdlib/iter/cartesian-product/test/test.js @@ -38,15 +38,15 @@ tape('the function throws an error if provided a first argument which is not a c var i; values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {} + '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]);