Skip to content

Commit 2250000

Browse files
committed
feat: add array/base/take-indexed2
--- 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 07906c5 commit 2250000

File tree

11 files changed

+729
-0
lines changed

11 files changed

+729
-0
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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+
# takeIndexed2
22+
23+
> Take elements from two indexed arrays in a single pass.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var takeIndexed2 = require( '@stdlib/array/base/take-indexed2' );
31+
```
32+
33+
#### takeIndexed2( x, y, indices )
34+
35+
Takes elements from two indexed arrays in a single pass.
36+
37+
```javascript
38+
var x = [ 1, 2, 3, 4 ];
39+
var y = [ 5, 6, 7, 8 ];
40+
41+
var out = takeIndexed2( x, y, [ 1, 3 ] );
42+
// returns [ [ 2, 4 ], [ 6, 8 ] ]
43+
```
44+
45+
If `indices` is an empty array, the function returns empty arrays.
46+
47+
```javascript
48+
var x = [ 1, 2, 3, 4 ];
49+
var y = [ 5, 6, 7, 8 ];
50+
51+
var out = takeIndexed2( x, y, [] );
52+
// returns [ [], [] ]
53+
```
54+
55+
</section>
56+
57+
<!-- /.usage -->
58+
59+
<section class="notes">
60+
61+
## Notes
62+
63+
- The function does **not** perform bounds checking. If an index is less than zero or greater than the maximum index of `x` or `y`, the value of the corresponding element in the respective output array is undefined.
64+
- An _indexed_ array-like object is a data structure in which one retrieves elements via integer indices using bracket `[]` notation (e.g., `Float64Array`, `Int32Array`, `Array`, etc). This is in contrast to an _accessor_ array-like object in which one retrieves elements using `get` and `set` methods (e.g., `Complex64Array` and `Complex128Array`).
65+
66+
</section>
67+
68+
<!-- /.notes -->
69+
70+
<section class="examples">
71+
72+
## Examples
73+
74+
<!-- eslint no-undef: "error" -->
75+
76+
```javascript
77+
var filledBy = require( '@stdlib/array/base/filled-by' );
78+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
79+
var linspace = require( '@stdlib/array/base/linspace' );
80+
var takeIndexed2 = require( '@stdlib/array/base/take-indexed2' );
81+
82+
// Generate linearly spaced arrays:
83+
var x = linspace( 0, 100, 11 );
84+
console.log( x );
85+
86+
var y = linspace( 100, 200, 11 );
87+
console.log( y );
88+
89+
// Generate an array of random indices:
90+
var N = discreteUniform( 5, 15 );
91+
var indices = filledBy( N, discreteUniform.factory( 0, x.length-1 ) );
92+
console.log( indices );
93+
94+
// Take a random sample of elements from `x` and `y`:
95+
var out = takeIndexed2( x, y, indices );
96+
console.log( out );
97+
```
98+
99+
</section>
100+
101+
<!-- /.examples -->
102+
103+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
104+
105+
<section class="related">
106+
107+
</section>
108+
109+
<!-- /.related -->
110+
111+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
112+
113+
<section class="links">
114+
115+
</section>
116+
117+
<!-- /.links -->
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 isArrayArray = require( '@stdlib/assert/is-array-array' );
25+
var zeroTo = require( '@stdlib/array/base/zero-to' );
26+
var pkg = require( './../package.json' ).name;
27+
var take2 = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg+'::copy:len=100', function benchmark( b ) {
33+
var x;
34+
var i;
35+
var v;
36+
37+
x = zeroTo( 100 );
38+
39+
b.tic();
40+
for ( i = 0; i < b.iterations; i++ ) {
41+
v = take2( x, x, x );
42+
if ( typeof v !== 'object' ) {
43+
b.fail( 'should return an array' );
44+
}
45+
}
46+
b.toc();
47+
if ( !isArrayArray( v ) ) {
48+
b.fail( 'should return an array of arrays' );
49+
}
50+
b.pass( 'benchmark finished' );
51+
b.end();
52+
});
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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 filledBy = require( '@stdlib/array/base/filled-by' );
26+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory;
27+
var isArrayArray = require( '@stdlib/assert/is-array-array' );
28+
var pkg = require( './../package.json' ).name;
29+
var take2 = require( './../lib' );
30+
31+
32+
// FUNCTIONS //
33+
34+
/**
35+
* Creates a benchmark function.
36+
*
37+
* @private
38+
* @param {PositiveInteger} len - array length
39+
* @returns {Function} benchmark function
40+
*/
41+
function createBenchmark( len ) {
42+
var idx = filledBy( len, discreteUniform( 0, 3 ) );
43+
return benchmark;
44+
45+
/**
46+
* Benchmark function.
47+
*
48+
* @private
49+
* @param {Benchmark} b - benchmark instance
50+
*/
51+
function benchmark( b ) {
52+
var x;
53+
var v;
54+
var i;
55+
56+
x = [ 1, 2, 3, 4 ];
57+
58+
b.tic();
59+
for ( i = 0; i < b.iterations; i++ ) {
60+
v = take2( x, x, idx );
61+
if ( typeof v !== 'object' ) {
62+
b.fail( 'should return an array' );
63+
}
64+
}
65+
b.toc();
66+
if ( !isArrayArray( v ) ) {
67+
b.fail( 'should return an array of arrays' );
68+
}
69+
b.pass( 'benchmark finished' );
70+
b.end();
71+
}
72+
}
73+
74+
75+
// MAIN //
76+
77+
/**
78+
* Main execution sequence.
79+
*
80+
* @private
81+
*/
82+
function main() {
83+
var len;
84+
var min;
85+
var max;
86+
var f;
87+
var i;
88+
89+
min = 1; // 10^min
90+
max = 6; // 10^max
91+
92+
for ( i = min; i <= max; i++ ) {
93+
len = pow( 10, i );
94+
f = createBenchmark( len );
95+
bench( pkg+':len='+len, f );
96+
}
97+
}
98+
99+
main();
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
{{alias}}( x, y, indices )
3+
Takes elements from two indexed arrays in a single pass.
4+
5+
If `indices` is an empty array, the function returns empty arrays.
6+
7+
The function does *not* perform bounds checking. If an index is less than
8+
zero or greater than the maximum index of `x` or `y`, the value of the
9+
corresponding element in the respective output array is undefined.
10+
11+
Parameters
12+
----------
13+
x: ArrayLikeObject
14+
First input array.
15+
16+
y: ArrayLikeObject
17+
Second input array.
18+
19+
indices: ArrayLikeObject<integer>
20+
List of element indices.
21+
22+
Returns
23+
-------
24+
out: Array<Array>
25+
Output arrays.
26+
27+
Examples
28+
--------
29+
> var x = [ 1, 2, 3, 4 ];
30+
> var y = [ 5, 6, 7, 8 ];
31+
> var out = {{alias}}( x, y, [ 1, 3 ] )
32+
[ [ 2, 4 ], [ 6, 8 ] ]
33+
34+
See Also
35+
--------
36+
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
// TypeScript Version: 4.1
20+
21+
/// <reference types="@stdlib/types"/>
22+
23+
import { Collection } from '@stdlib/types/array';
24+
25+
/**
26+
* Takes elements from two indexed arrays in a single pass.
27+
*
28+
* ## Notes
29+
*
30+
* - The function does **not** perform bounds checking. If an index is less than zero or greater than the maximum index of `x` or `y`, the value of the corresponding element in the respective output array is undefined.
31+
*
32+
* @param x - first input array
33+
* @param y - second input array
34+
* @param indices - list of element indices
35+
* @returns output arrays
36+
*
37+
* @example
38+
* var x = [ 1, 2, 3, 4 ];
39+
* var y = [ 5, 6, 7, 8 ];
40+
* var indices = [ 3, 1, 2, 0 ];
41+
*
42+
* var out = take2( x, y, indices );
43+
* // returns [ [ 4, 2, 3, 1 ], [ 8, 6, 7, 5 ] ]
44+
*/
45+
declare function take2<T = unknown, U = unknown>( x: Collection<T>, y: Collection<U>, indices: Collection<number> ): [ Array<T>, Array<U> ];
46+
47+
48+
// EXPORTS //
49+
50+
export = take2;

0 commit comments

Comments
 (0)