README.md (7334B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2019 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 # arrayview2iteratorRight 22 23 > Create an iterator from an array-like object view, iterating from right to left. 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 arrayview2iteratorRight = require( '@stdlib/array/to-view-iterator-right' ); 41 ``` 42 43 #### arrayview2iteratorRight( src\[, begin\[, end]]\[, mapFcn\[, thisArg]] ) 44 45 Returns an iterator which iterates from right to left over each element in an array-like `object` view. 46 47 ```javascript 48 var it = arrayview2iteratorRight( [ 1, 2, 3, 4 ] ); 49 // returns <Object> 50 51 var v = it.next().value; 52 // returns 4 53 54 v = it.next().value; 55 // returns 3 56 57 v = it.next().value; 58 // returns 2 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 The `begin` and `end` arguments define the starting (inclusive) and ending (non-inclusive) indices of the array view. By default, the returned iterator starts iterating from the last element in an array-like `object` (i.e., from the "end"). To specify an alternative view end, provide an `end` argument (non-inclusive). 69 70 ```javascript 71 var it = arrayview2iteratorRight( [ 1, 2, 3, 4 ], 0, 3 ); 72 // returns <Object> 73 74 var v = it.next().value; 75 // returns 3 76 77 v = it.next().value; 78 // returns 2 79 80 v = it.next().value; 81 // returns 1 82 83 var bool = it.next().done; 84 // returns true 85 ``` 86 87 If `end` is less than `0`, the first iterated value is resolved relative to the last view element. For example, the following generates the same behavior as in the previous example 88 89 ```javascript 90 var it = arrayview2iteratorRight( [ 1, 2, 3, 4 ], 0, -1 ); 91 // returns <Object> 92 93 var v = it.next().value; 94 // returns 3 95 96 v = it.next().value; 97 // returns 2 98 99 v = it.next().value; 100 // returns 1 101 102 var bool = it.next().done; 103 // returns true 104 ``` 105 106 By default, the returned iterator iterates through the first element in an array-like `object` view. To specify an alternative view beginning, provide a `begin` argument (inclusive). 107 108 ```javascript 109 var it = arrayview2iteratorRight( [ 1, 2, 3, 4 ], 1 ); 110 // returns <Object> 111 112 var v = it.next().value; 113 // returns 4 114 115 v = it.next().value; 116 // returns 3 117 118 v = it.next().value; 119 // returns 2 120 121 var bool = it.next().done; 122 // returns true 123 ``` 124 125 If `begin` is less than `0`, the last iterated value is resolved relative to the last view element. For example, the following generates the same behavior as in the previous example 126 127 ```javascript 128 var it = arrayview2iteratorRight( [ 1, 2, 3, 4 ], -3 ); 129 // returns <Object> 130 131 var v = it.next().value; 132 // returns 4 133 134 v = it.next().value; 135 // returns 3 136 137 v = it.next().value; 138 // returns 2 139 140 var bool = it.next().done; 141 // returns true 142 ``` 143 144 To invoke a function for each `src` value, provide a callback function. 145 146 ```javascript 147 function fcn( v ) { 148 return v * 10.0; 149 } 150 151 var it = arrayview2iteratorRight( [ 1, 2, 3, 4 ], fcn ); 152 // returns <Object> 153 154 var v = it.next().value; 155 // returns 40.0 156 157 v = it.next().value; 158 // returns 30.0 159 160 v = it.next().value; 161 // returns 20.0 162 163 // ... 164 ``` 165 166 The invoked function is provided four arguments: 167 168 - `value`: iterated value 169 - `index`: iterated value index 170 - `n`: iteration count (zero-based) 171 - `src`: source array-like object 172 173 ```javascript 174 function fcn( v, i ) { 175 return v * (i+1); 176 } 177 178 var it = arrayview2iteratorRight( [ 1, 2, 3, 4 ], fcn ); 179 // returns <Object> 180 181 var v = it.next().value; 182 // returns 16 183 184 v = it.next().value; 185 // returns 9 186 187 v = it.next().value; 188 // returns 4 189 190 // ... 191 ``` 192 193 To set the callback function execution context, provide a `thisArg`. 194 195 ```javascript 196 function fcn( v ) { 197 this.count += 1; 198 return v * 10.0; 199 } 200 201 var ctx = { 202 'count': 0 203 }; 204 205 var it = arrayview2iteratorRight( [ 1, 2, 3, 4 ], fcn, ctx ); 206 // returns <Object> 207 208 var v = it.next().value; 209 // returns 40.0 210 211 v = it.next().value; 212 // returns 30.0 213 214 v = it.next().value; 215 // returns 20.0 216 217 var count = ctx.count; 218 // returns 3 219 ``` 220 221 </section> 222 223 <!-- /.usage --> 224 225 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 226 227 <section class="notes"> 228 229 ## Notes 230 231 - If an environment supports `Symbol.iterator`, the returned iterator is iterable. 232 - If provided a generic `array`, the returned iterator does **not** ignore holes. To achieve greater performance for sparse arrays, use a custom iterator. 233 - 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. 234 - 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. 235 236 </section> 237 238 <!-- /.notes --> 239 240 <!-- Package usage examples. --> 241 242 <section class="examples"> 243 244 ## Examples 245 246 <!-- eslint no-undef: "error" --> 247 248 ```javascript 249 var Float64Array = require( '@stdlib/array/float64' ); 250 var inmap = require( '@stdlib/utils/inmap' ); 251 var randu = require( '@stdlib/random/base/randu' ); 252 var arrayview2iteratorRight = require( '@stdlib/array/to-view-iterator-right' ); 253 254 function scale( v, i ) { 255 return v * (i+1); 256 } 257 258 // Create an array filled with random numbers: 259 var arr = inmap( new Float64Array( 100 ), randu ); 260 261 // Create an iterator from an array view which scales iterated values: 262 var it = arrayview2iteratorRight( arr, 40, 60, scale ); 263 264 // Perform manual iteration... 265 var v; 266 while ( true ) { 267 v = it.next(); 268 if ( v.done ) { 269 break; 270 } 271 console.log( v.value ); 272 } 273 ``` 274 275 </section> 276 277 <!-- /.examples --> 278 279 <!-- 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. --> 280 281 <section class="references"> 282 283 </section> 284 285 <!-- /.references --> 286 287 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 288 289 <section class="links"> 290 291 </section> 292 293 <!-- /.links -->