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