@stdlib/utils-map-reduce 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,322 @@
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
+ # mapReduce
22
+
23
+ [![NPM version][npm-image]][npm-url] [![Build Status][test-image]][test-url] [![Coverage Status][coverage-image]][coverage-url] <!-- [![dependencies][dependencies-image]][dependencies-url] -->
24
+
25
+ > Perform a single-pass map-reduce operation against each element in an array and return the accumulated result.
26
+
27
+ <!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
28
+
29
+ <section class="intro">
30
+
31
+ </section>
32
+
33
+ <!-- /.intro -->
34
+
35
+ <!-- Package usage documentation. -->
36
+
37
+ <section class="installation">
38
+
39
+ ## Installation
40
+
41
+ ```bash
42
+ npm install @stdlib/utils-map-reduce
43
+ ```
44
+
45
+ </section>
46
+
47
+ <section class="usage">
48
+
49
+ ## Usage
50
+
51
+ ```javascript
52
+ var mapReduce = require( '@stdlib/utils-map-reduce' );
53
+ ```
54
+
55
+ #### mapReduce( arr, initial, mapper, reducer\[, thisArg ] )
56
+
57
+ Performs a map-reduce operation against each element in an array and returns the accumulated result.
58
+
59
+ ```javascript
60
+ function square( value ) {
61
+ return value * value;
62
+ }
63
+
64
+ function sum( accumulator, value ) {
65
+ return accumulator + value;
66
+ }
67
+
68
+ var arr = [ 1, 2, 3, 4 ];
69
+
70
+ var out = mapReduce( arr, 0, square, sum );
71
+ // returns 30
72
+ ```
73
+
74
+ The function accepts both array-like objects and [`ndarray`][@stdlib/ndarray/ctor]-like objects.
75
+
76
+ ```javascript
77
+ var array = require( '@stdlib/ndarray-array' );
78
+
79
+ function square( value ) {
80
+ return value * value;
81
+ }
82
+
83
+ function sum( accumulator, value ) {
84
+ return accumulator + value;
85
+ }
86
+
87
+ var opts = {
88
+ 'dtype': 'generic'
89
+ };
90
+ var arr = array( [ [ 1, 2, 3 ], [ 4, 5, 6 ] ], opts );
91
+
92
+ var out = mapReduce( arr, 0, square, sum );
93
+ // returns 91
94
+ ```
95
+
96
+ The mapping function is provided the following arguments:
97
+
98
+ - **value**: array element.
99
+ - **index**: element index.
100
+ - **arr**: input array.
101
+
102
+ The reducing function is provided the following arguments:
103
+
104
+ - **accumulator**: accumulated value.
105
+ - **value**: result of applying the mapping function to the current array element.
106
+ - **index**: element index.
107
+ - **arr**: input array.
108
+
109
+ To set the `this` context when invoking the reducing function, provide a `thisArg`.
110
+
111
+ <!-- eslint-disable no-invalid-this -->
112
+
113
+ ```javascript
114
+ function square( value ) {
115
+ return value * value;
116
+ }
117
+
118
+ function sum( accumulator, value ) {
119
+ this.count += 1;
120
+ return accumulator + value;
121
+ }
122
+
123
+ var arr = [ 1, 2, 3, 4 ];
124
+
125
+ var ctx = {
126
+ 'count': 0
127
+ };
128
+
129
+ var out = mapReduce( arr, 0, square, sum, ctx );
130
+ // returns 30
131
+
132
+ var mean = out / ctx.count;
133
+ // returns 7.5
134
+ ```
135
+
136
+ </section>
137
+
138
+ <!-- /.usage -->
139
+
140
+ <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
141
+
142
+ <section class="notes">
143
+
144
+ ## Notes
145
+
146
+ - 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).
147
+
148
+ ```javascript
149
+ var Complex64Array = require( '@stdlib/array-complex64' );
150
+ var Complex64 = require( '@stdlib/complex-float32' );
151
+ var cceil = require( '@stdlib/math-base-special-cceil' );
152
+ var realf = require( '@stdlib/complex-realf' );
153
+ var imagf = require( '@stdlib/complex-imagf' );
154
+
155
+ function sum( acc, z ) {
156
+ var re1 = realf( acc );
157
+ var im1 = imagf( acc );
158
+ var re2 = realf( z );
159
+ var im2 = imagf( z );
160
+ return new Complex64( re1+re2, im1+im2 );
161
+ }
162
+
163
+ var x = new Complex64Array( [ 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5 ] );
164
+
165
+ var v = mapReduce( x, new Complex64( 0.0, 0.0 ), cceil, sum );
166
+ // returns <Complex64>
167
+
168
+ var re = realf( v );
169
+ // returns 20.0
170
+
171
+ var im = imagf( v );
172
+ // returns 24.0
173
+ ```
174
+
175
+ - 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).
176
+
177
+ - 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.
178
+
179
+ </section>
180
+
181
+ <!-- /.notes -->
182
+
183
+ <!-- Package usage examples. -->
184
+
185
+ <section class="examples">
186
+
187
+ ## Examples
188
+
189
+ <!-- eslint no-undef: "error" -->
190
+
191
+ ```javascript
192
+ var filledarrayBy = require( '@stdlib/array-filled-by' );
193
+ var discreteUniform = require( '@stdlib/random-base-discrete-uniform' ).factory;
194
+ var naryFunction = require( '@stdlib/utils-nary-function' );
195
+ var add = require( '@stdlib/math-base-ops-add' );
196
+ var abs = require( '@stdlib/math-base-special-abs' );
197
+ var array = require( '@stdlib/ndarray-array' );
198
+ var mapReduce = require( '@stdlib/utils-map-reduce' );
199
+
200
+ function fill( i ) {
201
+ var rand = discreteUniform( -10*(i+1), 10*(i+1) );
202
+ return filledarrayBy( 10, 'generic', rand );
203
+ }
204
+
205
+ // Create a two-dimensional ndarray (i.e., a matrix):
206
+ var x = array( filledarrayBy( 10, 'generic', fill ), {
207
+ 'dtype': 'generic',
208
+ 'flatten': true
209
+ });
210
+
211
+ // Create an explicit unary function:
212
+ var f1 = naryFunction( abs, 1 );
213
+
214
+ // Create an explicit binary function:
215
+ var f2 = naryFunction( add, 2 );
216
+
217
+ // Compute the sum of absolute values:
218
+ var out = mapReduce( x, 0, f1, f2 );
219
+
220
+ console.log( 'x:' );
221
+ console.log( x.data );
222
+
223
+ console.log( 'sum: %d', out );
224
+ ```
225
+
226
+ </section>
227
+
228
+ <!-- /.examples -->
229
+
230
+ <!-- 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. -->
231
+
232
+ <section class="references">
233
+
234
+ </section>
235
+
236
+ <!-- /.references -->
237
+
238
+ <!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
239
+
240
+ <section class="related">
241
+
242
+ </section>
243
+
244
+ <!-- /.related -->
245
+
246
+ <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
247
+
248
+
249
+ <section class="main-repo" >
250
+
251
+ * * *
252
+
253
+ ## Notice
254
+
255
+ This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more.
256
+
257
+ For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib].
258
+
259
+ #### Community
260
+
261
+ [![Chat][chat-image]][chat-url]
262
+
263
+ ---
264
+
265
+ ## License
266
+
267
+ See [LICENSE][stdlib-license].
268
+
269
+
270
+ ## Copyright
271
+
272
+ Copyright &copy; 2016-2022. The Stdlib [Authors][stdlib-authors].
273
+
274
+ </section>
275
+
276
+ <!-- /.stdlib -->
277
+
278
+ <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
279
+
280
+ <section class="links">
281
+
282
+ [npm-image]: http://img.shields.io/npm/v/@stdlib/utils-map-reduce.svg
283
+ [npm-url]: https://npmjs.org/package/@stdlib/utils-map-reduce
284
+
285
+ [test-image]: https://github.com/stdlib-js/utils-map-reduce/actions/workflows/test.yml/badge.svg
286
+ [test-url]: https://github.com/stdlib-js/utils-map-reduce/actions/workflows/test.yml
287
+
288
+ [coverage-image]: https://img.shields.io/codecov/c/github/stdlib-js/utils-map-reduce/main.svg
289
+ [coverage-url]: https://codecov.io/github/stdlib-js/utils-map-reduce?branch=main
290
+
291
+ <!--
292
+
293
+ [dependencies-image]: https://img.shields.io/david/stdlib-js/utils-map-reduce.svg
294
+ [dependencies-url]: https://david-dm.org/stdlib-js/utils-map-reduce/main
295
+
296
+ -->
297
+
298
+ [umd]: https://github.com/umdjs/umd
299
+ [es-module]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules
300
+
301
+ [deno-url]: https://github.com/stdlib-js/utils-map-reduce/tree/deno
302
+ [umd-url]: https://github.com/stdlib-js/utils-map-reduce/tree/umd
303
+ [esm-url]: https://github.com/stdlib-js/utils-map-reduce/tree/esm
304
+
305
+ [chat-image]: https://img.shields.io/gitter/room/stdlib-js/stdlib.svg
306
+ [chat-url]: https://gitter.im/stdlib-js/stdlib/
307
+
308
+ [stdlib]: https://github.com/stdlib-js/stdlib
309
+
310
+ [stdlib-authors]: https://github.com/stdlib-js/stdlib/graphs/contributors
311
+
312
+ [stdlib-license]: https://raw.githubusercontent.com/stdlib-js/utils-map-reduce/main/LICENSE
313
+
314
+ [@stdlib/ndarray/ctor]: https://www.npmjs.com/package/@stdlib/stdlib
315
+
316
+ [@stdlib/array/complex64]: https://www.npmjs.com/package/@stdlib/stdlib
317
+
318
+ [@stdlib/array/complex128]: https://www.npmjs.com/package/@stdlib/stdlib
319
+
320
+ </section>
321
+
322
+ <!-- /.links -->
package/docs/repl.txt ADDED
@@ -0,0 +1,65 @@
1
+
2
+ {{alias}}( arr, initial, mapper, reducer[, thisArg] )
3
+ Performs a map-reduce operation for each element in an array and returns the
4
+ accumulated result.
5
+
6
+ When invoked, the mapping function is provided three arguments:
7
+
8
+ - value: array element.
9
+ - index: element index.
10
+ - arr: input array.
11
+
12
+ When invoked, the reducing function is provided four arguments:
13
+
14
+ - accumulator: accumulated value.
15
+ - value: result after applying the mapping function to the current array
16
+ element.
17
+ - index: element index.
18
+ - arr: input array.
19
+
20
+ If provided an empty array, the function returns the initial value.
21
+
22
+ When provided an ndarray, the function performs a single-pass map-reduce
23
+ operation over the entire input ndarray (i.e., higher-order ndarray
24
+ dimensions are flattened to a single-dimension).
25
+
26
+ Parameters
27
+ ----------
28
+ arr: ArrayLikeObject|ndarray
29
+ Input array.
30
+
31
+ initial: any
32
+ Accumulator value used in the first invocation of the reduction
33
+ function.
34
+
35
+ mapper: Function
36
+ Mapping function.
37
+
38
+ reducer: Function
39
+ Reducing function.
40
+
41
+ thisArg: any (optional)
42
+ Execution context for the reducing function.
43
+
44
+ Returns
45
+ -------
46
+ out: any
47
+ Accumulated result.
48
+
49
+ Examples
50
+ --------
51
+ // array-like object:
52
+ > var f1 = {{alias:@stdlib/utils/nary-function}}( {{alias:@stdlib/math/base/special/abs}}, 1 );
53
+ > var f2 = {{alias:@stdlib/utils/nary-function}}( {{alias:@stdlib/math/base/ops/add}}, 2 );
54
+ > var arr = [ -1.0, -2.0, -3.0, -4.0, -5.0, -6.0 ];
55
+ > var out = {{alias}}( arr, 0.0, f1, f2 )
56
+ 21.0
57
+
58
+ // ndarray:
59
+ > arr = {{alias:@stdlib/ndarray/array}}( arr, { 'shape': [ 2, 3 ] } );
60
+ > out = {{alias}}( arr, 0.0, f1, f2 )
61
+ 21.0
62
+
63
+ See Also
64
+ --------
65
+
@@ -0,0 +1,257 @@
1
+ /*
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2022 The Stdlib Authors.
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+
19
+ // TypeScript Version: 2.0
20
+
21
+ /// <reference types="@stdlib/types"/>
22
+
23
+ import { Collection } from '@stdlib/types/object';
24
+ import { ndarray } from '@stdlib/types/ndarray';
25
+
26
+ /**
27
+ * Callback invoked for each array element.
28
+ *
29
+ * @returns result
30
+ */
31
+ type Nullary = () => any;
32
+
33
+ /**
34
+ * Callback invoked for each array element.
35
+ *
36
+ * @param value - array element
37
+ * @returns result
38
+ */
39
+ type Unary = ( value: any ) => any;
40
+
41
+ /**
42
+ * Callback invoked for each array element.
43
+ *
44
+ * @param value - array element
45
+ * @param index - element index
46
+ * @returns result
47
+ */
48
+ type Binary = ( value: any, index: number ) => any;
49
+
50
+ /**
51
+ * Callback invoked for each array element.
52
+ *
53
+ * @param value - array element
54
+ * @param index - element index
55
+ * @param arr - array
56
+ * @returns result
57
+ */
58
+ type Ternary = ( value: any, index: number, arr: ndarray ) => any;
59
+
60
+ /**
61
+ * Callback invoked for each array element.
62
+ *
63
+ * @param value - array element
64
+ * @param index - element index
65
+ * @param arr - array
66
+ * @returns result
67
+ */
68
+ type ArrayTernary = ( value: any, index: number, arr: Collection ) => any;
69
+
70
+ /**
71
+ * Callback invoked for each array element.
72
+ *
73
+ * @param value - array element
74
+ * @param index - element index
75
+ * @param arr - array
76
+ * @returns result
77
+ */
78
+ type Mapper = Nullary | Unary | Binary | Ternary;
79
+
80
+ /**
81
+ * Callback invoked for each array element.
82
+ *
83
+ * @param value - array element
84
+ * @param index - element index
85
+ * @param arr - array
86
+ * @returns result
87
+ */
88
+ type ArrayMapper = Nullary | Unary | Binary | ArrayTernary;
89
+
90
+ /**
91
+ * Function applied against an accumulator.
92
+ *
93
+ * @returns accumulator value
94
+ */
95
+ type NullaryReducer = () => void;
96
+
97
+ /**
98
+ * Function applied against an accumulator.
99
+ *
100
+ * @param accumulator - accumulated value
101
+ * @returns accumulator value
102
+ */
103
+ type UnaryReducer = ( accumulator: any ) => any;
104
+
105
+ /**
106
+ * Function applied against an accumulator.
107
+ *
108
+ * @param accumulator - accumulated value
109
+ * @param value - array element
110
+ * @returns accumulator value
111
+ */
112
+ type BinaryReducer = ( accumulator: any, value: any ) => any;
113
+
114
+ /**
115
+ * Function applied against an accumulator.
116
+ *
117
+ * @param accumulator - accumulated value
118
+ * @param value - array element
119
+ * @param index - element index
120
+ * @returns accumulator value
121
+ */
122
+ type TernaryReducer = ( accumulator: any, value: any, index: number ) => any;
123
+
124
+ /**
125
+ * Function applied against an accumulator.
126
+ *
127
+ * @param accumulator - accumulated value
128
+ * @param value - array element
129
+ * @param index - element index
130
+ * @param arr - array
131
+ * @returns accumulator value
132
+ */
133
+ type QuaternaryReducer = ( accumulator: any, value: any, index: number, arr: ndarray ) => any; // tslint:disable-line:max-line-length
134
+
135
+ /**
136
+ * Function applied against an accumulator.
137
+ *
138
+ * @param accumulator - accumulated value
139
+ * @param value - array element
140
+ * @param index - element index
141
+ * @param arr - array
142
+ * @returns accumulator value
143
+ */
144
+ type ArrayQuaternaryReducer = ( accumulator: any, value: any, index: number, arr: Collection ) => any; // tslint:disable-line:max-line-length
145
+
146
+ /**
147
+ * Function applied against an accumulator.
148
+ *
149
+ * @param accumulator - accumulated value
150
+ * @param value - array element
151
+ * @param index - element index
152
+ * @param arr - array
153
+ * @returns accumulator value
154
+ */
155
+ type Reducer = NullaryReducer | UnaryReducer | BinaryReducer | TernaryReducer | QuaternaryReducer; // tslint:disable-line:max-line-length
156
+
157
+ /**
158
+ * Function applied against an accumulator.
159
+ *
160
+ * @param accumulator - accumulated value
161
+ * @param value - array element
162
+ * @param index - element index
163
+ * @param arr - array
164
+ * @returns accumulator value
165
+ */
166
+ type ArrayReducer = NullaryReducer | UnaryReducer | BinaryReducer | TernaryReducer | ArrayQuaternaryReducer; // tslint:disable-line:max-line-length
167
+
168
+ /**
169
+ * Performs a map-reduce operation for each element in an array and returns the accumulated result.
170
+ *
171
+ * ## Notes
172
+ *
173
+ * - The mapping function is provided four arguments:
174
+ *
175
+ * - **value**: array element.
176
+ * - **index**: element index.
177
+ * - **arr**: input array.
178
+ *
179
+ * - The reducing function is provided four arguments:
180
+ *
181
+ * - **accumulator**: accumulated value.
182
+ * - **value**: result after applying a mapping function to the current array element.
183
+ * - **index**: element index.
184
+ * - **arr**: input array.
185
+ *
186
+ * - If provided an empty array, the function returns the initial value.
187
+ *
188
+ * @param arr - input array
189
+ * @param initial - initial value
190
+ * @param mapper - mapping function
191
+ * @param reducer - reducing function
192
+ * @param thisArg - reducing function execution context
193
+ * @returns accumulated result
194
+ *
195
+ * @example
196
+ * var naryFunction = require( `@stdlib/utils/nary-function` );
197
+ * var abs = require( `@stdlib/math/base/special/abs` );
198
+ * var add = require( `@stdlib/math/base/ops/add` );
199
+ * var array = require( `@stdlib/ndarray/array` );
200
+ *
201
+ * var opts = {
202
+ * 'dtype': 'generic'
203
+ * };
204
+ * var arr = array( [ [ -1, -2, -3 ], [ -4, -5, -6 ] ], opts );
205
+ *
206
+ * var out = mapReduce( arr, 0, naryFunction( abs, 2 ), naryFunction( add, 2 ) );
207
+ * // returns 21
208
+ */
209
+ declare function mapReduce( arr: ndarray, initial: any, mapper: Mapper, reducer: Reducer, thisArg?: any ): any; // tslint:disable-line:max-line-length
210
+
211
+ /**
212
+ * Performs a map-reduce operation for each element in an array and returns the accumulated result.
213
+ *
214
+ * ## Notes
215
+ *
216
+ * - The mapping function is provided four arguments:
217
+ *
218
+ * - **value**: array element.
219
+ * - **index**: element index.
220
+ * - **arr**: input array.
221
+ *
222
+ * - The reducing function is provided four arguments:
223
+ *
224
+ * - **accumulator**: accumulated value.
225
+ * - **value**: result after applying a mapping function to the current array element.
226
+ * - **index**: element index.
227
+ * - **arr**: input array.
228
+ *
229
+ * - If provided an empty array, the function returns the initial value.
230
+ *
231
+ * @param arr - input array
232
+ * @param initial - initial value
233
+ * @param mapper - mapping function
234
+ * @param reducer - reducing function
235
+ * @param thisArg - reducing function execution context
236
+ * @returns accumulated result
237
+ *
238
+ * @example
239
+ * function square( value ) {
240
+ * return value * value;
241
+ * }
242
+ *
243
+ * function sum( acc, value ) {
244
+ * return acc + value;
245
+ * }
246
+ *
247
+ * var arr = [ 1, 2, 3, 4 ];
248
+ *
249
+ * var out = mapReduce( arr, 0, square, sum );
250
+ * // returns 30
251
+ */
252
+ declare function mapReduce( arr: Collection, initial: any, mapper: ArrayMapper, reducer: ArrayReducer, thisArg?: any ): any; // tslint:disable-line:max-line-length
253
+
254
+
255
+ // EXPORTS //
256
+
257
+ export = mapReduce;