|
| 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 --> |
0 commit comments