README.md (5231B)
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 # forEachRight 22 23 > Invoke a function for each element in a collection, iterating from the 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 forEachRight = require( '@stdlib/utils/for-each-right' ); 41 ``` 42 43 #### forEachRight( collection, fcn\[, thisArg ] ) 44 45 Invokes a `function` for each element in a `collection`, iterating from right to left. 46 47 ```javascript 48 function log( value, index ) { 49 console.log( '%s: %d', index, value ); 50 } 51 52 var arr = [ 1, 2, 3, 4 ]; 53 54 forEachRight( arr, log ); 55 /* => 56 3: 4 57 2: 3 58 1: 2 59 0: 1 60 */ 61 ``` 62 63 The invoked `function` is provided three arguments: 64 65 - `value`: collection element 66 - `index`: collection index 67 - `collection`: input collection 68 69 Basic support for dynamic collections is provided. Note, however, that index incrementation is **not** guaranteed to be monotonically **decreasing**. 70 71 ```javascript 72 var arr = [ 1, 2, 3, 4 ]; 73 var i = 0; 74 75 function log1( value, index, collection ) { 76 console.log( '%s: %d', index, value ); 77 i += 1; 78 if ( index === 0 && collection.length < 10 ) { 79 collection.unshift( i+1 ); 80 } 81 } 82 83 forEachRight( arr, log1 ); 84 /* => 85 3: 4 86 2: 3 87 1: 2 88 0: 1 89 0: 5 90 0: 6 91 0: 7 92 0: 8 93 0: 9 94 0: 10 95 */ 96 97 function log2( value, index, collection ) { 98 console.log( '%s: %d', index, value ); 99 collection.pop(); 100 } 101 102 arr = [ 1, 2, 3, 4 ]; 103 104 forEachRight( arr, log2 ); 105 /* => 106 3: 4 107 1: 2 108 */ 109 ``` 110 111 To set the function execution context, provide a `thisArg`. 112 113 ```javascript 114 function sum( value ) { 115 this.sum += value; 116 this.count += 1; 117 } 118 119 var arr = [ 1, 2, 3, 4 ]; 120 121 var context = { 122 'sum': 0, 123 'count': 0 124 }; 125 126 forEachRight( arr, sum, context ); 127 128 var mean = context.sum / context.count; 129 // returns 2.5 130 ``` 131 132 </section> 133 134 <!-- /.usage --> 135 136 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 137 138 <section class="notes"> 139 140 ## Notes 141 142 - A `collection` may be either an [`Array`][mdn-array], [`Typed Array`][mdn-typed-array], or an array-like [`Object`][mdn-object] (excluding `strings` and `functions`). 143 144 - The function differs from [`Array.prototype.forEach`][mdn-array-foreach] in the following ways: 145 146 - The function returns the input `collection`. 147 148 - The function does **not** skip `undefined` elements. 149 150 <!-- eslint-disable no-sparse-arrays --> 151 152 ```javascript 153 function log( value, index ) { 154 console.log( '%s: %s', index, value ); 155 } 156 157 var arr = [ 1, , , 4 ]; 158 159 forEachRight( arr, log ); 160 /* => 161 3: 4 162 2: undefined 163 1: undefined 164 0: 1 165 */ 166 ``` 167 168 - The function provides limited support for dynamic collections (i.e., collections whose `length` changes during execution). 169 170 </section> 171 172 <!-- /.notes --> 173 174 <!-- Package usage examples. --> 175 176 <section class="examples"> 177 178 ## Examples 179 180 <!-- eslint no-undef: "error" --> 181 182 ```javascript 183 var isEven = require( '@stdlib/assert/is-even' ).isPrimitive; 184 var forEachRight = require( '@stdlib/utils/for-each-right' ); 185 186 var arr; 187 var i; 188 189 function log( value, index, collection ) { 190 console.log( '%s: %d', index, value ); 191 i += 1; 192 if ( isEven( i ) ) { 193 collection.pop(); 194 } else { 195 collection.unshift( i+1 ); 196 } 197 } 198 199 arr = new Array( 100 ); 200 for ( i = arr.length-1; i >= 0; i-- ) { 201 arr[ i ] = i; 202 } 203 204 i = 0; 205 forEachRight( arr, log ); 206 ``` 207 208 </section> 209 210 <!-- /.examples --> 211 212 <!-- 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. --> 213 214 <section class="references"> 215 216 </section> 217 218 <!-- /.references --> 219 220 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 221 222 <section class="links"> 223 224 [mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array 225 226 [mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray 227 228 [mdn-object]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object 229 230 [mdn-array-foreach]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach 231 232 </section> 233 234 <!-- /.links -->