From 3ce9a4487535b8408fe98eb9c4455315516f0b1b Mon Sep 17 00:00:00 2001 From: nourhan hasan Date: Thu, 22 Feb 2024 21:05:08 +0300 Subject: [PATCH 1/7] add @stdlib/iter/cartesian-power --- .../@stdlib/iter/cartesian-power/README.md | 183 ++++++++++++++++++ .../iter/cartesian-power/docs/repl.txt | 36 ++++ .../cartesian-power/docs/types/index.d.ts | 53 +++++ .../iter/cartesian-power/docs/types/test.ts | 57 ++++++ .../@stdlib/iter/cartesian-power/lib/index.js | 46 +++++ .../@stdlib/iter/cartesian-power/lib/main.js | 147 ++++++++++++++ .../@stdlib/iter/cartesian-power/package.json | 70 +++++++ 7 files changed, 592 insertions(+) create mode 100644 node_modules/@stdlib/iter/cartesian-power/README.md create mode 100644 node_modules/@stdlib/iter/cartesian-power/docs/repl.txt create mode 100644 node_modules/@stdlib/iter/cartesian-power/docs/types/index.d.ts create mode 100644 node_modules/@stdlib/iter/cartesian-power/docs/types/test.ts create mode 100644 node_modules/@stdlib/iter/cartesian-power/lib/index.js create mode 100644 node_modules/@stdlib/iter/cartesian-power/lib/main.js create mode 100644 node_modules/@stdlib/iter/cartesian-power/package.json diff --git a/node_modules/@stdlib/iter/cartesian-power/README.md b/node_modules/@stdlib/iter/cartesian-power/README.md new file mode 100644 index 00000000000..72964a68487 --- /dev/null +++ b/node_modules/@stdlib/iter/cartesian-power/README.md @@ -0,0 +1,183 @@ + + +# iterCartesianPower + +> Create an iterator which generates the Cartesian power of an input array-like object. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var iterCartesianPower = require( '@stdlib/iter/cartesian-power' ); +``` + +#### iterCartesianPower( x, n ) + +Returns an iterator which generates the Cartesian power of an input array-like object. + +```javascript +var x = ['a', 'b', 'c']; +var n = 2; +var iterator = iterCartesianPower( x, n ); + +for (const combination of iterator) { + console.log( combination ); +} + +``` + +
+ + + + + +
+ +## Notes + +- The function expects the first argument x to be an array-like object. +- The second argument n must be a non-negative integer. +- The returned iterator will generate all possible combinations of n elements from the input array x. + +
+ + + + + +
+ +## Examples + + + + +```javascript +// Example 1: Generating Cartesian power of an array with n = 2 +var x = [1, 2, 3]; +var n = 2; +var iterator = iterCartesianPower(x, n); + +for (const combination of iterator) { + console.log(combination); +} + +// Output: +// [ 1, 1 ] +// [ 1, 2 ] +// [ 1, 3 ] +// [ 2, 1 ] +// [ 2, 2 ] +// [ 2, 3 ] +// [ 3, 1 ] +// [ 3, 2 ] +// [ 3, 3 ] + +``` + +```javascript +var x = ['a', 'b']; +var n = 3; +var iterator = iterCartesianPower(x, n); + +for (const combination of iterator) { + console.log(combination); +} + +// [ 'a', 'a', 'a' ] +// [ 'a', 'a', 'b' ] +// [ 'a', 'b', 'a' ] +// [ 'a', 'b', 'b' ] +// [ 'b', 'a', 'a' ] +// [ 'b', 'a', 'b' ] +// [ 'b', 'b', 'a' ] +// [ 'b', 'b', 'b' ] + +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/node_modules/@stdlib/iter/cartesian-power/docs/repl.txt b/node_modules/@stdlib/iter/cartesian-power/docs/repl.txt new file mode 100644 index 00000000000..f878f33a3eb --- /dev/null +++ b/node_modules/@stdlib/iter/cartesian-power/docs/repl.txt @@ -0,0 +1,36 @@ +{{alias}}( x, n ) + Returns an iterator which generates the Cartesian power of an input array-like object. + + If an environment supports Symbol.iterator, the returned iterator is iterable. + + Parameters + ---------- + x: Array|TypedArray|Iterable + Input array-like object. + + n: integer + Integer power. + + 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}}( ['a', 'b', 'c'], 2 ); + > var v = it.next().value; + [ 'a', 'a' ] + > v = it.next().value; + [ 'a', 'b' ] + + See Also + -------- diff --git a/node_modules/@stdlib/iter/cartesian-power/docs/types/index.d.ts b/node_modules/@stdlib/iter/cartesian-power/docs/types/index.d.ts new file mode 100644 index 00000000000..c45bfe34d43 --- /dev/null +++ b/node_modules/@stdlib/iter/cartesian-power/docs/types/index.d.ts @@ -0,0 +1,53 @@ +/* +* @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. +*/ + +// 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 the Cartesian power of an input array-like object. +* +* @param x - Input array-like object. +* @param n - Integer power. +* @throws {TypeError} first argument must be an array-like object +* @throws {TypeError} second argument must be a nonnegative integer +* @returns iterator +* +* @example +* import iterCartesianPower = require( '@stdlib/iter/cartesian-power' ); +* +* var arr = ['a', 'b', 'c']; +* var n = 2; +* var iterator = iterCartesianPower(arr, n); +* +* var result; +* while (!(result = iterator.next()).done) { +* console.log(result.value); +* } +*/ +declare function iterCartesianPower(x: Array | TypedArray | Iterable, n: number): Iterator; + +// EXPORTS // + +export = iterCartesianPower; diff --git a/node_modules/@stdlib/iter/cartesian-power/docs/types/test.ts b/node_modules/@stdlib/iter/cartesian-power/docs/types/test.ts new file mode 100644 index 00000000000..e8e19758269 --- /dev/null +++ b/node_modules/@stdlib/iter/cartesian-power/docs/types/test.ts @@ -0,0 +1,57 @@ +/* +* @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 iterCartesianPower = require( './index' ); + + +// TESTS // + +// The function returns an iterator... +{ + iterCartesianPower( ['a', 'b', 'c'], 2 ); // $ExpectType Iterator + iterCartesianPower( ['a', 'b', 'c'], 3 ); // $ExpectType Iterator + iterCartesianPower( [1, 2, 3], 3 ); // $ExpectType Iterator +} + +// The compiler throws an error if the function is provided a first argument which is not an array-like object... +{ + iterCartesianPower( 5, 2 ); // $ExpectError + iterCartesianPower( true, 2 ); // $ExpectError + iterCartesianPower( false, 2 ); // $ExpectError + iterCartesianPower( null, 2 ); // $ExpectError + iterCartesianPower( {}, 2 ); // $ExpectError + iterCartesianPower( ( x: number ): number => x, 2 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not a number... +{ + iterCartesianPower( ['a', 'b', 'c'], '5' ); // $ExpectError + iterCartesianPower( ['a', 'b', 'c'], true ); // $ExpectError + iterCartesianPower( ['a', 'b', 'c'], false ); // $ExpectError + iterCartesianPower( ['a', 'b', 'c'], null ); // $ExpectError + iterCartesianPower( ['a', 'b', 'c'], [] ); // $ExpectError + iterCartesianPower( ['a', 'b', 'c'], {} ); // $ExpectError + iterCartesianPower( ['a', 'b', 'c'], ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided insufficient arguments... +{ + iterCartesianPower(); // $ExpectError + iterCartesianPower( ['a', 'b', 'c'] ); // $ExpectError + iterCartesianPower( ['a', 'b', 'c'], 2, 3 ); // $ExpectError +} diff --git a/node_modules/@stdlib/iter/cartesian-power/lib/index.js b/node_modules/@stdlib/iter/cartesian-power/lib/index.js new file mode 100644 index 00000000000..d83a03bcd24 --- /dev/null +++ b/node_modules/@stdlib/iter/cartesian-power/lib/index.js @@ -0,0 +1,46 @@ +/** +* @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 returns Cartesian powers. +* +* @module @stdlib/iter/cartesian-power +* +* @example +* var iterCartesianPower = require( '@stdlib/iter/cartesian-power' ); +* +* var inputArray = [1, 2, 3]; +* var power = 2; +* var iterator = iterCartesianPower(inputArray, power); +* +* var result; +* while (!(result = iterator.next()).done) { +* console.log(result.value); +* } +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/node_modules/@stdlib/iter/cartesian-power/lib/main.js b/node_modules/@stdlib/iter/cartesian-power/lib/main.js new file mode 100644 index 00000000000..3a7664a6ede --- /dev/null +++ b/node_modules/@stdlib/iter/cartesian-power/lib/main.js @@ -0,0 +1,147 @@ +/** +* @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 isNumber = require('@stdlib/assert/is-number').isPrimitive; +var isnan = require('@stdlib/math/base/assert/is-nan'); +var isNonNegativeInteger = require('@stdlib/assert/is-nonnegative-integer').isPrimitive; +var iteratorSymbol = require('@stdlib/symbol/iterator'); +var format = require('@stdlib/string/format'); + +// MAIN // + +/** +* Returns an iterator which generates the Cartesian power of an input array-like object. +* +* @param {Array|TypedArray|Iterable} x - Input array-like object. +* @param {number} n - Integer power. +* @throws {TypeError} first argument must be an array-like object +* @throws {TypeError} second argument must be a nonnegative integer +* @returns {Iterator} iterator +* +* @example +* var iterCartesianPower = require( '@stdlib/iter/cartesian-power' ); +* +* var arr = ['a', 'b', 'c']; +* var n = 2; +* var iterator = iterCartesianPower(arr, n); +* +* var result; +* while (!(result = iterator.next()).done) { +* console.log(result.value); +* } +*/ + +function iterCartesianPower(x, n) { + var iter; + var FLG = false; + var i = 0; + var j = []; + var idx = []; + + // Validate input arguments + if (!Array.isArray(x) && !isTypedArray(x) && !isIterable(x)) { + throw new TypeError('invalid argument. First argument must be an array-like object.'); + } + if (!isNumber(n) || isnan(n)) { + throw new TypeError(format('invalid argument. Second argument must be a number. Value: `%s`.', n)); + } + if (!isNonNegativeInteger(n)) { + throw new TypeError('invalid argument. Second argument must be a nonnegative integer.'); + } + + // Initialize index array + for (var k = 0; k < n; k++) { + idx.push(0); + } + + // Create an iterator protocol-compliant object + iter = {}; + 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 (FLG) { + return { 'done': true }; + } + var res = []; + for (var k = 0; k < n; k++) { + res.push(x[idx[k]]); + } + // Update indices + for (var k = n - 1; k >= 0; k--) { + idx[k]++; + if (idx[k] === x.length) { + idx[k] = 0; + } else { + break; + } + } + i++; + if (i >= Math.pow(x.length, n)) { + FLG = true; + } + return { + 'value': res, + 'done': false + }; + } + + /** + * Finishes an iterator. + * + * @private + * @param {*} [value] - value to return + * @returns {Object} iterator protocol-compliant object + */ + function end(value) { + FLG = true; + if (arguments.length) { + return { 'value': value, 'done': true }; + } + return { 'done': true }; + } + + /** + * Returns a new iterator. + * + * @private + * @returns {Iterator} iterator + */ + function factory() { + return iterCartesianPower(x, n); + } +} + +// EXPORTS // +module.exports = iterCartesianPower; diff --git a/node_modules/@stdlib/iter/cartesian-power/package.json b/node_modules/@stdlib/iter/cartesian-power/package.json new file mode 100644 index 00000000000..7ccd0d94b89 --- /dev/null +++ b/node_modules/@stdlib/iter/cartesian-power/package.json @@ -0,0 +1,70 @@ +{ + "name": "@stdlib/iter/cartesian-power", + "version": "0.0.0", + "description": "Create an iterator which generates the Cartesian power of an input array-like object.", + "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", + "stdutils", + "stdutil", + "utilities", + "utility", + "utils", + "util", + "linspace", + "linear", + "sequence", + "monotonic", + "arange", + "range", + "iterator", + "iterate", + "iteration", + "iter" + ] +} From 05614b907fa45407d63dcbe48b4c6b7b410de1f7 Mon Sep 17 00:00:00 2001 From: nourhan hasan Date: Fri, 23 Feb 2024 17:31:41 +0300 Subject: [PATCH 2/7] update cartesian-power --- .../@stdlib/iter/cartesian-power/README.md | 29 +++ .../cartesian-power/benchmark/benchmark.js | 49 +++++ .../iter/cartesian-power/docs/repl.txt | 2 +- .../cartesian-power/docs/types/index.d.ts | 3 +- .../iter/cartesian-power/docs/types/test.ts | 40 ++-- .../@stdlib/iter/cartesian-power/lib/index.js | 2 +- .../@stdlib/iter/cartesian-power/lib/main.js | 174 +++++++++--------- .../@stdlib/iter/cartesian-power/test/test.js | 56 ++++++ 8 files changed, 242 insertions(+), 113 deletions(-) create mode 100644 node_modules/@stdlib/iter/cartesian-power/benchmark/benchmark.js create mode 100644 node_modules/@stdlib/iter/cartesian-power/test/test.js diff --git a/node_modules/@stdlib/iter/cartesian-power/README.md b/node_modules/@stdlib/iter/cartesian-power/README.md index 72964a68487..92784c80872 100644 --- a/node_modules/@stdlib/iter/cartesian-power/README.md +++ b/node_modules/@stdlib/iter/cartesian-power/README.md @@ -114,6 +114,7 @@ for (const combination of iterator) { console.log(combination); } +// Output: // [ 'a', 'a', 'a' ] // [ 'a', 'a', 'b' ] // [ 'a', 'b', 'a' ] @@ -125,6 +126,34 @@ for (const combination of iterator) { ``` +```javascript +var x = ['a', 'b', 'c']; +var n = 1; +var iterator = iterCartesianPower(x, n); + +for (const combination of iterator) { + console.log(combination); +} + +// Output: +// [ 'a' ] +// [ 'b' ] +// [ 'c' ] +``` + +```javascript +var x = ['a', 'b', 'c']; +var n = 0; +var iterator = iterCartesianPower(x, n); + +for (const combination of iterator) { + console.log(combination); +} + +// Output: +// [] +``` + diff --git a/node_modules/@stdlib/iter/cartesian-power/benchmark/benchmark.js b/node_modules/@stdlib/iter/cartesian-power/benchmark/benchmark.js new file mode 100644 index 00000000000..6d84435e0c1 --- /dev/null +++ b/node_modules/@stdlib/iter/cartesian-power/benchmark/benchmark.js @@ -0,0 +1,49 @@ +/** +* @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 // + +const bench = require('@stdlib/bench'); +const pkg = require('./../package.json').name; +const iterCartesianPower = require('./../lib'); + +// MAIN // + +bench(pkg, function benchmark(b) { + var arr = ['a', 'b', 'c']; + var n = 2; + var iterator = iterCartesianPower(arr, n); + var v; + + // Run the benchmark... + b.tic(); + for (var i = 0; i < b.iterations; i++) { + v = iterator.next(); + if (v.done) { + b.fail('should iterate over all elements'); + } + } + b.toc(); + if (v.done) { + b.pass('benchmark finished'); + } else { + b.fail('should have finished iteration'); + } +}); diff --git a/node_modules/@stdlib/iter/cartesian-power/docs/repl.txt b/node_modules/@stdlib/iter/cartesian-power/docs/repl.txt index f878f33a3eb..225846174fa 100644 --- a/node_modules/@stdlib/iter/cartesian-power/docs/repl.txt +++ b/node_modules/@stdlib/iter/cartesian-power/docs/repl.txt @@ -5,7 +5,7 @@ Parameters ---------- - x: Array|TypedArray|Iterable + x : ArrayLike | Iterable Input array-like object. n: integer diff --git a/node_modules/@stdlib/iter/cartesian-power/docs/types/index.d.ts b/node_modules/@stdlib/iter/cartesian-power/docs/types/index.d.ts index c45bfe34d43..a8454c84a9d 100644 --- a/node_modules/@stdlib/iter/cartesian-power/docs/types/index.d.ts +++ b/node_modules/@stdlib/iter/cartesian-power/docs/types/index.d.ts @@ -21,6 +21,7 @@ /// import { Iterator as Iter, IterableIterator } from '@stdlib/types/iter'; +import { ArrayLike } from '@stdlib/types/array'; // Define a union type representing both iterable and non-iterable iterators: type Iterator = Iter | IterableIterator; @@ -46,7 +47,7 @@ type Iterator = Iter | IterableIterator; * console.log(result.value); * } */ -declare function iterCartesianPower(x: Array | TypedArray | Iterable, n: number): Iterator; +declare function iterCartesianPower(x: ArrayLike | Iterable, n: number): Iterator; // EXPORTS // diff --git a/node_modules/@stdlib/iter/cartesian-power/docs/types/test.ts b/node_modules/@stdlib/iter/cartesian-power/docs/types/test.ts index e8e19758269..19d992558c5 100644 --- a/node_modules/@stdlib/iter/cartesian-power/docs/types/test.ts +++ b/node_modules/@stdlib/iter/cartesian-power/docs/types/test.ts @@ -16,42 +16,42 @@ * limitations under the License. */ -import iterCartesianPower = require( './index' ); +const iterCartesianPower = require('./index'); // TESTS // // The function returns an iterator... { - iterCartesianPower( ['a', 'b', 'c'], 2 ); // $ExpectType Iterator - iterCartesianPower( ['a', 'b', 'c'], 3 ); // $ExpectType Iterator - iterCartesianPower( [1, 2, 3], 3 ); // $ExpectType Iterator + iterCartesianPower(['a', 'b', 'c'], 2); // $ExpectType Iterator + iterCartesianPower(['a', 'b', 'c'], 3); // $ExpectType Iterator + iterCartesianPower([1, 2, 3], 3); // $ExpectType Iterator } // The compiler throws an error if the function is provided a first argument which is not an array-like object... { - iterCartesianPower( 5, 2 ); // $ExpectError - iterCartesianPower( true, 2 ); // $ExpectError - iterCartesianPower( false, 2 ); // $ExpectError - iterCartesianPower( null, 2 ); // $ExpectError - iterCartesianPower( {}, 2 ); // $ExpectError - iterCartesianPower( ( x: number ): number => x, 2 ); // $ExpectError + iterCartesianPower(5, 2); // $ExpectError + iterCartesianPower(true, 2); // $ExpectError + iterCartesianPower(false, 2); // $ExpectError + iterCartesianPower(null, 2); // $ExpectError + iterCartesianPower({}, 2); // $ExpectError + iterCartesianPower((x: number): number => x, 2); // $ExpectError } // The compiler throws an error if the function is provided a second argument which is not a number... { - iterCartesianPower( ['a', 'b', 'c'], '5' ); // $ExpectError - iterCartesianPower( ['a', 'b', 'c'], true ); // $ExpectError - iterCartesianPower( ['a', 'b', 'c'], false ); // $ExpectError - iterCartesianPower( ['a', 'b', 'c'], null ); // $ExpectError - iterCartesianPower( ['a', 'b', 'c'], [] ); // $ExpectError - iterCartesianPower( ['a', 'b', 'c'], {} ); // $ExpectError - iterCartesianPower( ['a', 'b', 'c'], ( x: number ): number => x ); // $ExpectError + iterCartesianPower(['a', 'b', 'c'], '5'); // $ExpectError + iterCartesianPower(['a', 'b', 'c'], true); // $ExpectError + iterCartesianPower(['a', 'b', 'c'], false); // $ExpectError + iterCartesianPower(['a', 'b', 'c'], null); // $ExpectError + iterCartesianPower(['a', 'b', 'c'], []); // $ExpectError + iterCartesianPower(['a', 'b', 'c'], {}); // $ExpectError + iterCartesianPower(['a', 'b', 'c'], (x: number): number => x); // $ExpectError } // The compiler throws an error if the function is provided insufficient arguments... { - iterCartesianPower(); // $ExpectError - iterCartesianPower( ['a', 'b', 'c'] ); // $ExpectError - iterCartesianPower( ['a', 'b', 'c'], 2, 3 ); // $ExpectError + iterCartesianPower(); // $ExpectError + iterCartesianPower(['a', 'b', 'c']); // $ExpectError + iterCartesianPower(['a', 'b', 'c'], 2, 3); // $ExpectError } diff --git a/node_modules/@stdlib/iter/cartesian-power/lib/index.js b/node_modules/@stdlib/iter/cartesian-power/lib/index.js index d83a03bcd24..ad4ff66ab7f 100644 --- a/node_modules/@stdlib/iter/cartesian-power/lib/index.js +++ b/node_modules/@stdlib/iter/cartesian-power/lib/index.js @@ -38,7 +38,7 @@ // MODULES // -var main = require( './main.js' ); +var main = require('./main.js'); // EXPORTS // diff --git a/node_modules/@stdlib/iter/cartesian-power/lib/main.js b/node_modules/@stdlib/iter/cartesian-power/lib/main.js index 3a7664a6ede..ad15b5ff3bf 100644 --- a/node_modules/@stdlib/iter/cartesian-power/lib/main.js +++ b/node_modules/@stdlib/iter/cartesian-power/lib/main.js @@ -20,8 +20,7 @@ // MODULES // var setReadOnly = require('@stdlib/utils/define-nonenumerable-read-only-property'); -var isNumber = require('@stdlib/assert/is-number').isPrimitive; -var isnan = require('@stdlib/math/base/assert/is-nan'); +var isCollection = require('@stdlib/assert/is-collection'); var isNonNegativeInteger = require('@stdlib/assert/is-nonnegative-integer').isPrimitive; var iteratorSymbol = require('@stdlib/symbol/iterator'); var format = require('@stdlib/string/format'); @@ -29,11 +28,11 @@ var format = require('@stdlib/string/format'); // MAIN // /** -* Returns an iterator which generates the Cartesian power of an input array-like object. +* Returns an iterator which generates the Cartesian power of an input collection. * -* @param {Array|TypedArray|Iterable} x - Input array-like object. -* @param {number} n - Integer power. -* @throws {TypeError} first argument must be an array-like object +* @param {Collection} x - input collection +* @param {NonNegativeInteger} n - integer power +* @throws {TypeError} first argument must be a collection * @throws {TypeError} second argument must be a nonnegative integer * @returns {Iterator} iterator * @@ -49,98 +48,93 @@ var format = require('@stdlib/string/format'); * console.log(result.value); * } */ - function iterCartesianPower(x, n) { - var iter; - var FLG = false; - var i = 0; - var j = []; - var idx = []; + var iter; + var FLG = false; + var i = 0; + var idx = []; - // Validate input arguments - if (!Array.isArray(x) && !isTypedArray(x) && !isIterable(x)) { - throw new TypeError('invalid argument. First argument must be an array-like object.'); - } - if (!isNumber(n) || isnan(n)) { - throw new TypeError(format('invalid argument. Second argument must be a number. Value: `%s`.', n)); - } - if (!isNonNegativeInteger(n)) { - throw new TypeError('invalid argument. Second argument must be a nonnegative integer.'); - } + // Validate input arguments + if (!isCollection(x)) { + throw new TypeError(format('invalid argument. First argument must be a collection. Value: `%s`.', x)); + } + if (!isNonNegativeInteger(n)) { + throw new TypeError(format('invalid argument. Second argument must be a nonnegative integer. Value: `%s`.', n)); + } - // Initialize index array - for (var k = 0; k < n; k++) { - idx.push(0); - } + // Initialize index array + for (var k = 0; k < n; k++) { + idx.push(0); + } - // Create an iterator protocol-compliant object - iter = {}; - setReadOnly(iter, 'next', next); - setReadOnly(iter, 'return', end); + // Create an iterator protocol-compliant object + iter = {}; + 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; + // 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 (FLG) { - return { 'done': true }; - } - var res = []; - for (var k = 0; k < n; k++) { - res.push(x[idx[k]]); - } - // Update indices - for (var k = n - 1; k >= 0; k--) { - idx[k]++; - if (idx[k] === x.length) { - idx[k] = 0; - } else { - break; - } - } - i++; - if (i >= Math.pow(x.length, n)) { - FLG = true; - } - return { - 'value': res, - 'done': false - }; - } + /** + * Returns an iterator protocol-compliant object containing the next iterated value. + * + * @private + * @returns {Object} iterator protocol-compliant object + */ + function next() { + if (FLG) { + return { 'done': true }; + } + var res = []; + for (var k = 0; k < n; k++) { + res.push(x[idx[k]]); + } + // Update indices + for (var k = n - 1; k >= 0; k--) { + idx[k]++; + if (idx[k] === x.length) { + idx[k] = 0; + } else { + break; + } + } + i++; + if (i >= Math.pow(x.length, n)) { + FLG = true; + } + return { + 'value': res, + 'done': false + }; + } - /** - * Finishes an iterator. - * - * @private - * @param {*} [value] - value to return - * @returns {Object} iterator protocol-compliant object - */ - function end(value) { - FLG = true; - if (arguments.length) { - return { 'value': value, 'done': true }; - } - return { 'done': true }; - } + /** + * Finishes an iterator. + * + * @private + * @param {*} [value] - value to return + * @returns {Object} iterator protocol-compliant object + */ + function end(value) { + FLG = true; + if (arguments.length) { + return { 'value': value, 'done': true }; + } + return { 'done': true }; + } - /** - * Returns a new iterator. - * - * @private - * @returns {Iterator} iterator - */ - function factory() { - return iterCartesianPower(x, n); - } + /** + * Returns a new iterator. + * + * @private + * @returns {Iterator} iterator + */ + function factory() { + return iterCartesianPower(x, n); + } } // EXPORTS // diff --git a/node_modules/@stdlib/iter/cartesian-power/test/test.js b/node_modules/@stdlib/iter/cartesian-power/test/test.js new file mode 100644 index 00000000000..bddfd00624a --- /dev/null +++ b/node_modules/@stdlib/iter/cartesian-power/test/test.js @@ -0,0 +1,56 @@ +/** +* @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 tape = require('tape'); +var iterCartesianPower = require('./../lib'); + + +tape('main export is a function', function test(t) { + t.ok(iterCartesianPower instanceof Function, 'main export is a function'); + t.end(); +}); + +tape('the function returns an iterator which generates all possible combinations of length `n` containing elements from the input array', function test(t) { + var expected = [['a', 'a'], ['a', 'b'], ['a', 'c'], ['b', 'a'], ['b', 'b'], ['b', 'c'], ['c', 'a'], ['c', 'b'], ['c', 'c']]; + var actual = []; + for (var v of iterCartesianPower(['a', 'b', 'c'], 2)) { + actual.push(v); + } + t.deepEqual(actual, expected, 'returns expected values'); + t.end(); +}); + +tape('the function returns an iterator which returns an empty array if `n` is 0', function test(t) { + var iter = iterCartesianPower(['a', 'b', 'c'], 0); + var actual = iter.next().value; + var expected = []; + t.deepEqual(actual, expected, 'returns expected value'); + t.end(); +}); + +tape('the function returns an iterator which generates all individual elements of the input array when `n` is 1', function test(t) { + var iter = iterCartesianPower(['a', 'b', 'c'], 1); + var actual = []; + for (var v of iter) { + actual.push(v); + } + var expected = [['a'], ['b'], ['c']] + t.deepEqual(actual, expected, 'returns expected value'); + t.end(); +}); From b2f6c9792e7b9037dc7bf53f257c1a6168994686 Mon Sep 17 00:00:00 2001 From: nourhan hasan Date: Sat, 24 Feb 2024 09:38:39 +0300 Subject: [PATCH 3/7] Updating spaces --- .../cartesian-power/benchmark/benchmark.js | 26 +++--- .../iter/cartesian-power/docs/repl.txt | 2 +- .../cartesian-power/docs/types/index.d.ts | 17 ++-- .../iter/cartesian-power/docs/types/test.ts | 38 ++++----- .../@stdlib/iter/cartesian-power/lib/index.js | 18 +++-- .../@stdlib/iter/cartesian-power/lib/main.js | 80 +++++++++++-------- .../@stdlib/iter/cartesian-power/test/test.js | 44 +++++----- 7 files changed, 123 insertions(+), 102 deletions(-) diff --git a/node_modules/@stdlib/iter/cartesian-power/benchmark/benchmark.js b/node_modules/@stdlib/iter/cartesian-power/benchmark/benchmark.js index 6d84435e0c1..df0bb2c7449 100644 --- a/node_modules/@stdlib/iter/cartesian-power/benchmark/benchmark.js +++ b/node_modules/@stdlib/iter/cartesian-power/benchmark/benchmark.js @@ -20,30 +20,30 @@ // MODULES // -const bench = require('@stdlib/bench'); -const pkg = require('./../package.json').name; -const iterCartesianPower = require('./../lib'); +const bench = require( '@stdlib/bench' ); +const pkg = require( './../package.json' ).name; +const iterCartesianPower = require( './../lib' ); // MAIN // -bench(pkg, function benchmark(b) { - var arr = ['a', 'b', 'c']; +bench( pkg, function benchmark( b ) { + var arr = [ 'a', 'b', 'c' ]; var n = 2; - var iterator = iterCartesianPower(arr, n); + var iterator = iterCartesianPower( arr, n ); var v; // Run the benchmark... b.tic(); - for (var i = 0; i < b.iterations; i++) { + for ( var i = 0; i < b.iterations; i++ ) { v = iterator.next(); - if (v.done) { - b.fail('should iterate over all elements'); + if ( v.done ) { + b.fail( 'should iterate over all elements' ); } } b.toc(); - if (v.done) { - b.pass('benchmark finished'); + if ( v.done ) { + b.pass( 'benchmark finished' ); } else { - b.fail('should have finished iteration'); + b.fail( 'should have finished iteration' ); } -}); +} ); diff --git a/node_modules/@stdlib/iter/cartesian-power/docs/repl.txt b/node_modules/@stdlib/iter/cartesian-power/docs/repl.txt index 225846174fa..f7d5041c6db 100644 --- a/node_modules/@stdlib/iter/cartesian-power/docs/repl.txt +++ b/node_modules/@stdlib/iter/cartesian-power/docs/repl.txt @@ -26,7 +26,7 @@ Examples -------- - > var it = {{alias}}( ['a', 'b', 'c'], 2 ); + > var it = {{alias}}( [ 'a', 'b', 'c' ], 2 ); > var v = it.next().value; [ 'a', 'a' ] > v = it.next().value; diff --git a/node_modules/@stdlib/iter/cartesian-power/docs/types/index.d.ts b/node_modules/@stdlib/iter/cartesian-power/docs/types/index.d.ts index a8454c84a9d..834629b8b9b 100644 --- a/node_modules/@stdlib/iter/cartesian-power/docs/types/index.d.ts +++ b/node_modules/@stdlib/iter/cartesian-power/docs/types/index.d.ts @@ -36,18 +36,23 @@ type Iterator = Iter | IterableIterator; * @returns iterator * * @example -* import iterCartesianPower = require( '@stdlib/iter/cartesian-power' ); +* var iterCartesianPower = require( '@stdlib/iter/cartesian-power' ); * -* var arr = ['a', 'b', 'c']; +* var x = [ 'a', 'b' ]; * var n = 2; -* var iterator = iterCartesianPower(arr, n); +* var iterator = iterCartesianPower( x, n ); * * var result; -* while (!(result = iterator.next()).done) { -* console.log(result.value); +* while ( !( result = iterator.next() ).done ) { +* console.log( result.value ); * } +* // return +* // [ 'a', 'a' ] +* // [ 'a', 'b' ] +* // [ 'b', 'a' ] +* // [ 'b', 'b' ] */ -declare function iterCartesianPower(x: ArrayLike | Iterable, n: number): Iterator; +declare function iterCartesianPower( x: ArrayLike | Iterable, n: number ): Iterator; // EXPORTS // diff --git a/node_modules/@stdlib/iter/cartesian-power/docs/types/test.ts b/node_modules/@stdlib/iter/cartesian-power/docs/types/test.ts index 19d992558c5..06cd377450b 100644 --- a/node_modules/@stdlib/iter/cartesian-power/docs/types/test.ts +++ b/node_modules/@stdlib/iter/cartesian-power/docs/types/test.ts @@ -16,42 +16,42 @@ * limitations under the License. */ -const iterCartesianPower = require('./index'); +const iterCartesianPower = require( './index' ); // TESTS // // The function returns an iterator... { - iterCartesianPower(['a', 'b', 'c'], 2); // $ExpectType Iterator - iterCartesianPower(['a', 'b', 'c'], 3); // $ExpectType Iterator - iterCartesianPower([1, 2, 3], 3); // $ExpectType Iterator + iterCartesianPower( [ 'a', 'b', 'c' ], 2 ); // $ExpectType Iterator + iterCartesianPower( [ 'a', 'b', 'c' ], 3 ); // $ExpectType Iterator + iterCartesianPower( [ 1, 2, 3 ], 3 ); // $ExpectType Iterator } // The compiler throws an error if the function is provided a first argument which is not an array-like object... { - iterCartesianPower(5, 2); // $ExpectError - iterCartesianPower(true, 2); // $ExpectError - iterCartesianPower(false, 2); // $ExpectError - iterCartesianPower(null, 2); // $ExpectError - iterCartesianPower({}, 2); // $ExpectError - iterCartesianPower((x: number): number => x, 2); // $ExpectError + iterCartesianPower( 5, 2 ); // $ExpectError + iterCartesianPower( true, 2 ); // $ExpectError + iterCartesianPower( false, 2 ); // $ExpectError + iterCartesianPower( null, 2 ); // $ExpectError + iterCartesianPower( {}, 2 ); // $ExpectError + iterCartesianPower( ( x: number ): number => x, 2 ); // $ExpectError } // The compiler throws an error if the function is provided a second argument which is not a number... { - iterCartesianPower(['a', 'b', 'c'], '5'); // $ExpectError - iterCartesianPower(['a', 'b', 'c'], true); // $ExpectError - iterCartesianPower(['a', 'b', 'c'], false); // $ExpectError - iterCartesianPower(['a', 'b', 'c'], null); // $ExpectError - iterCartesianPower(['a', 'b', 'c'], []); // $ExpectError - iterCartesianPower(['a', 'b', 'c'], {}); // $ExpectError - iterCartesianPower(['a', 'b', 'c'], (x: number): number => x); // $ExpectError + iterCartesianPower( [ 'a', 'b', 'c' ], '5' ); // $ExpectError + iterCartesianPower( [ 'a', 'b', 'c' ], true ); // $ExpectError + iterCartesianPower( [ 'a', 'b', 'c' ], false ); // $ExpectError + iterCartesianPower( [ 'a', 'b', 'c' ], null ); // $ExpectError + iterCartesianPower( [ 'a', 'b', 'c' ], [] ); // $ExpectError + iterCartesianPower( [ 'a', 'b', 'c' ], {} ); // $ExpectError + iterCartesianPower( [ 'a', 'b', 'c' ], ( x: number ): number => x ); // $ExpectError } // The compiler throws an error if the function is provided insufficient arguments... { iterCartesianPower(); // $ExpectError - iterCartesianPower(['a', 'b', 'c']); // $ExpectError - iterCartesianPower(['a', 'b', 'c'], 2, 3); // $ExpectError + iterCartesianPower( [ 'a', 'b', 'c' ] ); // $ExpectError + iterCartesianPower( [ 'a', 'b', 'c' ], 2, 3 ); // $ExpectError } diff --git a/node_modules/@stdlib/iter/cartesian-power/lib/index.js b/node_modules/@stdlib/iter/cartesian-power/lib/index.js index ad4ff66ab7f..fbebfedbbb6 100644 --- a/node_modules/@stdlib/iter/cartesian-power/lib/index.js +++ b/node_modules/@stdlib/iter/cartesian-power/lib/index.js @@ -26,20 +26,24 @@ * @example * var iterCartesianPower = require( '@stdlib/iter/cartesian-power' ); * -* var inputArray = [1, 2, 3]; -* var power = 2; -* var iterator = iterCartesianPower(inputArray, power); +* var x = [ 'a', 'b' ]; +* var n = 2; +* var iterator = iterCartesianPower( x, n ); * * var result; -* while (!(result = iterator.next()).done) { -* console.log(result.value); +* while ( !( result = iterator.next() ).done ) { +* console.log( result.value ); * } +* // return +* // [ 'a', 'a' ] +* // [ 'a', 'b' ] +* // [ 'b', 'a' ] +* // [ 'b', 'b' ] */ // MODULES // -var main = require('./main.js'); - +var main = require( './main.js' ); // EXPORTS // diff --git a/node_modules/@stdlib/iter/cartesian-power/lib/main.js b/node_modules/@stdlib/iter/cartesian-power/lib/main.js index ad15b5ff3bf..68ce035ede6 100644 --- a/node_modules/@stdlib/iter/cartesian-power/lib/main.js +++ b/node_modules/@stdlib/iter/cartesian-power/lib/main.js @@ -19,11 +19,11 @@ 'use strict'; // MODULES // -var setReadOnly = require('@stdlib/utils/define-nonenumerable-read-only-property'); -var isCollection = require('@stdlib/assert/is-collection'); -var isNonNegativeInteger = require('@stdlib/assert/is-nonnegative-integer').isPrimitive; -var iteratorSymbol = require('@stdlib/symbol/iterator'); -var format = require('@stdlib/string/format'); +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var isCollection = require( '@stdlib/assert/is-collection' ); +var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive; +var iteratorSymbol = require( '@stdlib/symbol/iterator' ); +var format = require( '@stdlib/string/format' ); // MAIN // @@ -39,42 +39,47 @@ var format = require('@stdlib/string/format'); * @example * var iterCartesianPower = require( '@stdlib/iter/cartesian-power' ); * -* var arr = ['a', 'b', 'c']; +* var x = [ 'a', 'b' ]; * var n = 2; -* var iterator = iterCartesianPower(arr, n); +* var iterator = iterCartesianPower( x, n ); * * var result; -* while (!(result = iterator.next()).done) { -* console.log(result.value); +* while ( !( result = iterator.next() ).done ) { +* console.log( result.value ); * } +* // return +* // [ 'a', 'a' ] +* // [ 'a', 'b' ] +* // [ 'b', 'a' ] +* // [ 'b', 'b' ] */ -function iterCartesianPower(x, n) { +function iterCartesianPower( x, n ) { var iter; var FLG = false; var i = 0; var idx = []; // Validate input arguments - if (!isCollection(x)) { - throw new TypeError(format('invalid argument. First argument must be a collection. Value: `%s`.', x)); + if ( !isCollection( x ) ) { + throw new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', x ) ); } - if (!isNonNegativeInteger(n)) { - throw new TypeError(format('invalid argument. Second argument must be a nonnegative integer. Value: `%s`.', n)); + if ( !isNonNegativeInteger( n ) ) { + throw new TypeError( format( 'invalid argument. Second argument must be a nonnegative integer. Value: `%s`.', n ) ); } // Initialize index array - for (var k = 0; k < n; k++) { - idx.push(0); + for ( var k = 0; k < n; k++ ) { + idx.push( 0 ); } // Create an iterator protocol-compliant object iter = {}; - 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; @@ -85,24 +90,26 @@ function iterCartesianPower(x, n) { * @returns {Object} iterator protocol-compliant object */ function next() { - if (FLG) { - return { 'done': true }; + if ( FLG ) { + return { + 'done': true + }; } var res = []; - for (var k = 0; k < n; k++) { - res.push(x[idx[k]]); + for ( var k = 0; k < n; k++ ) { + res.push( x[ idx[ k ] ] ); } // Update indices - for (var k = n - 1; k >= 0; k--) { - idx[k]++; - if (idx[k] === x.length) { - idx[k] = 0; + for ( var k = n - 1; k >= 0; k-- ) { + idx[ k ]++; + if ( idx[ k ] === x.length ) { + idx[ k ] = 0; } else { break; } } i++; - if (i >= Math.pow(x.length, n)) { + if ( i >= Math.pow( x.length, n ) ) { FLG = true; } return { @@ -118,12 +125,17 @@ function iterCartesianPower(x, n) { * @param {*} [value] - value to return * @returns {Object} iterator protocol-compliant object */ - function end(value) { + function end( value ) { FLG = true; - if (arguments.length) { - return { 'value': value, 'done': true }; + if ( arguments.length ) { + return { + 'value': value, + 'done': true + }; } - return { 'done': true }; + return { + 'done': true + }; } /** @@ -133,7 +145,7 @@ function iterCartesianPower(x, n) { * @returns {Iterator} iterator */ function factory() { - return iterCartesianPower(x, n); + return iterCartesianPower( x, n ); } } diff --git a/node_modules/@stdlib/iter/cartesian-power/test/test.js b/node_modules/@stdlib/iter/cartesian-power/test/test.js index bddfd00624a..afe457bc8c6 100644 --- a/node_modules/@stdlib/iter/cartesian-power/test/test.js +++ b/node_modules/@stdlib/iter/cartesian-power/test/test.js @@ -17,40 +17,40 @@ */ 'use strict'; -var tape = require('tape'); -var iterCartesianPower = require('./../lib'); +var tape = require( 'tape' ); +var iterCartesianPower = require( './../lib' ); -tape('main export is a function', function test(t) { - t.ok(iterCartesianPower instanceof Function, 'main export is a function'); +tape( 'main export is a function', function test( t ) { + t.ok( iterCartesianPower instanceof Function, 'main export is a function' ); t.end(); -}); +} ); -tape('the function returns an iterator which generates all possible combinations of length `n` containing elements from the input array', function test(t) { - var expected = [['a', 'a'], ['a', 'b'], ['a', 'c'], ['b', 'a'], ['b', 'b'], ['b', 'c'], ['c', 'a'], ['c', 'b'], ['c', 'c']]; +tape( 'the function returns an iterator which generates all possible combinations of length `n` containing elements from the input array', function test( t ) { + var expected = [ [ 'a', 'a' ], [ 'a', 'b' ], [ 'a', 'c' ], [ 'b', 'a' ], [ 'b', 'b' ], [ 'b', 'c' ], [ 'c', 'a' ], [ 'c', 'b' ], [ 'c', 'c' ] ]; var actual = []; - for (var v of iterCartesianPower(['a', 'b', 'c'], 2)) { - actual.push(v); + for ( var v of iterCartesianPower( [ 'a', 'b', 'c' ], 2) ) { + actual.push( v ); } - t.deepEqual(actual, expected, 'returns expected values'); + t.deepEqual( actual, expected, 'returns expected values' ); t.end(); -}); +} ); -tape('the function returns an iterator which returns an empty array if `n` is 0', function test(t) { - var iter = iterCartesianPower(['a', 'b', 'c'], 0); +tape( 'the function returns an iterator which returns an empty array if `n` is 0', function test( t ) { + var iter = iterCartesianPower( [ 'a', 'b', 'c' ], 0 ); var actual = iter.next().value; var expected = []; - t.deepEqual(actual, expected, 'returns expected value'); + t.deepEqual( actual, expected, 'returns expected value' ); t.end(); -}); +} ); -tape('the function returns an iterator which generates all individual elements of the input array when `n` is 1', function test(t) { - var iter = iterCartesianPower(['a', 'b', 'c'], 1); +tape( 'the function returns an iterator which generates all individual elements of the input array when `n` is 1', function test( t ) { + var iter = iterCartesianPower( [ 'a', 'b', 'c' ], 1 ); var actual = []; - for (var v of iter) { - actual.push(v); + for ( var v of iter ) { + actual.push( v ); } - var expected = [['a'], ['b'], ['c']] - t.deepEqual(actual, expected, 'returns expected value'); + var expected = [ [ 'a' ], [ 'b' ], [ 'c' ] ] + t.deepEqual( actual, expected, 'returns expected value' ); t.end(); -}); +} ); From fada61a3c7a65b34cc11dad42ea10681f20bf66c Mon Sep 17 00:00:00 2001 From: nourhan hasan Date: Sat, 24 Feb 2024 22:13:23 +0300 Subject: [PATCH 4/7] Updating README and writing in ES5 --- .../@stdlib/iter/cartesian-power/README.md | 153 +++++++----------- .../iter/cartesian-power/docs/repl.txt | 2 +- .../@stdlib/iter/cartesian-power/lib/index.js | 30 ++-- .../@stdlib/iter/cartesian-power/lib/main.js | 49 +++--- .../@stdlib/iter/cartesian-power/package.json | 9 +- .../@stdlib/iter/cartesian-power/test/test.js | 52 ++++-- 6 files changed, 150 insertions(+), 145 deletions(-) diff --git a/node_modules/@stdlib/iter/cartesian-power/README.md b/node_modules/@stdlib/iter/cartesian-power/README.md index 92784c80872..21a81c878ff 100644 --- a/node_modules/@stdlib/iter/cartesian-power/README.md +++ b/node_modules/@stdlib/iter/cartesian-power/README.md @@ -20,7 +20,7 @@ limitations under the License. # iterCartesianPower -> Create an iterator which generates the Cartesian power of an input array-like object. +> Create an iterator which returns the Cartesian power. @@ -42,15 +42,19 @@ var iterCartesianPower = require( '@stdlib/iter/cartesian-power' ); #### iterCartesianPower( x, n ) -Returns an iterator which generates the Cartesian power of an input array-like object. +Returns an iterator which returns the Cartesian power. ```javascript -var x = ['a', 'b', 'c']; -var n = 2; -var iterator = iterCartesianPower( x, n ); -for (const combination of iterator) { - console.log( combination ); +var iterator = iterCartesianPower( [ 'a', 'b', 'c' ], 2 ); + +var v; +while ( true ) { + v = iterator.next(); + if ( v.done ) { + break; + } + console.log( v.value ); } ``` @@ -62,13 +66,7 @@ for (const combination of iterator) {
- -## Notes - -- The function expects the first argument x to be an array-like object. -- The second argument n must be a non-negative integer. -- The returned iterator will generate all possible combinations of n elements from the input array x. - +
@@ -79,81 +77,71 @@ for (const combination of iterator) { ## Examples - ```javascript // Example 1: Generating Cartesian power of an array with n = 2 -var x = [1, 2, 3]; -var n = 2; -var iterator = iterCartesianPower(x, n); +var iterator = iterCartesianPower( [ 1, 2, 3 ], 2 ); +// returns -for (const combination of iterator) { - console.log(combination); -} +var v = iterator.next().value; +// returns [ 1, 1 ] -// Output: -// [ 1, 1 ] -// [ 1, 2 ] -// [ 1, 3 ] -// [ 2, 1 ] -// [ 2, 2 ] -// [ 2, 3 ] -// [ 3, 1 ] -// [ 3, 2 ] -// [ 3, 3 ] +v = iterator.next().value; +// returns [ 1, 2 ] -``` +v = iterator.next().value; +// returns [ 1, 3 ] -```javascript -var x = ['a', 'b']; -var n = 3; -var iterator = iterCartesianPower(x, n); +v = iterator.next().value; +// returns [ 2, 1 ] -for (const combination of iterator) { - console.log(combination); -} +v = iterator.next().value; +// returns [ 2, 2 ] -// Output: -// [ 'a', 'a', 'a' ] -// [ 'a', 'a', 'b' ] -// [ 'a', 'b', 'a' ] -// [ 'a', 'b', 'b' ] -// [ 'b', 'a', 'a' ] -// [ 'b', 'a', 'b' ] -// [ 'b', 'b', 'a' ] -// [ 'b', 'b', 'b' ] +v = iterator.next().value; +// returns [ 2, 3 ] -``` +v = iterator.next().value; +// returns [ 3, 1 ] -```javascript -var x = ['a', 'b', 'c']; -var n = 1; -var iterator = iterCartesianPower(x, n); +v = iterator.next().value; +// returns [ 3, 2 ] -for (const combination of iterator) { - console.log(combination); -} +v = iterator.next().value; +// returns [ 3, 3 ] -// Output: -// [ 'a' ] -// [ 'b' ] -// [ 'c' ] -``` +var done = iterator.next().done; +// returns true -```javascript -var x = ['a', 'b', 'c']; -var n = 0; -var iterator = iterCartesianPower(x, n); +// Example 2: Generating Cartesian power of an array with n = 1 +iterator = iterCartesianPower( [ 'a', 'b', 'c' ], 1 ); +// returns -for (const combination of iterator) { - console.log(combination); -} +v = iterator.next().value; +// returns [ 'a' ] + +v = iterator.next().value; +// returns [ 'b' ] -// Output: -// [] +v = iterator.next().value; +// returns [ 'c' ] + +done = iterator.next().done; +// returns true + +// Example 3: Generating Cartesian power of an array with n = 0 +iterator = iterCartesianPower( [ 'a', 'b', 'c' ], 0 ); +// returns + +v = iterator.next().value; +// returns [] + +done = iterator.next().done; +// returns true ``` + @@ -170,17 +158,6 @@ for (const combination of iterator) { @@ -191,21 +168,7 @@ for (const combination of iterator) { [@stdlib/math/base/special/roundn]: https://www.npmjs.com/package/@stdlib/math-base-special-roundn - - -[@stdlib/array/from-iterator]: https://www.npmjs.com/package/@stdlib/array-from-iterator - -[@stdlib/iter/datespace]: https://github.com/stdlib-js/iter/tree/main/datespace - -[@stdlib/iter/incrspace]: https://github.com/stdlib-js/iter/tree/main/incrspace - -[@stdlib/iter/logspace]: https://github.com/stdlib-js/iter/tree/main/logspace - -[@stdlib/iter/step]: https://github.com/stdlib-js/iter/tree/main/step - -[@stdlib/iter/unitspace]: https://github.com/stdlib-js/iter/tree/main/unitspace - diff --git a/node_modules/@stdlib/iter/cartesian-power/docs/repl.txt b/node_modules/@stdlib/iter/cartesian-power/docs/repl.txt index f7d5041c6db..d102273aabb 100644 --- a/node_modules/@stdlib/iter/cartesian-power/docs/repl.txt +++ b/node_modules/@stdlib/iter/cartesian-power/docs/repl.txt @@ -5,7 +5,7 @@ Parameters ---------- - x : ArrayLike | Iterable + x : collection Input array-like object. n: integer diff --git a/node_modules/@stdlib/iter/cartesian-power/lib/index.js b/node_modules/@stdlib/iter/cartesian-power/lib/index.js index fbebfedbbb6..1bf2729fe8c 100644 --- a/node_modules/@stdlib/iter/cartesian-power/lib/index.js +++ b/node_modules/@stdlib/iter/cartesian-power/lib/index.js @@ -26,19 +26,23 @@ * @example * var iterCartesianPower = require( '@stdlib/iter/cartesian-power' ); * -* var x = [ 'a', 'b' ]; -* var n = 2; -* var iterator = iterCartesianPower( x, n ); -* -* var result; -* while ( !( result = iterator.next() ).done ) { -* console.log( result.value ); -* } -* // return -* // [ 'a', 'a' ] -* // [ 'a', 'b' ] -* // [ 'b', 'a' ] -* // [ 'b', 'b' ] +* var iterator = iterCartesianPower( [ 'a', 'b' ], 2 ); +* // returns +* +* var v = iterator.next().value; +* // returns [ 'a', 'a' ] +* +* v = iterator.next().value; +* // returns [ 'a', 'b' ] +* +* v = iterator.next().value; +* // returns [ 'b', 'a' ] +* +* v = iterator.next().value; +* // returns [ 'b', 'b' ] +* +* var bool = iterator.next().done; +* // returns true */ // MODULES // diff --git a/node_modules/@stdlib/iter/cartesian-power/lib/main.js b/node_modules/@stdlib/iter/cartesian-power/lib/main.js index 68ce035ede6..b2361ddf70c 100644 --- a/node_modules/@stdlib/iter/cartesian-power/lib/main.js +++ b/node_modules/@stdlib/iter/cartesian-power/lib/main.js @@ -39,25 +39,30 @@ var format = require( '@stdlib/string/format' ); * @example * var iterCartesianPower = require( '@stdlib/iter/cartesian-power' ); * -* var x = [ 'a', 'b' ]; -* var n = 2; -* var iterator = iterCartesianPower( x, n ); +* var iterator = iterCartesianPower( [ 'a', 'b' ], 2 ); +* // returns * -* var result; -* while ( !( result = iterator.next() ).done ) { -* console.log( result.value ); -* } -* // return -* // [ 'a', 'a' ] -* // [ 'a', 'b' ] -* // [ 'b', 'a' ] -* // [ 'b', 'b' ] +* var v = iterator.next().value; +* // returns [ 'a', 'a' ] +* +* v = iterator.next().value; +* // returns [ 'a', 'b' ] +* +* v = iterator.next().value; +* // returns [ 'b', 'a' ] +* +* v = iterator.next().value; +* // returns [ 'b', 'b' ] +* +* var bool = iterator.next().done; +* // returns true */ function iterCartesianPower( x, n ) { var iter; - var FLG = false; - var i = 0; - var idx = []; + var FLG; + var i; + var idx; + var j; // Validate input arguments if ( !isCollection( x ) ) { @@ -68,12 +73,15 @@ function iterCartesianPower( x, n ) { } // Initialize index array - for ( var k = 0; k < n; k++ ) { + idx = []; + for ( j = 0; j < n; j++ ) { idx.push( 0 ); } // Create an iterator protocol-compliant object iter = {}; + FLG = false; + i = 0; setReadOnly( iter, 'next', next ); setReadOnly( iter, 'return', end ); @@ -90,17 +98,20 @@ function iterCartesianPower( x, n ) { * @returns {Object} iterator protocol-compliant object */ function next() { + var res = []; + var k; + if ( FLG ) { return { 'done': true }; } - var res = []; - for ( var k = 0; k < n; k++ ) { + + for ( k = 0; k < n; k++ ) { res.push( x[ idx[ k ] ] ); } // Update indices - for ( var k = n - 1; k >= 0; k-- ) { + for ( k = n - 1; k >= 0; k-- ) { idx[ k ]++; if ( idx[ k ] === x.length ) { idx[ k ] = 0; diff --git a/node_modules/@stdlib/iter/cartesian-power/package.json b/node_modules/@stdlib/iter/cartesian-power/package.json index 7ccd0d94b89..ee958add31e 100644 --- a/node_modules/@stdlib/iter/cartesian-power/package.json +++ b/node_modules/@stdlib/iter/cartesian-power/package.json @@ -56,12 +56,9 @@ "utility", "utils", "util", - "linspace", - "linear", - "sequence", - "monotonic", - "arange", - "range", + "cartesian", + "combination", + "permutation", "iterator", "iterate", "iteration", diff --git a/node_modules/@stdlib/iter/cartesian-power/test/test.js b/node_modules/@stdlib/iter/cartesian-power/test/test.js index afe457bc8c6..7dd7679966a 100644 --- a/node_modules/@stdlib/iter/cartesian-power/test/test.js +++ b/node_modules/@stdlib/iter/cartesian-power/test/test.js @@ -15,42 +15,72 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + 'use strict'; +// MODULES // + var tape = require( 'tape' ); var iterCartesianPower = require( './../lib' ); +// TESTS // + tape( 'main export is a function', function test( t ) { t.ok( iterCartesianPower instanceof Function, 'main export is a function' ); t.end(); } ); tape( 'the function returns an iterator which generates all possible combinations of length `n` containing elements from the input array', function test( t ) { - var expected = [ [ 'a', 'a' ], [ 'a', 'b' ], [ 'a', 'c' ], [ 'b', 'a' ], [ 'b', 'b' ], [ 'b', 'c' ], [ 'c', 'a' ], [ 'c', 'b' ], [ 'c', 'c' ] ]; - var actual = []; - for ( var v of iterCartesianPower( [ 'a', 'b', 'c' ], 2) ) { - actual.push( v ); + var expected; + var iterator; + var actual; + + iterator = iterCartesianPower( [ 'a', 'b', 'c' ], 2 ); + expected = [ [ 'a', 'a' ], [ 'a', 'b' ], [ 'a', 'c' ], [ 'b', 'a' ], [ 'b', 'b' ], [ 'b', 'c' ], [ 'c', 'a' ], [ 'c', 'b' ], [ 'c', 'c' ] ]; + actual = []; + + var v; + while ( true ) { + v = iterator.next(); + if ( v.done ) { + break; + } + actual.push( v.value ); } t.deepEqual( actual, expected, 'returns expected values' ); t.end(); } ); tape( 'the function returns an iterator which returns an empty array if `n` is 0', function test( t ) { - var iter = iterCartesianPower( [ 'a', 'b', 'c' ], 0 ); - var actual = iter.next().value; + var expected; + var iterator; + var actual; + + var iterator = iterCartesianPower( [ 'a', 'b', 'c' ], 0 ); var expected = []; + var actual = iterator.next().value; t.deepEqual( actual, expected, 'returns expected value' ); t.end(); } ); tape( 'the function returns an iterator which generates all individual elements of the input array when `n` is 1', function test( t ) { - var iter = iterCartesianPower( [ 'a', 'b', 'c' ], 1 ); - var actual = []; - for ( var v of iter ) { - actual.push( v ); + var expected; + var iterator; + var actual; + + iterator = iterCartesianPower( [ 'a', 'b', 'c' ], 1 ); + expected = [ [ 'a' ], [ 'b' ], [ 'c' ] ]; + actual = []; + + var v; + while ( true ) { + v = iterator.next(); + if ( v.done ) { + break; + } + actual.push( v.value ); } - var expected = [ [ 'a' ], [ 'b' ], [ 'c' ] ] t.deepEqual( actual, expected, 'returns expected value' ); t.end(); } ); From ef3ec4140de21201bccaae91b0954b39d4c5b167 Mon Sep 17 00:00:00 2001 From: nourhan hasan Date: Sat, 24 Feb 2024 22:26:25 +0300 Subject: [PATCH 5/7] Updating index.d.ts example --- .../cartesian-power/docs/types/index.d.ts | 30 +++++++++++-------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/node_modules/@stdlib/iter/cartesian-power/docs/types/index.d.ts b/node_modules/@stdlib/iter/cartesian-power/docs/types/index.d.ts index 834629b8b9b..a2f392c5948 100644 --- a/node_modules/@stdlib/iter/cartesian-power/docs/types/index.d.ts +++ b/node_modules/@stdlib/iter/cartesian-power/docs/types/index.d.ts @@ -38,19 +38,23 @@ type Iterator = Iter | IterableIterator; * @example * var iterCartesianPower = require( '@stdlib/iter/cartesian-power' ); * -* var x = [ 'a', 'b' ]; -* var n = 2; -* var iterator = iterCartesianPower( x, n ); -* -* var result; -* while ( !( result = iterator.next() ).done ) { -* console.log( result.value ); -* } -* // return -* // [ 'a', 'a' ] -* // [ 'a', 'b' ] -* // [ 'b', 'a' ] -* // [ 'b', 'b' ] +* var iterator = iterCartesianPower( [ 'a', 'b' ], 2 ); +* // returns +* +* var v = iterator.next().value; +* // returns [ 'a', 'a' ] +* +* v = iterator.next().value; +* // returns [ 'a', 'b' ] +* +* v = iterator.next().value; +* // returns [ 'b', 'a' ] +* +* v = iterator.next().value; +* // returns [ 'b', 'b' ] +* +* var bool = iterator.next().done; +* // returns true */ declare function iterCartesianPower( x: ArrayLike | Iterable, n: number ): Iterator; From b7028a7e541cd157867440b68316b328180ed246 Mon Sep 17 00:00:00 2001 From: nourhan hasan Date: Sun, 25 Feb 2024 12:11:15 +0300 Subject: [PATCH 6/7] Updating benchmark.js --- .../cartesian-power/benchmark/benchmark.js | 35 +++++++++---------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/node_modules/@stdlib/iter/cartesian-power/benchmark/benchmark.js b/node_modules/@stdlib/iter/cartesian-power/benchmark/benchmark.js index df0bb2c7449..fba0083d97b 100644 --- a/node_modules/@stdlib/iter/cartesian-power/benchmark/benchmark.js +++ b/node_modules/@stdlib/iter/cartesian-power/benchmark/benchmark.js @@ -24,26 +24,23 @@ const bench = require( '@stdlib/bench' ); const pkg = require( './../package.json' ).name; const iterCartesianPower = require( './../lib' ); + // MAIN // bench( pkg, function benchmark( b ) { - var arr = [ 'a', 'b', 'c' ]; - var n = 2; - var iterator = iterCartesianPower( arr, n ); - var v; - - // Run the benchmark... - b.tic(); - for ( var i = 0; i < b.iterations; i++ ) { - v = iterator.next(); - if ( v.done ) { - b.fail( 'should iterate over all elements' ); - } - } - b.toc(); - if ( v.done ) { - b.pass( 'benchmark finished' ); - } else { - b.fail( 'should have finished iteration' ); - } + var iterator; + var v; + + iterator = iterCartesianPower( [ 'a', 'b', 'c' ], 2 ); + + b.tic(); + v = iterator.next(); + b.toc(); + + if ( v.done ) { + b.fail( 'should not be done' ); + } else { + b.pass( 'single iteration completed' ); + } + b.end(); } ); From e9d0cbe236bdb98c6a5ad60e5e90355f17216ae3 Mon Sep 17 00:00:00 2001 From: nourhan hasan Date: Sun, 25 Feb 2024 16:18:22 +0300 Subject: [PATCH 7/7] Updating Copyright --- .../cartesian-power/benchmark/benchmark.js | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/node_modules/@stdlib/iter/cartesian-power/benchmark/benchmark.js b/node_modules/@stdlib/iter/cartesian-power/benchmark/benchmark.js index fba0083d97b..1e9188daef7 100644 --- a/node_modules/@stdlib/iter/cartesian-power/benchmark/benchmark.js +++ b/node_modules/@stdlib/iter/cartesian-power/benchmark/benchmark.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2019 The Stdlib Authors. +* 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. @@ -28,19 +28,19 @@ const iterCartesianPower = require( './../lib' ); // MAIN // bench( pkg, function benchmark( b ) { - var iterator; - var v; + var iterator; + var v; iterator = iterCartesianPower( [ 'a', 'b', 'c' ], 2 ); - b.tic(); - v = iterator.next(); - b.toc(); + b.tic(); + v = iterator.next(); + b.toc(); - if ( v.done ) { - b.fail( 'should not be done' ); - } else { - b.pass( 'single iteration completed' ); - } + if ( v.done ) { + b.fail( 'should not be done' ); + } else { + b.pass( 'single iteration completed' ); + } b.end(); } );