README.md (5401B)
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 # array2iterator 22 23 > Create an iterator from an array-like object. 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 array2iterator = require( '@stdlib/array/to-iterator' ); 41 ``` 42 43 #### array2iterator( src\[, mapFcn\[, thisArg]] ) 44 45 Returns an iterator which iterates over each element in an array-like `object`. 46 47 ```javascript 48 var it = array2iterator( [ 1, 2, 3, 4 ] ); 49 // returns <Object> 50 51 var v = it.next().value; 52 // returns 1 53 54 v = it.next().value; 55 // returns 2 56 57 v = it.next().value; 58 // returns 3 59 60 // ... 61 ``` 62 63 The returned iterator protocol-compliant object has the following properties: 64 65 - **next**: function which returns an iterator protocol-compliant object containing the next iterated value (if one exists) assigned to a `value` property and a `done` property having a `boolean` value indicating whether the iterator is finished. 66 - **return**: function which closes an iterator and returns a single (optional) argument in an iterator protocol-compliant object. 67 68 To invoke a function for each `src` value, provide a callback function. 69 70 ```javascript 71 function fcn( v ) { 72 return v * 10.0; 73 } 74 75 var it = array2iterator( [ 1, 2, 3, 4 ], fcn ); 76 // returns <Object> 77 78 var v = it.next().value; 79 // returns 10.0 80 81 v = it.next().value; 82 // returns 20.0 83 84 v = it.next().value; 85 // returns 30.0 86 87 // ... 88 ``` 89 90 The invoked function is provided three arguments: 91 92 - `value`: iterated value 93 - `index`: iterated value index 94 - `src`: source array-like object 95 96 ```javascript 97 function fcn( v, i ) { 98 return v * (i+1); 99 } 100 101 var it = array2iterator( [ 1, 2, 3, 4 ], fcn ); 102 // returns <Object> 103 104 var v = it.next().value; 105 // returns 1 106 107 v = it.next().value; 108 // returns 4 109 110 v = it.next().value; 111 // returns 9 112 113 // ... 114 ``` 115 116 To set the callback function execution context, provide a `thisArg`. 117 118 ```javascript 119 function fcn( v ) { 120 this.count += 1; 121 return v * 10.0; 122 } 123 124 var ctx = { 125 'count': 0 126 }; 127 128 var it = array2iterator( [ 1, 2, 3, 4 ], fcn, ctx ); 129 // returns <Object> 130 131 var v = it.next().value; 132 // returns 10.0 133 134 v = it.next().value; 135 // returns 20.0 136 137 v = it.next().value; 138 // returns 30.0 139 140 var count = ctx.count; 141 // returns 3 142 ``` 143 144 </section> 145 146 <!-- /.usage --> 147 148 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 149 150 <section class="notes"> 151 152 ## Notes 153 154 - If an environment supports `Symbol.iterator`, the returned iterator is iterable. 155 - If provided a generic `array`, the returned iterator does **not** ignore holes. To achieve greater performance for sparse arrays, use [`@stdlib/array/to-sparse-iterator`][@stdlib/array/to-sparse-iterator]. 156 - A returned iterator does **not** copy a provided array-like `object`. To ensure iterable reproducibility, copy a provided array-like `object` **before** creating an iterator. Otherwise, any changes to the contents of an array-like `object` will be reflected in the returned iterator. 157 - In environments supporting `Symbol.iterator`, the function **explicitly** does **not** invoke an array's `@@iterator` method, regardless of whether this method is defined. To convert an array to an implementation defined iterator, invoke this method directly. 158 159 </section> 160 161 <!-- /.notes --> 162 163 <!-- Package usage examples. --> 164 165 <section class="examples"> 166 167 ## Examples 168 169 <!-- eslint no-undef: "error" --> 170 171 ```javascript 172 var Float64Array = require( '@stdlib/array/float64' ); 173 var inmap = require( '@stdlib/utils/inmap' ); 174 var randu = require( '@stdlib/random/base/randu' ); 175 var array2iterator = require( '@stdlib/array/to-iterator' ); 176 177 function scale( v, i ) { 178 return v * (i+1); 179 } 180 181 // Create an array filled with random numbers: 182 var arr = inmap( new Float64Array( 100 ), randu ); 183 184 // Create an iterator from the array which scales iterated values: 185 var it = array2iterator( arr, scale ); 186 187 // Perform manual iteration... 188 var v; 189 while ( true ) { 190 v = it.next(); 191 if ( v.done ) { 192 break; 193 } 194 console.log( v.value ); 195 } 196 ``` 197 198 </section> 199 200 <!-- /.examples --> 201 202 <!-- 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. --> 203 204 <section class="references"> 205 206 </section> 207 208 <!-- /.references --> 209 210 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 211 212 <section class="links"> 213 214 [@stdlib/array/to-sparse-iterator]: https://www.npmjs.com/package/@stdlib/array/tree/main/to-sparse-iterator 215 216 </section> 217 218 <!-- /.links -->