Skip to content

feat: add iter/cartesian-product #1399

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

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 139 additions & 0 deletions lib/node_modules/@stdlib/iter/cartesian-product/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
<!--

@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.

-->

# iterCartesianProduct

> Return the [Cartesian product iterator][cartesian-product].

<section class="usage">

## 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);

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.

```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
```

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.

</section>

<!-- /.usage -->

<section class="notes">

## Notes

- If an environment supports `Symbol.iterator` **and** a provided [iterator][mdn-iterator-protocol] is iterable, the returned [iterator][mdn-iterator-protocol] is iterable.

</section>

<!-- /.notes -->

<section class="examples">

## Examples

<!-- eslint no-undef: "error" -->

```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 ]....
```

</section>

<!-- /.examples -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">

## See Also

- <span class="package-name">[`@stdlib/array/cartesian-product`][@stdlib/array/cartesian-product]</span><span class="delimiter">: </span><span class="description">return the Cartesian Product Array.</span>

</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">

[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

<!-- <related-links> -->

[@stdlib/array/cartesian-product]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/cartesian-product

<!-- </related-links> -->

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
* @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 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' );


// MAIN //

bench( pkg, 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();
});

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();
});
39 changes: 39 additions & 0 deletions lib/node_modules/@stdlib/iter/cartesian-product/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -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
--------
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.
*/

/* eslint-disable @typescript-eslint/no-explicit-any */

// TypeScript Version: 4.1

/// <reference types="@stdlib/types"/>

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<any>, x2: Array<any> ): Iterator;


// EXPORTS //

export = iterCartesianProduct;
56 changes: 56 additions & 0 deletions lib/node_modules/@stdlib/iter/cartesian-product/docs/types/test.ts
Original file line number Diff line number Diff line change
@@ -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.
*/

import iterCartesianProduct = require( './index' );


// TESTS //

// The function returns an iterator...
{
iterCartesianProduct( [ 1, 2, 3 ], [ 4, 5, 6 ] ); // $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...
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a case for string, boolean, null and undefined to both arguments.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not resolved yet, I meant we need to add the case for these dtypes for the error cases rather than the correct case

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is still not resolved

{
iterCartesianProduct( 2, [ 4, 5, 6 ] ); // $ExpectError
iterCartesianProduct( false, [ 4, 5, 6 ] ); // $ExpectError
iterCartesianProduct( true, [ 4, 5, 6 ] ); // $ExpectError
iterCartesianProduct( {}, [ 4, 5, 6 ] ); // $ExpectError
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous comment meant to add cases where the first argument is string, boolean, null and undefined

Suggested change
iterCartesianProduct( {}, [ 4, 5, 6 ] ); // $ExpectError
iterCartesianProduct( {}, [ 4, 5, 6 ] ); // $ExpectError
iterCartesianProduct( 'a', [ 4, 5, 6 ] ); // $ExpectError
iterCartesianProduct( null, [ 4, 5, 6 ] ); // $ExpectError
iterCartesianProduct( undefined, [ 4, 5, 6 ] ); // $ExpectError

Please do the same for the other test cases.

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
}
Loading