Skip to content

feat: add JavaScript implementation for iter/cartesian-power #2534

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
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
168 changes: 168 additions & 0 deletions lib/node_modules/@stdlib/iter/cartesian-power/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
<!--

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

-->

# iterCartesianPower

> Create an iterator which returns the Cartesian power.

<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->

<section class="intro">

</section>

<!-- /.intro -->

<!-- Package usage documentation. -->

<section class="usage">

## Usage

```javascript
var iterCartesianPower = require( '@stdlib/iter/cartesian-power' );
```

#### iterCartesianPower( x, n )

Returns an iterator which returns the Cartesian power.

```javascript
var iterator = iterCartesianPower( [ 'a', 'b', 'c' ], 2 );

var v;
while ( true ) {
v = iterator.next();
if ( v.done ) {
break;
}
console.log( v.value );
}
```

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

</section>

<!-- /.notes -->

<!-- Package usage examples. -->

<section class="examples">

## Examples

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

```javascript
// Example 1: Generating Cartesian power of an array with n = 2
var iterator = iterCartesianPower( [ 1, 2, 3 ], 2 );
// returns <Object>

var v = iterator.next().value;
// returns [ 1, 1 ]

v = iterator.next().value;
// returns [ 1, 2 ]

v = iterator.next().value;
// returns [ 1, 3 ]

v = iterator.next().value;
// returns [ 2, 1 ]

v = iterator.next().value;
// returns [ 2, 2 ]

v = iterator.next().value;
// returns [ 2, 3 ]

v = iterator.next().value;
// returns [ 3, 1 ]

v = iterator.next().value;
// returns [ 3, 2 ]

v = iterator.next().value;
// returns [ 3, 3 ]

var done = iterator.next().done;
// returns true

// Example 2: Generating Cartesian power of an array with n = 1
iterator = iterCartesianPower( [ 'a', 'b', 'c' ], 1 );
// returns <Object>

v = iterator.next().value;
// returns [ 'a' ]

v = iterator.next().value;
// returns [ 'b' ]

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

v = iterator.next().value;
// returns []

done = iterator.next().done;
// returns true
```

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

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -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';

// MODULES //

var bench = require( '@stdlib/bench' );
var pkg = require( './../package.json' ).name;
var iterCartesianPower = require( './../lib' );


// MAIN //

bench( pkg, function benchmark( b ) {
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();
} );
38 changes: 38 additions & 0 deletions lib/node_modules/@stdlib/iter/cartesian-power/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{{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: collection
Input collection.

n: NonNegativeInteger
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
--------
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* @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 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
* var iterCartesianPower = require( '@stdlib/iter/cartesian-power' );
*
* var iterator = iterCartesianPower( [ 'a', 'b' ], 2 );
* // returns <Object>
*
* 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<T>( x: ArrayLike<T> | Iterable<T>, n: number ): Iterator;

// EXPORTS //

export = iterCartesianPower;
57 changes: 57 additions & 0 deletions lib/node_modules/@stdlib/iter/cartesian-power/docs/types/test.ts
Original file line number Diff line number Diff line change
@@ -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
}
Loading
Loading