Skip to content

Commit 6c1f6b3

Browse files
feat(iter/cartesian-square): add lib/main.js
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent 06b39a8 commit 6c1f6b3

1 file changed

Lines changed: 139 additions & 0 deletions

File tree

  • lib/node_modules/@stdlib/iter/cartesian-square/lib
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' );
24+
var isFunction = require( '@stdlib/assert/is-function' );
25+
var isCollection = require( '@stdlib/assert/is-collection' );
26+
var iteratorSymbol = require( '@stdlib/symbol/iterator' );
27+
var format = require( '@stdlib/string/format' );
28+
var resolveGetter = require( '@stdlib/array/base/resolve-getter' );
29+
var floor = require( '@stdlib/math/base/special/floor' );
30+
31+
32+
// MAIN //
33+
34+
/**
35+
* Returns an iterator of the Cartesian square.
36+
*
37+
* @param {Collection} x - input array
38+
* @throws {TypeError} first argument must be a collection
39+
* @returns {Iterator} iterator of ordered tuples comprising the Cartesian product
40+
*
41+
* @example
42+
* var x = [ 1, 2 ];
43+
*
44+
* var iter = iterCartesianSquare( x );
45+
*
46+
* var v = iter.next().value;
47+
* // returns [ 1, 1 ]
48+
*
49+
* v = iter.next().value;
50+
* // returns [ 1, 2 ]
51+
*
52+
* v = iter.next().value;
53+
* // returns [ 2, 1 ]
54+
*
55+
* // ...
56+
*/
57+
function iterCartesianSquare( x ) {
58+
var iter;
59+
var FLG;
60+
var get;
61+
var i;
62+
if ( !isCollection( x ) ) {
63+
throw new TypeError( format( 'invalid argument. First argument must be an array-like object. Value: `%s`.', x ) );
64+
}
65+
get = resolveGetter( x );
66+
i = 0;
67+
68+
// Create an iterator protocol-compliant object:
69+
iter = {};
70+
setReadOnly( iter, 'next', next );
71+
setReadOnly( iter, 'return', end );
72+
73+
// If an environment supports `Symbol.iterator` and a provided iterator is iterable, make the iterator iterable:
74+
if ( iteratorSymbol && isFunction( x[ iteratorSymbol ] ) ) {
75+
setReadOnly( iter, iteratorSymbol, factory );
76+
}
77+
return iter;
78+
79+
/**
80+
* Returns an iterator protocol-compliant object containing the next iterated value.
81+
*
82+
* @private
83+
* @returns {Object} iterator protocol-compliant object
84+
*/
85+
function next() {
86+
var N;
87+
var v;
88+
if ( FLG ) {
89+
return {
90+
'done': true
91+
};
92+
}
93+
N = x.length * x.length;
94+
v = [ get(x, floor(i / x.length)), get(x, i % x.length) ];
95+
i += 1;
96+
if ( i >= N ) {
97+
FLG = true;
98+
}
99+
return {
100+
'value': v,
101+
'done': false
102+
};
103+
}
104+
105+
/**
106+
* Finishes an iterator.
107+
*
108+
* @private
109+
* @param {*} [value] - value to return
110+
* @returns {Object} iterator protocol-compliant object
111+
*/
112+
function end( value ) {
113+
FLG = true;
114+
if ( arguments.length ) {
115+
return {
116+
'value': value,
117+
'done': true
118+
};
119+
}
120+
return {
121+
'done': true
122+
};
123+
}
124+
125+
/**
126+
* Returns a new iterator.
127+
*
128+
* @private
129+
* @returns {Iterator} iterator
130+
*/
131+
function factory() {
132+
return iterCartesianSquare( x[ iteratorSymbol ]() );
133+
}
134+
}
135+
136+
137+
// EXPORTS //
138+
139+
module.exports = iterCartesianSquare;

0 commit comments

Comments
 (0)