Skip to content

Commit 37d48b4

Browse files
committed
feat: add array/slice
1 parent 24736d2 commit 37d48b4

File tree

10 files changed

+1515
-0
lines changed

10 files changed

+1515
-0
lines changed
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
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+
# slice
22+
23+
> Return a shallow copy of a portion of an array.
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<!-- Package usage documentation. -->
34+
35+
<section class="usage">
36+
37+
## Usage
38+
39+
```javascript
40+
var slice = require( '@stdlib/array/slice' );
41+
```
42+
43+
#### slice( x\[, start\[, end]] )
44+
45+
Returns a shallow copy of a portion of an array.
46+
47+
```javascript
48+
var x = [ 1, 2, 3, 4, 5, 6 ];
49+
50+
var out = slice( x );
51+
// returns [ 1, 2, 3, 4, 5, 6 ]
52+
53+
var bool = ( out === x );
54+
// returns false
55+
```
56+
57+
The function accepts the following arguments:
58+
59+
- **x**: input array.
60+
- **start**: starting index (inclusive). Default: `0`.
61+
- **end**: ending index (exclusive). Default: `x.length`.
62+
63+
By default, the function returns a shallow copy beginning from the first element of the provided input array. To begin copying from an alternative element, provide a starting index (inclusive).
64+
65+
```javascript
66+
var x = [ 1, 2, 3, 4, 5, 6 ];
67+
68+
var out = slice( x, 2 );
69+
// returns [ 3, 4, 5, 6 ]
70+
```
71+
72+
By default, the function copies through the end of the provided input array. To copy up until a specific element, provide an ending index (exclusive).
73+
74+
```javascript
75+
var x = [ 1, 2, 3, 4, 5, 6 ];
76+
77+
var out = slice( x, 1, 4 );
78+
// returns [ 2, 3, 4 ]
79+
```
80+
81+
</section>
82+
83+
<!-- /.usage -->
84+
85+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
86+
87+
<section class="notes">
88+
89+
## Notes
90+
91+
- If provided an array-like object having a `slice` method, the function defers execution to that method and assumes that the method API has the following signature:
92+
93+
```text
94+
x.slice( start, end )
95+
```
96+
97+
- If provided an array-like object without a `slice` method, the function copies input array elements to a new generic array.
98+
99+
</section>
100+
101+
<!-- /.notes -->
102+
103+
<!-- Package usage examples. -->
104+
105+
<section class="examples">
106+
107+
## Examples
108+
109+
<!-- eslint no-undef: "error" -->
110+
111+
```javascript
112+
var Float64Array = require( '@stdlib/array/float64' );
113+
var zeroTo = require( '@stdlib/array/zero-to' );
114+
var slice = require( '@stdlib/array/slice' );
115+
116+
var x = zeroTo( 6, 'float64' );
117+
// returns <Float64Array>[ 0.0, 1.0, 2.0, 3.0, 4.0, 5.0 ]
118+
119+
var s = slice( x );
120+
// returns <Float64Array>[ 0.0, 1.0, 2.0, 3.0, 4.0, 5.0 ]
121+
122+
s = slice( x, 0, 4 );
123+
// returns <Float64Array>[ 0.0, 1.0, 2.0, 3.0 ]
124+
125+
s = slice( x, 2 );
126+
// returns <Float64Array>[ 2.0, 3.0, 4.0, 5.0 ]
127+
128+
s = slice( x, 2, 4 );
129+
// returns <Float64Array>[ 2.0, 3.0 ]
130+
```
131+
132+
</section>
133+
134+
<!-- /.examples -->
135+
136+
<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
137+
138+
<section class="references">
139+
140+
</section>
141+
142+
<!-- /.references -->
143+
144+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
145+
146+
<section class="related">
147+
148+
</section>
149+
150+
<!-- /.related -->
151+
152+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
153+
154+
<section class="links">
155+
156+
</section>
157+
158+
<!-- /.links -->
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) 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 isArray = require( '@stdlib/assert/is-array' );
26+
var ones = require( '@stdlib/array/ones' );
27+
var pkg = require( './../package.json' ).name;
28+
var slice = 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 x = ones( len, 'generic' );
42+
return benchmark;
43+
44+
/**
45+
* Benchmark function.
46+
*
47+
* @private
48+
* @param {Benchmark} b - benchmark instance
49+
*/
50+
function benchmark( b ) {
51+
var out;
52+
var i;
53+
54+
b.tic();
55+
for ( i = 0; i < b.iterations; i++ ) {
56+
out = slice( x );
57+
if ( out.length !== len ) {
58+
b.fail( 'unexpected length' );
59+
}
60+
}
61+
b.toc();
62+
if ( !isArray( out ) ) {
63+
b.fail( 'should return an array' );
64+
}
65+
b.pass( 'benchmark finished' );
66+
b.end();
67+
}
68+
}
69+
70+
71+
// MAIN //
72+
73+
/**
74+
* Main execution sequence.
75+
*
76+
* @private
77+
*/
78+
function main() {
79+
var len;
80+
var min;
81+
var max;
82+
var f;
83+
var i;
84+
85+
min = 1; // 10^min
86+
max = 6; // 10^max
87+
88+
for ( i = min; i <= max; i++ ) {
89+
len = pow( 10, i );
90+
91+
f = createBenchmark( len );
92+
bench( pkg+':dtype=generic,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}}( x[, start[, end]] )
3+
Returns a shallow copy of a portion of an array.
4+
5+
If provided an array-like object having a `slice` method, the function
6+
defers execution to that method and assumes that the method has the
7+
following signature:
8+
9+
x.slice( start, end )
10+
11+
If provided an array-like object without a `slice` method, the function
12+
copies input array elements to a new generic array.
13+
14+
Parameters
15+
----------
16+
x: ArrayLikeObject
17+
Input array.
18+
19+
start: integer (optional)
20+
Starting index (inclusive). Default: 0.
21+
22+
end: integer (optional)
23+
Ending index (exclusive). Default: x.length.
24+
25+
Returns
26+
-------
27+
out: Array|TypedArray
28+
Output array.
29+
30+
Examples
31+
--------
32+
> var out = {{alias}}( [ 1, 2, 3, 4 ] )
33+
[ 1, 2, 3, 4 ]
34+
> out = {{alias}}( [ 1, 2, 3, 4 ], 1 )
35+
[ 2, 3, 4 ]
36+
> out = {{alias}}( [ 1, 2, 3, 4 ], 1, 3 )
37+
[ 2, 3 ]
38+
39+
See Also
40+
--------
41+

0 commit comments

Comments
 (0)