README.md (5739B)
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 # sparsearray2iteratorRight 22 23 > Create an iterator from a sparse array-like object, 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 sparsearray2iteratorRight = require( '@stdlib/array/to-sparse-iterator-right' ); 41 ``` 42 43 #### sparsearray2iteratorRight( src\[, mapFcn\[, thisArg]] ) 44 45 Returns an iterator which iterates from right to left over each element in a sparse array-like `object`. 46 47 <!-- eslint-disable no-sparse-arrays --> 48 49 ```javascript 50 var it = sparsearray2iteratorRight( [ 1, , , 4 ] ); 51 // returns <Object> 52 53 var v = it.next().value; 54 // returns 4 55 56 v = it.next().value; 57 // returns 1 58 59 var bool = it.next().done; 60 // returns true 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 <!-- eslint-disable no-sparse-arrays --> 71 72 ```javascript 73 function fcn( v ) { 74 return v * 10.0; 75 } 76 77 var it = sparsearray2iteratorRight( [ 1, 2, , 4 ], fcn ); 78 // returns <Object> 79 80 var v = it.next().value; 81 // returns 40.0 82 83 v = it.next().value; 84 // returns 20.0 85 86 // ... 87 ``` 88 89 The invoked function is provided three arguments: 90 91 - `value`: iterated value 92 - `index`: iterated value index 93 - `src`: source array-like object 94 95 <!-- eslint-disable no-sparse-arrays --> 96 97 ```javascript 98 function fcn( v, i ) { 99 return v * (i+1); 100 } 101 102 var it = sparsearray2iteratorRight( [ 1, 2, , 4 ], fcn ); 103 // returns <Object> 104 105 var v = it.next().value; 106 // returns 16 107 108 v = it.next().value; 109 // returns 4 110 111 v = it.next().value; 112 // returns 1 113 ``` 114 115 To set the callback function execution context, provide a `thisArg`. 116 117 <!-- eslint-disable no-sparse-arrays --> 118 119 ```javascript 120 function fcn( v ) { 121 this.count += 1; 122 return v * 10.0; 123 } 124 125 var ctx = { 126 'count': 0 127 }; 128 129 var it = sparsearray2iteratorRight( [ 1, 2, , 4 ], fcn, ctx ); 130 // returns <Object> 131 132 var v = it.next().value; 133 // returns 40.0 134 135 v = it.next().value; 136 // returns 20.0 137 138 var count = ctx.count; 139 // returns 2 140 ``` 141 142 </section> 143 144 <!-- /.usage --> 145 146 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 147 148 <section class="notes"> 149 150 ## Notes 151 152 - If an environment supports `Symbol.iterator`, the returned iterator is iterable. 153 - If provided a generic `array`, the returned iterator **ignores** holes (i.e., `undefined` values). To iterate over all generic `array` elements, use [`@stdlib/array/to-iterator-right`][@stdlib/array/to-iterator-right]. 154 - 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. 155 - 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. 156 157 </section> 158 159 <!-- /.notes --> 160 161 <!-- Package usage examples. --> 162 163 <section class="examples"> 164 165 ## Examples 166 167 <!-- eslint no-undef: "error" --> 168 169 ```javascript 170 var Float64Array = require( '@stdlib/array/float64' ); 171 var inmap = require( '@stdlib/utils/inmap' ); 172 var randu = require( '@stdlib/random/base/randu' ); 173 var sparsearray2iteratorRight = require( '@stdlib/array/to-sparse-iterator-right' ); 174 175 function scale( v, i ) { 176 return v * (i+1); 177 } 178 179 // Create an array partially filled with random numbers: 180 var arr = new Array( 100 ); 181 var i; 182 for ( i = 0; i < arr.length; i += 2 ) { 183 arr[ i ] = randu(); 184 } 185 186 // Create an iterator from the array which scales iterated values: 187 var it = sparsearray2iteratorRight( arr, scale ); 188 189 // Perform manual iteration... 190 var v; 191 while ( true ) { 192 v = it.next(); 193 if ( v.done ) { 194 break; 195 } 196 console.log( v.value ); 197 } 198 ``` 199 200 </section> 201 202 <!-- /.examples --> 203 204 <!-- 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. --> 205 206 <section class="references"> 207 208 </section> 209 210 <!-- /.references --> 211 212 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 213 214 <section class="links"> 215 216 [@stdlib/array/to-iterator-right]: https://www.npmjs.com/package/@stdlib/array/tree/main/to-iterator-right 217 218 </section> 219 220 <!-- /.links -->