Skip to content

Commit 6670980

Browse files
committed
feat: add array/base/take-indexed
1 parent 6ece459 commit 6670980

File tree

11 files changed

+680
-0
lines changed

11 files changed

+680
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2023 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+
# takeIndexed
22+
23+
> Take elements from an indexed array.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var takeIndexed = require( '@stdlib/array/base/take-indexed' );
31+
```
32+
33+
#### takeIndexed( x, indices )
34+
35+
Takes elements from an indexed array.
36+
37+
```javascript
38+
var x = [ 1, 2, 3, 4 ];
39+
40+
var y = takeIndexed( x, [ 1, 3 ] );
41+
// returns [ 2, 4 ]
42+
```
43+
44+
If `indices` is an empty array, the function returns an empty array.
45+
46+
```javascript
47+
var x = [ 1, 2, 3, 4 ];
48+
49+
var y = takeIndexed( x, [] );
50+
// returns []
51+
```
52+
53+
</section>
54+
55+
<!-- /.usage -->
56+
57+
<section class="notes">
58+
59+
## Notes
60+
61+
- The function does **not** perform bounds checking. If an index is less than zero or greater than the maximum index of `x`, the value of the corresponding element in the output array is undefined.
62+
- 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`).
63+
64+
</section>
65+
66+
<!-- /.notes -->
67+
68+
<section class="examples">
69+
70+
## Examples
71+
72+
<!-- eslint no-undef: "error" -->
73+
74+
```javascript
75+
var filledBy = require( '@stdlib/array/base/filled-by' );
76+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
77+
var linspace = require( '@stdlib/array/base/linspace' );
78+
var takeIndexed = require( '@stdlib/array/base/take-indexed' );
79+
80+
// Generate a linearly spaced array:
81+
var x = linspace( 0, 100, 11 );
82+
83+
// Generate an array of random indices:
84+
var N = discreteUniform( 5, 15 );
85+
var indices = filledBy( N, discreteUniform.factory( 0, x.length-1 ) );
86+
87+
// Take a random sample of elements from `x`:
88+
var y = takeIndexed( x, indices );
89+
90+
console.log( x );
91+
console.log( indices );
92+
console.log( y );
93+
```
94+
95+
</section>
96+
97+
<!-- /.examples -->
98+
99+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
100+
101+
<section class="related">
102+
103+
</section>
104+
105+
<!-- /.related -->
106+
107+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
108+
109+
<section class="links">
110+
111+
</section>
112+
113+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 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 take = 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 = take( x, x );
42+
if ( typeof v !== 'object' ) {
43+
b.fail( 'should return an array' );
44+
}
45+
}
46+
b.toc();
47+
if ( !isArray( v ) ) {
48+
b.fail( 'should return an array' );
49+
}
50+
b.pass( 'benchmark finished' );
51+
b.end();
52+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 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 isArray = require( '@stdlib/assert/is-array' );
28+
var pkg = require( './../package.json' ).name;
29+
var take = 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 = take( x, idx );
61+
if ( typeof v !== 'object' ) {
62+
b.fail( 'should return an array' );
63+
}
64+
}
65+
b.toc();
66+
if ( !isArray( v ) ) {
67+
b.fail( 'should return an array' );
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();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
{{alias}}( x, indices )
3+
Takes elements from an indexed array.
4+
5+
If `indices` is an empty array, the function returns an empty array.
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`, the value of the
9+
corresponding element in the output array is undefined.
10+
11+
Parameters
12+
----------
13+
x: ArrayLikeObject
14+
Input array.
15+
16+
indices: ArrayLikeObject<integer>
17+
List of element indices.
18+
19+
Returns
20+
-------
21+
out: Array
22+
Output array.
23+
24+
Examples
25+
--------
26+
> var x = [ 1, 2, 3, 4 ];
27+
> var y = {{alias}}( x, [ 1, 3 ] )
28+
[ 2, 4 ]
29+
30+
See Also
31+
--------
32+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 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 element from an indexed array.
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`, the value of the corresponding element in the output array is undefined.
31+
*
32+
* @param x - input array
33+
* @param indices - list of element indices
34+
* @returns output array
35+
*
36+
* @example
37+
* var x = [ 1, 2, 3, 4 ];
38+
*
39+
* var y = take( x, [ 1, 3 ] );
40+
* // returns [ 2, 4 ]
41+
*/
42+
declare function take<T>( x: Collection<T>, indices: Collection<number> ): Array<T>;
43+
44+
45+
// EXPORTS //
46+
47+
export = take;

0 commit comments

Comments
 (0)