Skip to content

Commit c1ee338

Browse files
committed
feat: add array/take
1 parent c308840 commit c1ee338

14 files changed

Lines changed: 1551 additions & 0 deletions

File tree

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2024 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+
# take
22+
23+
> Take elements from an array.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var take = require( '@stdlib/array/take' );
31+
```
32+
33+
#### take( x, indices\[, options] )
34+
35+
Takes elements from an array.
36+
37+
```javascript
38+
var x = [ 1, 2, 3, 4 ];
39+
40+
var y = take( x, [ 1, 3 ] );
41+
// returns [ 2, 4 ]
42+
```
43+
44+
The function supports the following parameters:
45+
46+
- **x**: input array.
47+
- **indices**: list of indices.
48+
- **options**: function options.
49+
50+
The function supports the following options:
51+
52+
- **mode**: index [mode][@stdlib/ndarray/base/ind]. Default: `'normalize'`.
53+
54+
By default, the function normalizes negative integer indices to positive integer index equivalents.
55+
56+
```javascript
57+
var x = [ 1, 2, 3, 4 ];
58+
59+
var y = take( x, [ -3, -1 ] );
60+
// returns [ 2, 4 ]
61+
```
62+
63+
To specify an alternative index [mode][@stdlib/ndarray/base/ind], provide a `mode` option.
64+
65+
```javascript
66+
var x = [ 1, 2, 3, 4 ];
67+
68+
var y = take( x, [ -10, 10 ], {
69+
'mode': 'clamp'
70+
});
71+
// returns [ 1, 4 ]
72+
```
73+
74+
</section>
75+
76+
<!-- /.usage -->
77+
78+
<section class="notes">
79+
80+
## Notes
81+
82+
- If `indices` is an empty array, the function returns an empty array.
83+
84+
```javascript
85+
var x = [ 1, 2, 3, 4 ];
86+
87+
var y = take( x, [] );
88+
// returns []
89+
```
90+
91+
- If provided an input array having a recognized [data type][@stdlib/array/dtypes], the function returns an array having the same [data type][@stdlib/array/dtypes] as the input array. Otherwise, the function **always** returns a "generic" array.
92+
93+
</section>
94+
95+
<!-- /.notes -->
96+
97+
<section class="examples">
98+
99+
## Examples
100+
101+
<!-- eslint no-undef: "error" -->
102+
103+
```javascript
104+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
105+
var linspace = require( '@stdlib/array/linspace' );
106+
var take = require( '@stdlib/array/take' );
107+
108+
// Generate a linearly spaced array:
109+
var x = linspace( 0, 100, 11 );
110+
console.log( x );
111+
112+
// Generate an array of random indices:
113+
var indices = discreteUniform( 10, 0, x.length-1 );
114+
console.log( indices );
115+
116+
// Take a random sample of elements from `x`:
117+
var y = take( x, indices );
118+
console.log( y );
119+
```
120+
121+
</section>
122+
123+
<!-- /.examples -->
124+
125+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
126+
127+
<section class="related">
128+
129+
</section>
130+
131+
<!-- /.related -->
132+
133+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
134+
135+
<section class="links">
136+
137+
[@stdlib/ndarray/base/ind]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/ind
138+
139+
[@stdlib/array/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/dtypes
140+
141+
</section>
142+
143+
<!-- /.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) 2024 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+
});
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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 discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
26+
var isArray = require( '@stdlib/assert/is-array' );
27+
var pkg = require( './../package.json' ).name;
28+
var take = 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 idx = discreteUniform( len, 0, 3 );
42+
return benchmark;
43+
44+
/**
45+
* Benchmark function.
46+
*
47+
* @private
48+
* @param {Benchmark} b - benchmark instance
49+
*/
50+
function benchmark( b ) {
51+
var x;
52+
var v;
53+
var i;
54+
55+
x = [ 1, 2, 3, 4 ];
56+
57+
b.tic();
58+
for ( i = 0; i < b.iterations; i++ ) {
59+
v = take( x, idx );
60+
if ( typeof v !== 'object' ) {
61+
b.fail( 'should return an array' );
62+
}
63+
}
64+
b.toc();
65+
if ( !isArray( v ) ) {
66+
b.fail( 'should return an array' );
67+
}
68+
b.pass( 'benchmark finished' );
69+
b.end();
70+
}
71+
}
72+
73+
74+
// MAIN //
75+
76+
/**
77+
* Main execution sequence.
78+
*
79+
* @private
80+
*/
81+
function main() {
82+
var len;
83+
var min;
84+
var max;
85+
var f;
86+
var i;
87+
88+
min = 1; // 10^min
89+
max = 6; // 10^max
90+
91+
for ( i = min; i <= max; i++ ) {
92+
len = pow( 10, i );
93+
f = createBenchmark( len );
94+
bench( pkg+':len='+len, f );
95+
}
96+
}
97+
98+
main();
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
{{alias}}( x, indices[, options] )
3+
Takes elements from an array.
4+
5+
If `indices` is an empty array, the function returns an empty array.
6+
7+
Parameters
8+
----------
9+
x: Array|TypedArray|Object
10+
Input array.
11+
12+
indices: ArrayLikeObject<integer>
13+
List of element indices.
14+
15+
options: Object (optional)
16+
Function options.
17+
18+
options.mode: string (optional)
19+
Specifies how to handle an index outside the interval [0, max], where
20+
`max` is the maximum possible array index. If equal to 'throw', the
21+
function throws an error. If equal to 'normalize', the function throws
22+
an error if provided an out-of-bounds normalized index. If equal to
23+
'wrap', the function wraps around an index using modulo arithmetic. If
24+
equal to 'clamp', the function sets an index to either 0 (minimum index)
25+
or the maximum index. Default: 'normalize'.
26+
27+
Returns
28+
-------
29+
out: Array|TypedArray
30+
Output array.
31+
32+
Examples
33+
--------
34+
> var x = [ 1, 2, 3, 4 ];
35+
> var y = {{alias}}( x, [ 1, 3 ] )
36+
[ 2, 4 ]
37+
38+
See Also
39+
--------
40+

0 commit comments

Comments
 (0)