-
-
Notifications
You must be signed in to change notification settings - Fork 828
feat: add iter/cartesian-square
#1371
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
vinayak-00017
wants to merge
4
commits into
stdlib-js:develop
Choose a base branch
from
vinayak-00017:feature/iterCartesianSquare
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b902843
feat(lib/node_modules/@stdlib/iter/cartesian-square): added iterCarte…
vinayak-00017 deb1b07
feat(/lib/node_modules/@stdlib/iter/cartesian-square): added docs folder
vinayak-00017 e0c038c
fix(lib/node_modules/@stdlib/iter/cartesian-square): resolved issues …
vinayak-00017 d44610d
fix(lib/node_modules/@stdlib/iter/cartesian-square): resolved issues …
vinayak-00017 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
133 changes: 133 additions & 0 deletions
133
lib/node_modules/@stdlib/iter/cartesian-square/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
<!-- | ||
|
||
@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. | ||
|
||
--> | ||
|
||
# iterCartesianSquare | ||
|
||
> Create an iterator which generates the Cartesian square of an input array-like object. | ||
|
||
<section class="intro"> | ||
|
||
</section> | ||
|
||
<section class="usage"> | ||
|
||
## Usage | ||
|
||
```javascript | ||
var iterCartesianSquare = require( '@stdlib/iter/cartesian-square' ); | ||
``` | ||
|
||
#### iterCartesianSquare( x ) | ||
|
||
Returns an iterator which generates the Cartesian Square of an input array-like object. | ||
|
||
```javascript | ||
var x = ['a', 'b', 'c']; | ||
var pair; | ||
var cartesianSquare = iterCartesianSquare( x, n ); | ||
|
||
for ( pair of cartesianSquare ) { | ||
console.log( pair ); | ||
} | ||
|
||
``` | ||
|
||
</section> | ||
|
||
<!-- /.usage --> | ||
|
||
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||
|
||
<section class="notes"> | ||
|
||
## Notes | ||
|
||
The function expects only one argument x, an array-like object. | ||
The returned iterator will generate all possible combinations from the input array x. | ||
vinayak-00017 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
</section> | ||
|
||
<!-- /.notes --> | ||
|
||
<!-- Package usage examples. --> | ||
|
||
<section class="examples"> | ||
|
||
## Examples | ||
|
||
|
||
<!-- eslint no-undef: "error" --> | ||
|
||
```javascript | ||
// Example : Generating Cartesian square of an array | ||
var x = [1, 2, 3]; | ||
var cartesianSquare = iterCartesianSquare( x ); | ||
var pair; | ||
|
||
for ( pair of cartesianSquare ) { | ||
vinayak-00017 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
console.log( pair ); | ||
} | ||
|
||
// Output: | ||
// [ 1, 1 ] | ||
// [ 1, 2 ] | ||
// [ 1, 3 ] | ||
// [ 2, 1 ] | ||
// [ 2, 2 ] | ||
// [ 2, 3 ] | ||
// [ 3, 1 ] | ||
// [ 3, 2 ] | ||
// [ 3, 3 ] | ||
|
||
``` | ||
|
||
</section> | ||
|
||
<!-- /.examples --> | ||
|
||
<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||
|
||
<section class="references"> | ||
|
||
</section> | ||
|
||
<!-- /.references --> | ||
|
||
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> | ||
|
||
<section class="related"> | ||
|
||
|
||
</section> | ||
|
||
<!-- /.related --> | ||
|
||
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||
|
||
<section class="links"> | ||
|
||
<!-- <related-links> --> | ||
|
||
|
||
<!-- </related-links> --> | ||
|
||
</section> | ||
|
||
<!-- /.links --> |
59 changes: 59 additions & 0 deletions
59
lib/node_modules/@stdlib/iter/cartesian-square/benchmark/benchmark.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
|
||
/** | ||
* @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 pkg = require('./../package.json').name; | ||
var iterCartesianSquare = require('./../lib'); | ||
|
||
|
||
// MAIN // | ||
|
||
bench( pkg, function benchmark( b ) { | ||
var iterator; | ||
var arr = ['a', 'b', 'c']; | ||
var v; | ||
var i; | ||
|
||
// Run the benchmark... | ||
vinayak-00017 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
b.tic(); | ||
for (i = 0; i < b.iterations; i++) { | ||
// Reset the iterator: | ||
iterator = iterCartesianSquare(arr); | ||
v = iterator.next(); | ||
|
||
// Iterate over the Cartesian square: | ||
while (!v.done) { | ||
if (v.done) { | ||
b.fail('should iterate over all elements'); | ||
} | ||
v = iterator.next(); | ||
} | ||
vinayak-00017 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
b.toc(); | ||
if (v.done) { | ||
b.pass('benchmark finished'); | ||
} else { | ||
b.fail('should have finished iteration'); | ||
} | ||
b.end(); | ||
}); |
33 changes: 33 additions & 0 deletions
33
lib/node_modules/@stdlib/iter/cartesian-square/docs/repl.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{{alias}}( x ) | ||
Returns an iterator which generates the Cartesian square of an input | ||
array-like object. If an environment supports Symbol.iterator, the returned | ||
iterator is iterable. | ||
vinayak-00017 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Parameters | ||
---------- | ||
x : collection | ||
vinayak-00017 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Input array-like object. | ||
|
||
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}}( [ '1', '2', '3' ] ) | ||
> var v = it.next().value | ||
[ '1', '1' ] | ||
> v = it.next().value | ||
[ '1', '2' ] | ||
|
||
See Also | ||
-------- |
60 changes: 60 additions & 0 deletions
60
lib/node_modules/@stdlib/iter/cartesian-square/docs/types/index.d.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. | ||
*/ | ||
|
||
// TypeScript Version: 4.1 | ||
|
||
/// <reference types="@stdlib/types"/> | ||
|
||
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; | ||
|
||
/** | ||
* Returns an iterator which generates the Cartesian square of an input array-like object. | ||
* | ||
* @param x - Input array-like object. | ||
* @throws {TypeError} first argument must be an array-like object | ||
* @returns iterator | ||
* | ||
* @example | ||
* var iterCartesianSquare = require( '@stdlib/iter/cartesian-square' ); | ||
* | ||
* var iterator = iterCartesianSquare( [ 'a', 'b', 'c' ] ); | ||
* // returns <Object> | ||
* | ||
* var v = iterator.next().value; | ||
* // returns [ 'a', 'a' ] | ||
* | ||
* v = iterator.next().value; | ||
* // returns [ 'a', 'b' ] | ||
* | ||
* v = iterator.next().value; | ||
* // returns [ 'a', 'c' ] | ||
* | ||
* // ... | ||
* | ||
* var bool = iterator.next().done; | ||
* // returns true | ||
*/ | ||
declare function iterCartesianSquare<T>( x: ArrayLike<T> | Iterable<T> ): Iterator; | ||
vinayak-00017 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// EXPORTS // | ||
vinayak-00017 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
export = iterCartesianSquare; |
43 changes: 43 additions & 0 deletions
43
lib/node_modules/@stdlib/iter/cartesian-square/docs/types/test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* @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 iterCartesianSquare = require( './index' ); | ||
|
||
// TESTS // | ||
|
||
// The function returns an iterator... | ||
{ | ||
iterCartesianSquare( [ 'a', 'b', 'c' ] ); // $ExpectType Iterator | ||
iterCartesianSquare( [ 1, 2, 3 ] ); // $ExpectType Iterator | ||
} | ||
|
||
// The compiler throws an error if the function is provided an argument which is not an array-like object... | ||
{ | ||
iterCartesianSquare( 5 ); // $ExpectError | ||
iterCartesianSquare( true ); // $ExpectError | ||
iterCartesianSquare( false ); // $ExpectError | ||
iterCartesianSquare( null ); // $ExpectError | ||
iterCartesianSquare( {} ); // $ExpectError | ||
iterCartesianSquare( ( x: number ): number => x ); // $ExpectError | ||
} | ||
|
||
// The compiler throws an error if the function is provided insufficient arguments... | ||
{ | ||
iterCartesianSquare(); // $ExpectError | ||
} | ||
|
35 changes: 35 additions & 0 deletions
35
lib/node_modules/@stdlib/iter/cartesian-square/examples/index.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
|
||
/** | ||
* @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 iterCartesianSquare = require( './../lib' ); | ||
|
||
// Create an array: | ||
var arr = [ 'a', 'b', 'c' ]; | ||
|
||
// Create an iterator: | ||
var iter = iterCartesianSquare( arr ); | ||
|
||
// Iterate over the Cartesian square: | ||
var v = iter.next(); | ||
while ( !v.done ) { | ||
console.log( v.value ); | ||
v = iter.next(); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.