Skip to content

Commit 4a651c7

Browse files
committed
feat: add array/base/zip2views
--- 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: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - 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: passed - task: lint_license_headers status: passed ---
1 parent 75a09fd commit 4a651c7

File tree

11 files changed

+1114
-0
lines changed

11 files changed

+1114
-0
lines changed
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# zip2views
22+
23+
> Zip one or more arrays to an array of composite views.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var zip2views = require( '@stdlib/array/base/zip2views' );
31+
```
32+
33+
#### zip2views( arrays, labels )
34+
35+
Zips one or more arrays to an array of composite views.
36+
37+
```javascript
38+
var x = [ 1, 2, 3 ];
39+
var y = [ 'a', 'b', 'c' ];
40+
41+
var labels = [ 'x', 'y' ];
42+
43+
var z = zip2views( [ x, y ], labels );
44+
// returns [ <Object>, <Object>, <Object> ]
45+
46+
var v0 = z[ 0 ].toJSON();
47+
// returns { 'x': 1, 'y': 'a' }
48+
49+
var v1 = z[ 1 ].toJSON();
50+
// returns { 'x': 2, 'y': 'b' }
51+
52+
var v2 = z[ 2 ].toJSON();
53+
// returns { 'x': 3, 'y': 'c' }
54+
55+
// Mutate one of the input arrays:
56+
x[ 0 ] = 5;
57+
58+
v0 = z[ 0 ].toJSON();
59+
// returns { 'x': 5, 'y': 'a' }
60+
61+
// Set a view property:
62+
z[ 1 ].y = 'beep';
63+
64+
v1 = z[ 1 ].toJSON();
65+
// returns { 'x': 2, 'y': 'beep' }
66+
67+
y1 = y.slice();
68+
// returns [ 'a', 'beep', 'c' ]
69+
```
70+
71+
The function supports the following parameters:
72+
73+
- **arrays**: list of arrays to zip.
74+
- **labels**: list of array labels.
75+
76+
Each element in the returned array is a class instance having prototype properties corresponding to the list of array labels. As demonstrated in the example above, to convert an element to a regular object, invoke an element's `toJSON` method. Note, however, that the object returned by an element's `toJSON` method no longer shares the same memory as the provided input arrays.
77+
78+
</section>
79+
80+
<!-- /.usage -->
81+
82+
<section class="notes">
83+
84+
- The function assumes that the list of arrays to be zipped all have the same length.
85+
- The number of provided array labels should equal the number of arrays to be zipped.
86+
- Each view in the returned array shares the same memory as the corresponding elements in the input arrays. Accordingly, mutation of either an input array or a view will mutate the other.
87+
88+
</section>
89+
90+
<!-- /.notes -->
91+
92+
<section class="examples">
93+
94+
## Examples
95+
96+
<!-- eslint no-undef: "error" -->
97+
98+
```javascript
99+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
100+
var zeroTo = require( '@stdlib/array/base/zero-to' );
101+
var zip2views = require( '@stdlib/array/base/zip2views' );
102+
103+
var x = zeroTo( 10 );
104+
var y = discreteUniform( x.length, -100, 100 );
105+
106+
var labels = [ 'x', 'y' ];
107+
108+
var out = zip2views( [ x, y ], labels );
109+
// returns [...]
110+
```
111+
112+
</section>
113+
114+
<!-- /.examples -->
115+
116+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
117+
118+
<section class="related">
119+
120+
</section>
121+
122+
<!-- /.related -->
123+
124+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
125+
126+
<section class="links">
127+
128+
</section>
129+
130+
<!-- /.links -->
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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 bench = require( '@stdlib/bench' );
24+
var isArray = require( '@stdlib/assert/is-array' );
25+
var zeroTo = require( '@stdlib/array/base/zero-to' );
26+
var pkg = require( './../package.json' ).name;
27+
var zip2views = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg+':len=100', function benchmark( b ) {
33+
var labels;
34+
var x;
35+
var i;
36+
var v;
37+
38+
labels = [ 'x', 'y' ];
39+
x = zeroTo( 10 );
40+
41+
b.tic();
42+
for ( i = 0; i < b.iterations; i++ ) {
43+
v = zip2views( [ x, x ], labels );
44+
if ( typeof v !== 'object' ) {
45+
b.fail( 'should return an array' );
46+
}
47+
}
48+
b.toc();
49+
if ( !isArray( v ) ) {
50+
b.fail( 'should return an array' );
51+
}
52+
b.pass( 'benchmark finished' );
53+
b.end();
54+
});
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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 bench = require( '@stdlib/bench' );
24+
var pow = require( '@stdlib/math/base/special/pow' );
25+
var zeroTo = require( '@stdlib/array/base/zero-to' );
26+
var isArray = require( '@stdlib/assert/is-array' );
27+
var pkg = require( './../package.json' ).name;
28+
var zip2views = require( './../lib' );
29+
30+
31+
// FUNCTIONS //
32+
33+
/**
34+
* Creates a benchmark function.
35+
*
36+
* @private
37+
* @param {PositiveInteger} len - array length
38+
* @returns {Function} benchmark function
39+
*/
40+
function createBenchmark( len ) {
41+
var labels = [ 'x', 'y' ];
42+
var x = zeroTo( len );
43+
return benchmark;
44+
45+
/**
46+
* Benchmark function.
47+
*
48+
* @private
49+
* @param {Benchmark} b - benchmark instance
50+
*/
51+
function benchmark( b ) {
52+
var v;
53+
var i;
54+
55+
b.tic();
56+
for ( i = 0; i < b.iterations; i++ ) {
57+
v = zip2views( [ x, x ], labels );
58+
if ( typeof v !== 'object' ) {
59+
b.fail( 'should return an array' );
60+
}
61+
}
62+
b.toc();
63+
if ( !isArray( v ) ) {
64+
b.fail( 'should return an array' );
65+
}
66+
b.pass( 'benchmark finished' );
67+
b.end();
68+
}
69+
}
70+
71+
72+
// MAIN //
73+
74+
/**
75+
* Main execution sequence.
76+
*
77+
* @private
78+
*/
79+
function main() {
80+
var len;
81+
var min;
82+
var max;
83+
var f;
84+
var i;
85+
86+
min = 1; // 10^min
87+
max = 6; // 10^max
88+
89+
for ( i = min; i <= max; i++ ) {
90+
len = pow( 10, i );
91+
f = createBenchmark( len );
92+
bench( pkg+':len='+len, f );
93+
}
94+
}
95+
96+
main();
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
{{alias}}( arrays, labels )
3+
Zips one or more arrays to an array of composite views.
4+
5+
The function assumes that the list of arrays to be zipped all have the same
6+
length.
7+
8+
The number of provided array labels should equal the number of arrays to be
9+
zipped.
10+
11+
Each view in the returned array shares the same memory as the corresponding
12+
elements in the input arrays. Accordingly, mutation of either an input array
13+
or a view will mutate the other.
14+
15+
Parameters
16+
----------
17+
arrays: ArrayLikeObject<ArrayLikeObject>
18+
List of arrays to zip.
19+
20+
labels: ArrayLikeObject
21+
List of array labels.
22+
23+
Returns
24+
-------
25+
out: Array<Object>
26+
Output array.
27+
28+
Examples
29+
--------
30+
> var x = [ 1, 2 ];
31+
> var y = [ 3, 4 ];
32+
> var labels = [ 'x', 'y' ];
33+
> var out = {{alias}}( [ x, y ], labels );
34+
> var v = out[ 0 ].toJSON()
35+
{ 'x': 1, 'y': 3 }
36+
> v = out[ 1 ].toJSON()
37+
{ 'x': 2, 'y': 4 }
38+
39+
See Also
40+
--------
41+

0 commit comments

Comments
 (0)