|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2022 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 | +# mapReduceRight |
| 22 | + |
| 23 | +> Perform a single-pass map-reduce operation against each element in an array while iterating from right to left and return the accumulated result. |
| 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 mapReduceRight = require( '@stdlib/utils/map-reduce-right' ); |
| 41 | +``` |
| 42 | + |
| 43 | +#### mapReduceRight( arr, initial, mapper, reducer\[, thisArg ] ) |
| 44 | + |
| 45 | +Performs a map-reduce operation against each element in an array while iterating from right to left and returns the accumulated result. |
| 46 | + |
| 47 | +```javascript |
| 48 | +function square( value ) { |
| 49 | + return value * value; |
| 50 | +} |
| 51 | + |
| 52 | +function sum( accumulator, value ) { |
| 53 | + return accumulator + value; |
| 54 | +} |
| 55 | + |
| 56 | +var arr = [ 1, 2, 3, 4 ]; |
| 57 | + |
| 58 | +var out = mapReduceRight( arr, 0, square, sum ); |
| 59 | +// returns 30 |
| 60 | +``` |
| 61 | + |
| 62 | +The function accepts both array-like objects and [`ndarray`][@stdlib/ndarray/ctor]-like objects. |
| 63 | + |
| 64 | +```javascript |
| 65 | +var array = require( '@stdlib/ndarray/array' ); |
| 66 | + |
| 67 | +function square( value ) { |
| 68 | + return value * value; |
| 69 | +} |
| 70 | + |
| 71 | +function sum( accumulator, value ) { |
| 72 | + return accumulator + value; |
| 73 | +} |
| 74 | + |
| 75 | +var opts = { |
| 76 | + 'dtype': 'generic' |
| 77 | +}; |
| 78 | +var arr = array( [ [ 1, 2, 3 ], [ 4, 5, 6 ] ], opts ); |
| 79 | + |
| 80 | +var out = mapReduceRight( arr, 0, square, sum ); |
| 81 | +// returns 91 |
| 82 | +``` |
| 83 | + |
| 84 | +The mapping function is provided the following arguments: |
| 85 | + |
| 86 | +- **value**: array element. |
| 87 | +- **index**: element index. |
| 88 | +- **arr**: input array. |
| 89 | + |
| 90 | +The reducing function is provided the following arguments: |
| 91 | + |
| 92 | +- **accumulator**: accumulated value. |
| 93 | +- **value**: result of applying the mapping function to the current array element. |
| 94 | +- **index**: element index. |
| 95 | +- **arr**: input array. |
| 96 | + |
| 97 | +To set the `this` context when invoking the reducing function, provide a `thisArg`. |
| 98 | + |
| 99 | +<!-- eslint-disable no-invalid-this --> |
| 100 | + |
| 101 | +```javascript |
| 102 | +function square( value ) { |
| 103 | + return value * value; |
| 104 | +} |
| 105 | + |
| 106 | +function sum( accumulator, value ) { |
| 107 | + this.count += 1; |
| 108 | + return accumulator + value; |
| 109 | +} |
| 110 | + |
| 111 | +var arr = [ 1, 2, 3, 4 ]; |
| 112 | + |
| 113 | +var ctx = { |
| 114 | + 'count': 0 |
| 115 | +}; |
| 116 | + |
| 117 | +var out = mapReduceRight( arr, 0, square, sum, ctx ); |
| 118 | +// returns 30 |
| 119 | + |
| 120 | +var mean = out / ctx.count; |
| 121 | +// returns 7.5 |
| 122 | +``` |
| 123 | + |
| 124 | +</section> |
| 125 | + |
| 126 | +<!-- /.usage --> |
| 127 | + |
| 128 | +<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 129 | + |
| 130 | +<section class="notes"> |
| 131 | + |
| 132 | +## Notes |
| 133 | + |
| 134 | +- The function supports array-like objects exposing getters and setters for array element access (e.g., [`Complex64Array`][@stdlib/array/complex64], [`Complex128Array`][@stdlib/array/complex128], etc). |
| 135 | + |
| 136 | + ```javascript |
| 137 | + var Complex64Array = require( '@stdlib/array/complex64' ); |
| 138 | + var Complex64 = require( '@stdlib/complex/float32' ); |
| 139 | + var cceil = require( '@stdlib/math/base/special/cceil' ); |
| 140 | + var realf = require( '@stdlib/complex/realf' ); |
| 141 | + var imagf = require( '@stdlib/complex/imagf' ); |
| 142 | + |
| 143 | + function sum( acc, z ) { |
| 144 | + var re1 = realf( acc ); |
| 145 | + var im1 = imagf( acc ); |
| 146 | + var re2 = realf( z ); |
| 147 | + var im2 = imagf( z ); |
| 148 | + return new Complex64( re1+re2, im1+im2 ); |
| 149 | + } |
| 150 | + |
| 151 | + var x = new Complex64Array( [ 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5 ] ); |
| 152 | + |
| 153 | + var v = mapReduceRight( x, new Complex64( 0.0, 0.0 ), cceil, sum ); |
| 154 | + // returns <Complex64> |
| 155 | + |
| 156 | + var re = realf( v ); |
| 157 | + // returns 20.0 |
| 158 | + |
| 159 | + var im = imagf( v ); |
| 160 | + // returns 24.0 |
| 161 | + ``` |
| 162 | + |
| 163 | +- For [`ndarray`][@stdlib/ndarray/ctor]-like objects, the function performs a single-pass map-reduce operation over the entire input [`ndarray`][@stdlib/ndarray/ctor] (i.e., higher-order [`ndarray`][@stdlib/ndarray/ctor] dimensions are flattened to a single-dimension). |
| 164 | + |
| 165 | +- When applying a function to [`ndarray`][@stdlib/ndarray/ctor]-like objects, performance will be best for [`ndarray`][@stdlib/ndarray/ctor]-like objects which are single-segment contiguous. |
| 166 | + |
| 167 | +</section> |
| 168 | + |
| 169 | +<!-- /.notes --> |
| 170 | + |
| 171 | +<!-- Package usage examples. --> |
| 172 | + |
| 173 | +<section class="examples"> |
| 174 | + |
| 175 | +## Examples |
| 176 | + |
| 177 | +<!-- eslint no-undef: "error" --> |
| 178 | + |
| 179 | +```javascript |
| 180 | +var filledarrayBy = require( '@stdlib/array/filled-by' ); |
| 181 | +var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory; |
| 182 | +var naryFunction = require( '@stdlib/utils/nary-function' ); |
| 183 | +var add = require( '@stdlib/math/base/ops/add' ); |
| 184 | +var abs = require( '@stdlib/math/base/special/abs' ); |
| 185 | +var array = require( '@stdlib/ndarray/array' ); |
| 186 | +var mapReduceRight = require( '@stdlib/utils/map-reduce-right' ); |
| 187 | + |
| 188 | +function fill( i ) { |
| 189 | + var rand = discreteUniform( -10*(i+1), 10*(i+1) ); |
| 190 | + return filledarrayBy( 10, 'generic', rand ); |
| 191 | +} |
| 192 | + |
| 193 | +// Create a two-dimensional ndarray (i.e., a matrix): |
| 194 | +var x = array( filledarrayBy( 10, 'generic', fill ), { |
| 195 | + 'dtype': 'generic', |
| 196 | + 'flatten': true |
| 197 | +}); |
| 198 | + |
| 199 | +// Create an explicit unary function: |
| 200 | +var f1 = naryFunction( abs, 1 ); |
| 201 | + |
| 202 | +// Create an explicit binary function: |
| 203 | +var f2 = naryFunction( add, 2 ); |
| 204 | + |
| 205 | +// Compute the sum of absolute values: |
| 206 | +var out = mapReduceRight( x, 0, f1, f2 ); |
| 207 | + |
| 208 | +console.log( 'x:' ); |
| 209 | +console.log( x.data ); |
| 210 | + |
| 211 | +console.log( 'sum: %d', out ); |
| 212 | +``` |
| 213 | + |
| 214 | +</section> |
| 215 | + |
| 216 | +<!-- /.examples --> |
| 217 | + |
| 218 | +<!-- 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. --> |
| 219 | + |
| 220 | +<section class="references"> |
| 221 | + |
| 222 | +</section> |
| 223 | + |
| 224 | +<!-- /.references --> |
| 225 | + |
| 226 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 227 | + |
| 228 | +<section class="related"> |
| 229 | + |
| 230 | +</section> |
| 231 | + |
| 232 | +<!-- /.related --> |
| 233 | + |
| 234 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 235 | + |
| 236 | +<section class="links"> |
| 237 | + |
| 238 | +[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib |
| 239 | + |
| 240 | +[@stdlib/array/complex64]: https://github.com/stdlib-js/stdlib |
| 241 | + |
| 242 | +[@stdlib/array/complex128]: https://github.com/stdlib-js/stdlib |
| 243 | + |
| 244 | +</section> |
| 245 | + |
| 246 | +<!-- /.links --> |
0 commit comments