README.md (4238B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2018 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 # shift 22 23 > Remove and return the first element of a collection. 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 shift = require( '@stdlib/utils/shift' ); 41 ``` 42 43 #### shift( collection ) 44 45 Removes and returns the first element of a `collection`. A `collection` may be either an [`Array`][mdn-array], [`Typed Array`][mdn-typed-array], or an array-like [`Object`][mdn-object] (i.e., an [`Object`][mdn-object] having a valid writable `length` property). 46 47 ```javascript 48 var arr = [ 1.0, 2.0, 3.0, 4.0, 5.0 ]; 49 50 var out = shift( arr ); 51 // returns [ [ 2.0, 3.0, 4.0, 5.0 ], 1.0 ] 52 53 var bool = ( out[ 0 ] === arr ); 54 // returns true 55 56 var lastValue = out[ 1 ]; 57 // returns 1.0 58 ``` 59 60 In contrast to [`Array.prototype.shift`][mdn-array-shift] which returns only the removed element, the function also returns the shortened collection. For [typed arrays][mdn-typed-array] having a length greater than `0`, the returned collection is a new [typed array][mdn-typed-array] view. 61 62 ```javascript 63 var Float64Array = require( '@stdlib/array/float64' ); 64 65 var arr = new Float64Array( 2 ); 66 arr[ 0 ] = 1.0; 67 arr[ 1 ] = 2.0; 68 69 var out = shift( arr ); 70 // returns [ <Float64Array>[ 2.0 ], 1.0 ] 71 72 var bool = ( out[ 0 ] === arr ); 73 // returns false 74 75 bool = ( out[ 0 ].buffer === arr.buffer ); 76 // returns true 77 78 var lastValue = out[ 1 ]; 79 // returns 1.0 80 ``` 81 82 </section> 83 84 <!-- /.usage --> 85 86 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 87 88 <section class="notes"> 89 90 ## Notes 91 92 - When provided a [typed array][mdn-typed-array], the function does **not** change the underlying [`ArrayBuffer`][mdn-arraybuffer]. The function returns a new [typed array][mdn-typed-array] view whose length is one less than the input [typed array][mdn-typed-array] length. Accordingly, the function does **not** reduce the memory footprint of an input [typed array][mdn-typed-array]. 93 94 </section> 95 96 <!-- /.notes --> 97 98 <!-- Package usage examples. --> 99 100 <section class="examples"> 101 102 ## Examples 103 104 <!-- eslint no-undef: "error" --> 105 106 ```javascript 107 var Float64Array = require( '@stdlib/array/float64' ); 108 var shift = require( '@stdlib/utils/shift' ); 109 110 var arr; 111 var out; 112 var i; 113 114 arr = new Float64Array( 100 ); 115 for ( i = 0; i < 100; i++ ) { 116 out = shift( arr ); 117 arr = out[ 0 ]; 118 console.log( 'Length: %d', arr.length ); 119 } 120 console.log( arr ); 121 ``` 122 123 </section> 124 125 <!-- /.examples --> 126 127 <!-- 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. --> 128 129 <section class="references"> 130 131 </section> 132 133 <!-- /.references --> 134 135 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 136 137 <section class="links"> 138 139 [mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array 140 141 [mdn-array-shift]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/shift 142 143 [mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays 144 145 [mdn-arraybuffer]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer 146 147 [mdn-object]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object 148 149 </section> 150 151 <!-- /.links -->