@stdlib/utils-reduce 0.0.7 → 0.0.8

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/NOTICE CHANGED
@@ -1 +1 @@
1
- Copyright (c) 2016-2021 The Stdlib Authors.
1
+ Copyright (c) 2016-2022 The Stdlib Authors.
package/README.md CHANGED
@@ -20,9 +20,9 @@ limitations under the License.
20
20
 
21
21
  # reduce
22
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]
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
24
 
25
- > Apply a function against an accumulator and each element in a collection and return the accumulated result.
25
+ > Apply a function against an accumulator and each element in an array and return the accumulated result.
26
26
 
27
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
28
 
@@ -52,9 +52,9 @@ npm install @stdlib/utils-reduce
52
52
  var reduce = require( '@stdlib/utils-reduce' );
53
53
  ```
54
54
 
55
- #### reduce( collection, initial, reducer\[, thisArg ] )
55
+ #### reduce( arr, initial, reducer\[, thisArg ] )
56
56
 
57
- Applies a `function` against an accumulator and each element in a `collection` and returns the accumulated result.
57
+ Applies a function against an accumulator and each element in an array and returns the accumulated result.
58
58
 
59
59
  ```javascript
60
60
  function sum( accumulator, value ) {
@@ -67,40 +67,34 @@ var out = reduce( arr, 0, sum );
67
67
  // returns 10
68
68
  ```
69
69
 
70
- The `reducer` function is provided four arguments:
71
-
72
- - `accumulator`: accumulated value
73
- - `value`: collection element
74
- - `index`: collection index
75
- - `collection`: input collection
76
-
77
- Basic support for dynamic collections is provided. Note, however, that index incrementation is monotonically increasing.
70
+ The function accepts both array-like objects and [`ndarray`][@stdlib/ndarray/ctor]-like objects.
78
71
 
79
72
  ```javascript
80
- function sum1( accumulator, value, index, collection ) {
81
- if ( index === collection.length-1 && collection.length < 10 ) {
82
- collection.push( index+2 );
83
- }
73
+ var array = require( '@stdlib/ndarray-array' );
74
+
75
+ function sum( accumulator, value ) {
84
76
  return accumulator + value;
85
77
  }
86
78
 
87
- var arr = [ 1, 2, 3, 4 ];
79
+ var opts = {
80
+ 'dtype': 'generic'
81
+ };
82
+ var arr = array( [ [ 1, 2, 3 ], [ 4, 5, 6 ] ], opts );
88
83
 
89
- var out = reduce( arr, 0, sum1 );
90
- // returns 55
84
+ var out = reduce( arr, 0, sum );
85
+ // returns 21
86
+ ```
91
87
 
92
- function sum2( accumulator, value, index, collection ) {
93
- collection.shift();
94
- return accumulator + value;
95
- }
88
+ The applied function is provided the following arguments:
96
89
 
97
- arr = [ 1, 2, 3, 4 ];
90
+ - **accumulator**: accumulated value.
91
+ - **value**: array element.
92
+ - **index**: element index.
93
+ - **arr**: input array.
98
94
 
99
- out = reduce( arr, 0, sum2 );
100
- // returns 4
101
- ```
95
+ To set the `this` context when invoking the input function, provide a `thisArg`.
102
96
 
103
- To set the function execution context, provide a `thisArg`.
97
+ <!-- eslint-disable no-invalid-this -->
104
98
 
105
99
  ```javascript
106
100
  function sum( accumulator, value ) {
@@ -110,14 +104,14 @@ function sum( accumulator, value ) {
110
104
 
111
105
  var arr = [ 1, 2, 3, 4 ];
112
106
 
113
- var context = {
107
+ var ctx = {
114
108
  'count': 0
115
109
  };
116
110
 
117
- var out = reduce( arr, 0, sum, context );
111
+ var out = reduce( arr, 0, sum, ctx );
118
112
  // returns 10
119
113
 
120
- var mean = out / context.count;
114
+ var mean = out / ctx.count;
121
115
  // returns 2.5
122
116
  ```
123
117
 
@@ -131,36 +125,44 @@ var mean = out / context.count;
131
125
 
132
126
  ## Notes
133
127
 
134
- - 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`).
135
-
136
- - The function differs from [`Array.prototype.reduce`][mdn-array-reduce] in the following ways:
128
+ - For input arrays, the function differs from [`Array.prototype.reduce`][mdn-array-reduce] in the following ways:
137
129
 
138
130
  - The function **requires** an `initial` value for the `accumulator`. The `initial` value is used during the first invocation of the `reducer` function.
139
-
140
- - The function does **not** skip the first element in the `collection`.
141
-
131
+ - The function does **not** skip the first array element.
142
132
  - The function does **not** skip `undefined` elements.
133
+ - The function does **not** support dynamic array-like objects (i.e., array-like objects whose `length` changes during execution).
134
+
135
+ - 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).
136
+
137
+ ```javascript
138
+ var Complex64Array = require( '@stdlib/array-complex64' );
139
+ var Complex64 = require( '@stdlib/complex-float32' );
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.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] );
143
152
 
144
- <!-- eslint-disable no-sparse-arrays, stdlib/doctest-marker -->
153
+ var v = reduce( x, new Complex64( 0.0, 0.0 ), sum );
154
+ // returns <Complex64>
145
155
 
146
- ```javascript
147
- function log( accumulator, value, index ) {
148
- console.log( '%s: %s', index, value );
149
- return accumulator;
150
- }
156
+ var re = realf( v );
157
+ // returns 16.0
151
158
 
152
- var arr = [ 1, , , 4 ];
159
+ var im = imagf( v );
160
+ // returns 20.0
161
+ ```
153
162
 
154
- var out = reduce( arr, 0, log );
155
- /* =>
156
- 0: 1
157
- 1: undefined
158
- 2: undefined
159
- 3: 4
160
- */
161
- ```
163
+ - For [`ndarray`][@stdlib/ndarray/ctor]-like objects, the function performs a reduction over the entire input [`ndarray`][@stdlib/ndarray/ctor] (i.e., higher-order [`ndarray`][@stdlib/ndarray/ctor] dimensions are flattened to a single-dimension).
162
164
 
163
- - The function provides limited support for dynamic collections (i.e., collections whose `length` changes during execution).
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.
164
166
 
165
167
  </section>
166
168
 
@@ -175,23 +177,34 @@ var mean = out / context.count;
175
177
  <!-- eslint no-undef: "error" -->
176
178
 
177
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 array = require( '@stdlib/ndarray-array' );
178
185
  var reduce = require( '@stdlib/utils-reduce' );
179
186
 
180
- var arr;
181
- var s;
182
- var i;
183
-
184
- function sum( acc, value ) {
185
- return acc + value;
187
+ function fill( i ) {
188
+ var rand = discreteUniform( -10*(i+1), 10*(i+1) );
189
+ return filledarrayBy( 10, 'generic', rand );
186
190
  }
187
191
 
188
- arr = new Array( 1000 );
189
- for ( i = 0; i < arr.length; i++ ) {
190
- arr[ i ] = i;
191
- }
192
+ // Create a two-dimensional ndarray (i.e., a matrix):
193
+ var x = array( filledarrayBy( 10, 'generic', fill ), {
194
+ 'dtype': 'generic',
195
+ 'flatten': true
196
+ });
197
+
198
+ // Create an explicit binary function:
199
+ var f = naryFunction( add, 2 );
200
+
201
+ // Compute the sum:
202
+ var out = reduce( x, 0, f );
203
+
204
+ console.log( 'x:' );
205
+ console.log( x.data );
192
206
 
193
- s = reduce( arr, 0, sum );
194
- console.log( s );
207
+ console.log( 'sum: %d', out );
195
208
  ```
196
209
 
197
210
  </section>
@@ -206,6 +219,22 @@ console.log( s );
206
219
 
207
220
  <!-- /.references -->
208
221
 
222
+ <!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
223
+
224
+ <section class="related">
225
+
226
+ * * *
227
+
228
+ ## See Also
229
+
230
+ - <span class="package-name">[`@stdlib/utils/for-each`][@stdlib/utils/for-each]</span><span class="delimiter">: </span><span class="description">invoke a function for each element in a collection.</span>
231
+ - <span class="package-name">[`@stdlib/utils/async/reduce`][@stdlib/utils/async/reduce]</span><span class="delimiter">: </span><span class="description">apply a function against an accumulator and each element in a collection and return the accumulated result.</span>
232
+ - <span class="package-name">[`@stdlib/utils/reduce-right`][@stdlib/utils/reduce-right]</span><span class="delimiter">: </span><span class="description">apply a function against an accumulator and each element in a collection and return the accumulated result, iterating from right to left.</span>
233
+
234
+ </section>
235
+
236
+ <!-- /.related -->
237
+
209
238
  <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
210
239
 
211
240
 
@@ -232,7 +261,7 @@ See [LICENSE][stdlib-license].
232
261
 
233
262
  ## Copyright
234
263
 
235
- Copyright &copy; 2016-2021. The Stdlib [Authors][stdlib-authors].
264
+ Copyright &copy; 2016-2022. The Stdlib [Authors][stdlib-authors].
236
265
 
237
266
  </section>
238
267
 
@@ -251,9 +280,20 @@ Copyright &copy; 2016-2021. The Stdlib [Authors][stdlib-authors].
251
280
  [coverage-image]: https://img.shields.io/codecov/c/github/stdlib-js/utils-reduce/main.svg
252
281
  [coverage-url]: https://codecov.io/github/stdlib-js/utils-reduce?branch=main
253
282
 
283
+ <!--
284
+
254
285
  [dependencies-image]: https://img.shields.io/david/stdlib-js/utils-reduce.svg
255
286
  [dependencies-url]: https://david-dm.org/stdlib-js/utils-reduce/main
256
287
 
288
+ -->
289
+
290
+ [umd]: https://github.com/umdjs/umd
291
+ [es-module]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules
292
+
293
+ [deno-url]: https://github.com/stdlib-js/utils-reduce/tree/deno
294
+ [umd-url]: https://github.com/stdlib-js/utils-reduce/tree/umd
295
+ [esm-url]: https://github.com/stdlib-js/utils-reduce/tree/esm
296
+
257
297
  [chat-image]: https://img.shields.io/gitter/room/stdlib-js/stdlib.svg
258
298
  [chat-url]: https://gitter.im/stdlib-js/stdlib/
259
299
 
@@ -263,13 +303,23 @@ Copyright &copy; 2016-2021. The Stdlib [Authors][stdlib-authors].
263
303
 
264
304
  [stdlib-license]: https://raw.githubusercontent.com/stdlib-js/utils-reduce/main/LICENSE
265
305
 
266
- [mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
306
+ [mdn-array-reduce]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce
267
307
 
268
- [mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
308
+ [@stdlib/ndarray/ctor]: https://www.npmjs.com/package/@stdlib/stdlib
269
309
 
270
- [mdn-object]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object
310
+ [@stdlib/array/complex64]: https://www.npmjs.com/package/@stdlib/stdlib
271
311
 
272
- [mdn-array-reduce]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce
312
+ [@stdlib/array/complex128]: https://www.npmjs.com/package/@stdlib/stdlib
313
+
314
+ <!-- <related-links> -->
315
+
316
+ [@stdlib/utils/for-each]: https://www.npmjs.com/package/@stdlib/utils-for-each
317
+
318
+ [@stdlib/utils/async/reduce]: https://www.npmjs.com/package/@stdlib/utils-async-reduce
319
+
320
+ [@stdlib/utils/reduce-right]: https://www.npmjs.com/package/@stdlib/utils-reduce-right
321
+
322
+ <!-- </related-links> -->
273
323
 
274
324
  </section>
275
325
 
package/docs/repl.txt CHANGED
@@ -1,29 +1,32 @@
1
1
 
2
- {{alias}}( collection, initial, reducer[, thisArg] )
3
- Applies a function against an accumulator and each element in a collection
4
- and returns the accumulated result.
2
+ {{alias}}( arr, initial, reducer[, thisArg] )
3
+ Applies a function against an accumulator and each element in an array and
4
+ returns the accumulated result.
5
5
 
6
6
  When invoked, the reduction function is provided four arguments:
7
7
 
8
- - `accumulator`: accumulated value
9
- - `value`: collection value
10
- - `index`: collection index
11
- - `collection`: the input collection
8
+ - accumulator: accumulated value.
9
+ - value: array element.
10
+ - index: element index.
11
+ - arr: input array.
12
12
 
13
- If provided an empty collection, the function returns the initial value.
13
+ If provided an empty array, the function returns the initial value.
14
+
15
+ When provided an ndarray, the function performs a reduction over the entire
16
+ input ndarray (i.e., higher-order ndarray dimensions are flattened to a
17
+ single-dimension).
14
18
 
15
19
  Parameters
16
20
  ----------
17
- collection: Array|TypedArray|Object
18
- Input collection. If provided an object, the object must be array-like
19
- (excluding strings and functions).
21
+ arr: ArrayLikeObject|ndarray
22
+ Input array.
20
23
 
21
24
  initial: any
22
25
  Accumulator value used in the first invocation of the reduction
23
26
  function.
24
27
 
25
28
  reducer: Function
26
- Function to invoke for each element in the input collection.
29
+ Function to invoke for each element in the input array.
27
30
 
28
31
  thisArg: any (optional)
29
32
  Execution context.
@@ -35,10 +38,16 @@
35
38
 
36
39
  Examples
37
40
  --------
38
- > function sum( acc, v ) { return acc + v; };
39
- > var arr = [ 1.0, 2.0, 3.0 ];
40
- > var out = {{alias}}( arr, 0, sum )
41
- 6.0
41
+ // array-like object:
42
+ > var f = {{alias:@stdlib/utils/nary-function}}( {{alias:@stdlib/math/base/ops/add}}, 2 );
43
+ > var arr = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ];
44
+ > var out = {{alias}}( arr, 0.0, f )
45
+ 21.0
46
+
47
+ // ndarray:
48
+ > arr = {{alias:@stdlib/ndarray/array}}( arr, { 'shape': [ 2, 3 ] } );
49
+ > out = {{alias}}( arr, 0.0, f )
50
+ 21.0
42
51
 
43
52
  See Also
44
53
  --------
@@ -21,6 +21,7 @@
21
21
  /// <reference types="@stdlib/types"/>
22
22
 
23
23
  import { Collection } from '@stdlib/types/object';
24
+ import { ndarray } from '@stdlib/types/ndarray';
24
25
 
25
26
  /**
26
27
  * Function applied against an accumulator.
@@ -41,7 +42,7 @@ type Unary = ( accumulator: any ) => any;
41
42
  * Function applied against an accumulator.
42
43
  *
43
44
  * @param accumulator - accumulated value
44
- * @param value - collection value
45
+ * @param value - array element
45
46
  * @returns accumulator value
46
47
  */
47
48
  type Binary = ( accumulator: any, value: any ) => any;
@@ -50,49 +51,106 @@ type Binary = ( accumulator: any, value: any ) => any;
50
51
  * Function applied against an accumulator.
51
52
  *
52
53
  * @param accumulator - accumulated value
53
- * @param value - collection value
54
- * @param index - collection index
54
+ * @param value - array element
55
+ * @param index - element index
55
56
  * @returns accumulator value
56
57
  */
57
- type Tertiary = ( accumulator: any, value: any, index: number ) => any;
58
+ type Ternary = ( accumulator: any, value: any, index: number ) => any;
58
59
 
59
60
  /**
60
61
  * Function applied against an accumulator.
61
62
  *
62
63
  * @param accumulator - accumulated value
63
- * @param value - collection value
64
- * @param index - collection index
65
- * @param collection - the input collection
64
+ * @param value - array element
65
+ * @param index - element index
66
+ * @param arr - array
66
67
  * @returns accumulator value
67
68
  */
68
- type Quaternary = ( accumulator: any, value: any, index: number, collection: Collection ) => any; // tslint-disable-line max-line-length
69
+ type Quaternary = ( accumulator: any, value: any, index: number, arr: ndarray ) => any; // tslint-disable-line max-line-length
69
70
 
70
71
  /**
71
72
  * Function applied against an accumulator.
72
73
  *
73
74
  * @param accumulator - accumulated value
74
- * @param value - collection value
75
- * @param index - collection index
76
- * @param collection - the input collection
75
+ * @param value - array element
76
+ * @param index - element index
77
+ * @param arr - array
77
78
  * @returns accumulator value
78
79
  */
79
- type Reducer = Nullary | Unary | Binary | Tertiary | Quaternary;
80
+ type ArrayQuaternary = ( accumulator: any, value: any, index: number, arr: Collection ) => any; // tslint-disable-line max-line-length
80
81
 
81
82
  /**
82
- * Applies a function against an accumulator and each element in a collection and returns the accumulated result.
83
+ * Function applied against an accumulator.
84
+ *
85
+ * @param accumulator - accumulated value
86
+ * @param value - array element
87
+ * @param index - element index
88
+ * @param arr - array
89
+ * @returns accumulator value
90
+ */
91
+ type Reducer = Nullary | Unary | Binary | Ternary | Quaternary;
92
+
93
+ /**
94
+ * Function applied against an accumulator.
95
+ *
96
+ * @param accumulator - accumulated value
97
+ * @param value - array element
98
+ * @param index - element index
99
+ * @param arr - array
100
+ * @returns accumulator value
101
+ */
102
+ type ArrayReducer = Nullary | Unary | Binary | Ternary | ArrayQuaternary;
103
+
104
+ /**
105
+ * Applies a function against an accumulator and each element in an array and returns the accumulated result.
106
+ *
107
+ * ## Notes
108
+ *
109
+ * - The reduction function is provided four arguments:
110
+ *
111
+ * - **accumulator**: accumulated value.
112
+ * - **value**: array element.
113
+ * - **index**: element index.
114
+ * - **arr**: input array.
115
+ *
116
+ * - If provided an empty array, the function returns the initial value.
117
+ *
118
+ * @param arr - input array
119
+ * @param initial - initial value
120
+ * @param reducer - reduction function
121
+ * @param thisArg - reduction function execution context
122
+ * @returns accumulated result
123
+ *
124
+ * @example
125
+ * var naryFunction = require( `@stdlib/utils/nary-function` );
126
+ * var add = require( `@stdlib/math/base/ops/add` );
127
+ * var array = require( `@stdlib/ndarray/array` );
128
+ *
129
+ * var opts = {
130
+ * 'dtype': 'generic'
131
+ * };
132
+ * var arr = array( [ [ 1, 2, 3 ], [ 4, 5, 6 ] ], opts );
133
+ *
134
+ * var out = reduce( arr, 0, naryFunction( add, 2 ) );
135
+ * // returns 21
136
+ */
137
+ declare function reduce( arr: ndarray, initial: any, reducer: Reducer, thisArg?: any ): any; // tslint-disable-line max-line-length
138
+
139
+ /**
140
+ * Applies a function against an accumulator and each element in an array and returns the accumulated result.
83
141
  *
84
142
  * ## Notes
85
143
  *
86
- * - When invoked, the reduction function is provided four arguments:
144
+ * - The reduction function is provided four arguments:
87
145
  *
88
- * - `accumulator`: accumulated value
89
- * - `value`: collection value
90
- * - `index`: collection index
91
- * - `collection`: the input collection
146
+ * - **accumulator**: accumulated value.
147
+ * - **value**: array element.
148
+ * - **index**: element index.
149
+ * - **arr**: input array.
92
150
  *
93
- * - If provided an empty collection, the function returns the initial value.
151
+ * - If provided an empty array, the function returns the initial value.
94
152
  *
95
- * @param collection - input collection
153
+ * @param arr - input array
96
154
  * @param initial - initial value
97
155
  * @param reducer - reduction function
98
156
  * @param thisArg - reduction function execution context
@@ -105,10 +163,10 @@ type Reducer = Nullary | Unary | Binary | Tertiary | Quaternary;
105
163
  *
106
164
  * var arr = [ 1, 2, 3, 4 ];
107
165
  *
108
- * var s = reduce( arr, 0, sum );
166
+ * var out = reduce( arr, 0, sum );
109
167
  * // returns 10
110
168
  */
111
- declare function reduce( collection: Collection, initial: any, reducer: Reducer, thisArg?: any ): any; // tslint-disable-line max-line-length
169
+ declare function reduce( arr: Collection, initial: any, reducer: ArrayReducer, thisArg?: any ): any; // tslint-disable-line max-line-length
112
170
 
113
171
 
114
172
  // EXPORTS //
@@ -16,44 +16,112 @@
16
16
  * limitations under the License.
17
17
  */
18
18
 
19
+ import array = require( '@stdlib/ndarray-array' );
19
20
  import reduce = require( './index' );
20
21
 
21
- const sum = ( acc: number, value: number ): number => {
22
+ /**
23
+ * Callback function.
24
+ *
25
+ * @param acc - accumulated value
26
+ * @param v - array element
27
+ * @returns result
28
+ */
29
+ function clbk( acc: number, value: number ): number {
22
30
  return acc + value;
23
- };
31
+ }
24
32
 
25
33
 
26
34
  // TESTS //
27
35
 
28
- // The function returns the accumulated value...
36
+ // The function returns the accumulated value when provided a collection...
29
37
  {
30
- reduce( [ 0, 1, 1, NaN, 2 ], 0, sum ); // $ExpectType any
31
- reduce( [ -1, 1, 2 ], 100, sum ); // $ExpectType any
32
- reduce( [ -1, 1, 2 ], 0, sum, {} ); // $ExpectType any
38
+ reduce( [ 0, 1, 1, NaN, 2 ], 0, clbk ); // $ExpectType any
39
+ reduce( [ -1, 1, 2 ], 100, clbk ); // $ExpectType any
40
+ reduce( [ -1, 1, 2 ], 0, clbk, {} ); // $ExpectType any
33
41
  }
34
42
 
35
- // The compiler throws an error if the function is provided a first argument which is not a collection...
43
+ // The function returns the accumulated value when provided an ndarray...
36
44
  {
37
- reduce( 2, 0, sum ); // $ExpectError
38
- reduce( false, 0, sum ); // $ExpectError
39
- reduce( true, 0, sum ); // $ExpectError
40
- reduce( {}, 0, sum ); // $ExpectError
45
+ const arr = array( [ 1, 2, 3, 4, 5, 6 ] );
46
+
47
+ reduce( arr, 0, clbk ); // $ExpectType any
48
+ reduce( arr, 100, clbk ); // $ExpectType any
49
+ reduce( arr, 0, clbk, {} ); // $ExpectType any
41
50
  }
42
51
 
43
- // The compiler throws an error if the function is provided a third argument which is not a function...
52
+ // The compiler throws an error if the function is provided a first argument which is not a collection or ndarray...
44
53
  {
45
- reduce( [ 0, 1, 1, NaN, 2 ], 0, 2 ); // $ExpectError
46
- reduce( [ 0, 1, 1, NaN, 2 ], 0, false ); // $ExpectError
47
- reduce( [ 0, 1, 1, NaN, 2 ], 0, true ); // $ExpectError
48
- reduce( [ 0, 1, 1, NaN, 2 ], 0, 'abc' ); // $ExpectError
49
- reduce( [ 0, 1, 1, NaN, 2 ], 0, {} ); // $ExpectError
50
- reduce( [ 0, 1, 1, NaN, 2 ], 0, [] ); // $ExpectError
54
+ reduce( 2, 0, clbk ); // $ExpectError
55
+ reduce( false, 0, clbk ); // $ExpectError
56
+ reduce( true, 0, clbk ); // $ExpectError
57
+ reduce( null, 0, clbk ); // $ExpectError
58
+ reduce( {}, 0, clbk ); // $ExpectError
59
+
60
+ reduce( 2, 0, clbk, {} ); // $ExpectError
61
+ reduce( false, 0, clbk, {} ); // $ExpectError
62
+ reduce( true, 0, clbk, {} ); // $ExpectError
63
+ reduce( null, 0, clbk, {} ); // $ExpectError
64
+ reduce( {}, 0, clbk, {} ); // $ExpectError
51
65
  }
52
66
 
53
- // The compiler throws an error if the function is provided an invalid number of arguments...
67
+ // The compiler throws an error if the function is provided a third argument which is not a function with a supported signature...
54
68
  {
69
+ const arr1 = [ 0, 1, 1, NaN, 2 ];
70
+
71
+ reduce( arr1, 0, 'abc' ); // $ExpectError
72
+ reduce( arr1, 0, 2 ); // $ExpectError
73
+ reduce( arr1, 0, false ); // $ExpectError
74
+ reduce( arr1, 0, true ); // $ExpectError
75
+ reduce( arr1, 0, null ); // $ExpectError
76
+ reduce( arr1, 0, {} ); // $ExpectError
77
+ reduce( arr1, 0, [] ); // $ExpectError
78
+
79
+ reduce( arr1, 0, 'abc', {} ); // $ExpectError
80
+ reduce( arr1, 0, 2, {} ); // $ExpectError
81
+ reduce( arr1, 0, false, {} ); // $ExpectError
82
+ reduce( arr1, 0, true, {} ); // $ExpectError
83
+ reduce( arr1, 0, null, {} ); // $ExpectError
84
+ reduce( arr1, 0, {}, {} ); // $ExpectError
85
+ reduce( arr1, 0, [], {} ); // $ExpectError
86
+
87
+ reduce( arr1, 0, ( x: number, y: number, z: number, w: number ): number => x + y + z + w ); // $ExpectError
88
+ reduce( arr1, 0, ( x: number, y: number, z: number, w: number ): number => x + y + z + w, {} ); // $ExpectError
89
+
90
+ const arr2 = array( [ 0, 1, 1, NaN, 2 ] );
91
+
92
+ reduce( arr2, 0, 'abc' ); // $ExpectError
93
+ reduce( arr2, 0, 2 ); // $ExpectError
94
+ reduce( arr2, 0, false ); // $ExpectError
95
+ reduce( arr2, 0, true ); // $ExpectError
96
+ reduce( arr2, 0, null ); // $ExpectError
97
+ reduce( arr2, 0, {} ); // $ExpectError
98
+ reduce( arr2, 0, [] ); // $ExpectError
99
+
100
+ reduce( arr2, 0, 'abc', {} ); // $ExpectError
101
+ reduce( arr2, 0, 2, {} ); // $ExpectError
102
+ reduce( arr2, 0, false, {} ); // $ExpectError
103
+ reduce( arr2, 0, true, {} ); // $ExpectError
104
+ reduce( arr2, 0, null, {} ); // $ExpectError
105
+ reduce( arr2, 0, {}, {} ); // $ExpectError
106
+ reduce( arr2, 0, [], {} ); // $ExpectError
107
+
108
+ reduce( arr2, 0, ( x: number, y: number, z: number, w: number ): number => x + y + z + w ); // $ExpectError
109
+ reduce( arr2, 0, ( x: number, y: number, z: number, w: number ): number => x + y + z + w, {} ); // $ExpectError
110
+ }
111
+
112
+ // The compiler throws an error if the function is provided an unsupported number of arguments...
113
+ {
114
+ const arr1 = [ 1, 2, 3 ];
115
+
116
+ reduce(); // $ExpectError
117
+ reduce( arr1 ); // $ExpectError
118
+ reduce( arr1, 0 ); // $ExpectError
119
+ reduce( arr1, 0, clbk, {}, 3 ); // $ExpectError
120
+
121
+ const arr2 = array( [ 1, 2, 3 ] );
122
+
55
123
  reduce(); // $ExpectError
56
- reduce( [ 1, 2, 3 ] ); // $ExpectError
57
- reduce( [ 1, 2, 3 ], 0 ); // $ExpectError
58
- reduce( [ 1, 2, 3 ], 0, sum, {}, 3 ); // $ExpectError
124
+ reduce( arr2 ); // $ExpectError
125
+ reduce( arr2, 0 ); // $ExpectError
126
+ reduce( arr2, 0, clbk, {}, 3 ); // $ExpectError
59
127
  }
package/lib/array.js ADDED
@@ -0,0 +1,78 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2021 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
+ 'use strict';
20
+
21
+ // MAIN //
22
+
23
+ /**
24
+ * Applies a function against an accumulator and each element in an array and returns the accumulated result.
25
+ *
26
+ * @private
27
+ * @param {Object} x - object containing input array data
28
+ * @param {ArrayLikeObject} x.data - input array data
29
+ * @param {Function} x.getter - callback for accessing input array data elements
30
+ * @param {*} initial - initial value
31
+ * @param {Function} fcn - function to apply
32
+ * @param {*} thisArg - function execution context
33
+ * @returns {*} accumulated result
34
+ *
35
+ * @example
36
+ * function sum( acc, value ) {
37
+ * return acc + value;
38
+ * }
39
+ *
40
+ * // Define a getter:
41
+ * function getter( buf, idx ) {
42
+ * return buf[ idx ];
43
+ * }
44
+ *
45
+ * // Create the input array object:
46
+ * var x = {
47
+ * 'data': [ 1, 2, 3, 4 ],
48
+ * 'getter': getter
49
+ * };
50
+ *
51
+ * // Compute the sum:
52
+ * var out = reduce( x, 0, sum );
53
+ * // returns 10
54
+ */
55
+ function reduce( x, initial, fcn, thisArg ) {
56
+ var xbuf;
57
+ var get;
58
+ var acc;
59
+ var i;
60
+
61
+ // Cache reference to the input data:
62
+ xbuf = x.data;
63
+
64
+ // Cache the element accessor:
65
+ get = x.getter;
66
+
67
+ // Iterate over each element...
68
+ acc = initial;
69
+ for ( i = 0; i < xbuf.length; i++ ) {
70
+ acc = fcn.call( thisArg, acc, get( xbuf, i ), i, xbuf );
71
+ }
72
+ return acc;
73
+ }
74
+
75
+
76
+ // EXPORTS //
77
+
78
+ module.exports = reduce;
package/lib/index.js CHANGED
@@ -19,7 +19,7 @@
19
19
  'use strict';
20
20
 
21
21
  /**
22
- * Apply a function against an accumulator and each element in a collection and return the accumulated result.
22
+ * Apply a function against an accumulator and each element in an array and return the accumulated result.
23
23
  *
24
24
  * @module @stdlib/utils-reduce
25
25
  *
@@ -32,15 +32,29 @@
32
32
  *
33
33
  * var arr = [ 1, 2, 3, 4 ];
34
34
  *
35
- * var s = reduce( arr, 0, sum );
35
+ * var out = reduce( arr, 0, sum );
36
36
  * // returns 10
37
+ *
38
+ * @example
39
+ * var naryFunction = require( '@stdlib/utils-nary-function' );
40
+ * var add = require( '@stdlib/math-base-ops-add' );
41
+ * var array = require( '@stdlib/ndarray-array' );
42
+ * var reduce = require( '@stdlib/utils-reduce' );
43
+ *
44
+ * var opts = {
45
+ * 'dtype': 'generic'
46
+ * };
47
+ * var arr = array( [ [ 1, 2, 3 ], [ 4, 5, 6 ] ], opts );
48
+ *
49
+ * var out = reduce( arr, 0, naryFunction( add, 2 ) );
50
+ * // returns 21
37
51
  */
38
52
 
39
53
  // MODULES //
40
54
 
41
- var reduce = require( './reduce.js' );
55
+ var main = require( './main.js' );
42
56
 
43
57
 
44
58
  // EXPORTS //
45
59
 
46
- module.exports = reduce;
60
+ module.exports = main;
package/lib/main.js ADDED
@@ -0,0 +1,93 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2018 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
+ 'use strict';
20
+
21
+ // MODULES //
22
+
23
+ var isArrayLikeObject = require( '@stdlib/assert-is-array-like-object' );
24
+ var isndarrayLike = require( '@stdlib/assert-is-ndarray-like' );
25
+ var isFunction = require( '@stdlib/assert-is-function' );
26
+ var ndarraylike2object = require( '@stdlib/ndarray-base-ndarraylike2object' );
27
+ var arraylike2object = require( '@stdlib/array-base-arraylike2object' );
28
+ var ndarrayFcn = require( './ndarray.js' );
29
+ var arrayFcn = require( './array.js' );
30
+
31
+
32
+ // MAIN //
33
+
34
+ /**
35
+ * Applies a function against an accumulator and each element in an array and returns the accumulated result.
36
+ *
37
+ * ## Notes
38
+ *
39
+ * - The applied function is provided the following arguments:
40
+ *
41
+ * - **accumulator**: accumulated value.
42
+ * - **value**: array element.
43
+ * - **index**: element index.
44
+ * - **arr**: input array.
45
+ *
46
+ * @param {(ArrayLikeObject|ndarray)} arr - input array
47
+ * @param {*} initial - initial value
48
+ * @param {Function} reducer - reduction function
49
+ * @param {*} [thisArg] - reduction function execution context
50
+ * @throws {TypeError} first argument must be an array-like object or an ndarray
51
+ * @throws {TypeError} third argument must be a function
52
+ * @returns {*} accumulated result
53
+ *
54
+ * @example
55
+ * function sum( acc, value ) {
56
+ * return acc + value;
57
+ * }
58
+ *
59
+ * var arr = [ 1, 2, 3, 4 ];
60
+ *
61
+ * var out = reduce( arr, 0, sum );
62
+ * // returns 10
63
+ *
64
+ * @example
65
+ * var naryFunction = require( '@stdlib/utils-nary-function' );
66
+ * var add = require( '@stdlib/math-base-ops-add' );
67
+ * var array = require( '@stdlib/ndarray-array' );
68
+ *
69
+ * var opts = {
70
+ * 'dtype': 'generic'
71
+ * };
72
+ * var arr = array( [ [ 1, 2, 3 ], [ 4, 5, 6 ] ], opts );
73
+ *
74
+ * var out = reduce( arr, 0, naryFunction( add, 2 ) );
75
+ * // returns 21
76
+ */
77
+ function reduce( arr, initial, reducer, thisArg ) {
78
+ if ( !isFunction( reducer ) ) {
79
+ throw new TypeError( 'invalid argument. Third argument must be a function. Value:`' + reducer + '`.' );
80
+ }
81
+ if ( isndarrayLike( arr ) ) { // note: assertion order matters here, as an ndarray-like object is also array-like
82
+ return ndarrayFcn( ndarraylike2object( arr ), initial, reducer, thisArg ); // eslint-disable-line max-len
83
+ }
84
+ if ( isArrayLikeObject( arr ) ) {
85
+ return arrayFcn( arraylike2object( arr ), initial, reducer, thisArg );
86
+ }
87
+ throw new TypeError( 'invalid argument. First argument must be an array-like object or an ndarray. Value: `' + arr + '`.' );
88
+ }
89
+
90
+
91
+ // EXPORTS //
92
+
93
+ module.exports = reduce;
package/lib/ndarray.js ADDED
@@ -0,0 +1,153 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2021 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
+ 'use strict';
20
+
21
+ // MODULES //
22
+
23
+ var vind2bind = require( '@stdlib/ndarray-base-vind2bind' );
24
+
25
+
26
+ // VARIABLES //
27
+
28
+ var MODE = 'throw';
29
+
30
+
31
+ // MAIN //
32
+
33
+ /**
34
+ * Applies a function against an accumulator and each element in an array and returns the accumulated result.
35
+ *
36
+ * @private
37
+ * @param {Object} x - object containing input ndarray meta data
38
+ * @param {string} x.ref - reference to original input ndarray-like object
39
+ * @param {string} x.dtype - data type
40
+ * @param {Collection} x.data - data buffer
41
+ * @param {NonNegativeInteger} x.length - number of elements
42
+ * @param {NonNegativeIntegerArray} x.shape - dimensions
43
+ * @param {IntegerArray} x.strides - stride lengths
44
+ * @param {NonNegativeInteger} x.offset - index offset
45
+ * @param {string} x.order - specifies whether `x` is row-major (C-style) or column-major (Fortran-style)
46
+ * @param {Function} x.getter - callback for accessing `x` data buffer elements
47
+ * @param {*} initial - initial value
48
+ * @param {Function} fcn - function to apply
49
+ * @param {*} thisArg - function execution context
50
+ * @returns {*} accumulated result
51
+ *
52
+ * @example
53
+ * var Complex64Array = require( '@stdlib/array-complex64' );
54
+ * var Complex64 = require( '@stdlib/complex-float32' );
55
+ * var realf = require( '@stdlib/complex-realf' );
56
+ * var imagf = require( '@stdlib/complex-imagf' );
57
+ * var cadd = require( '@stdlib/math-base-ops-cadd' );
58
+ * var naryFunction = require( '@stdlib/utils-nary-function' );
59
+ *
60
+ * // Create a data buffer:
61
+ * var xbuf = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] );
62
+ *
63
+ * // Define the shape of the input array:
64
+ * var shape = [ 2, 2 ];
65
+ *
66
+ * // Define the array strides:
67
+ * var sx = [ 2, 1 ];
68
+ *
69
+ * // Define the index offset:
70
+ * var ox = 0;
71
+ *
72
+ * // Define a getter:
73
+ * function getter( buf, idx ) {
74
+ * return buf.get( idx );
75
+ * }
76
+ *
77
+ * // Create the input ndarray-like object:
78
+ * var x = {
79
+ * 'ref': null,
80
+ * 'dtype': 'complex64',
81
+ * 'data': xbuf,
82
+ * 'length': 4,
83
+ * 'shape': shape,
84
+ * 'strides': sx,
85
+ * 'offset': ox,
86
+ * 'order': 'row-major',
87
+ * 'getter': getter
88
+ * };
89
+ * x.ref = x;
90
+ *
91
+ * // Compute the sum:
92
+ * var v = reduce( x, new Complex64( 0.0, 0.0 ), naryFunction( cadd, 2 ) );
93
+ *
94
+ * var re = realf( v );
95
+ * // returns 16.0
96
+ *
97
+ * var im = imagf( v );
98
+ * // returns 20.0
99
+ */
100
+ function reduce( x, initial, fcn, thisArg ) {
101
+ var xbuf;
102
+ var ordx;
103
+ var acc;
104
+ var len;
105
+ var get;
106
+ var ref;
107
+ var shx;
108
+ var sx;
109
+ var ox;
110
+ var ix;
111
+ var i;
112
+
113
+ // Cache the total number of elements over which to iterate:
114
+ len = x.length;
115
+
116
+ // Cache the input array shape:
117
+ shx = x.shape;
118
+
119
+ // Cache reference to the input ndarray data buffer:
120
+ xbuf = x.data;
121
+
122
+ // Cache reference to the stride array:
123
+ sx = x.strides;
124
+
125
+ // Cache the index of the first indexed element:
126
+ ox = x.offset;
127
+
128
+ // Cache the array order:
129
+ ordx = x.order;
130
+
131
+ // Cache the element accessor:
132
+ get = x.getter;
133
+
134
+ // Cache the reference to the original input array:
135
+ ref = x.ref;
136
+
137
+ // Check for a zero-dimensional array...
138
+ if ( shx.length === 0 ) {
139
+ return fcn.call( thisArg, initial, get( xbuf, ox ), 0, ref );
140
+ }
141
+ // Iterate over each element based on the linear **view** index, regardless as to how the data is stored in memory (note: this has negative performance implications for non-contiguous ndarrays due to a lack of data locality)...
142
+ acc = initial;
143
+ for ( i = 0; i < len; i++ ) {
144
+ ix = vind2bind( shx, sx, ox, ordx, i, MODE );
145
+ acc = fcn.call( thisArg, acc, get( xbuf, ix ), i, ref );
146
+ }
147
+ return acc;
148
+ }
149
+
150
+
151
+ // EXPORTS //
152
+
153
+ module.exports = reduce;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@stdlib/utils-reduce",
3
- "version": "0.0.7",
4
- "description": "Apply a function against an accumulator and each element in a collection and return the accumulated result.",
3
+ "version": "0.0.8",
4
+ "description": "Apply a function against an accumulator and each element in an array and return the accumulated result.",
5
5
  "license": "Apache-2.0",
6
6
  "author": {
7
7
  "name": "The Stdlib Authors",
@@ -37,14 +37,31 @@
37
37
  "url": "https://github.com/stdlib-js/stdlib/issues"
38
38
  },
39
39
  "dependencies": {
40
- "@stdlib/assert-is-collection": "^0.0.x",
40
+ "@stdlib/array-base-arraylike2object": "^0.0.x",
41
+ "@stdlib/assert-is-array-like-object": "^0.0.x",
41
42
  "@stdlib/assert-is-function": "^0.0.x",
43
+ "@stdlib/assert-is-ndarray-like": "^0.0.x",
44
+ "@stdlib/ndarray-base-ndarraylike2object": "^0.0.x",
45
+ "@stdlib/ndarray-base-vind2bind": "^0.0.x",
42
46
  "@stdlib/types": "^0.0.x"
43
47
  },
44
48
  "devDependencies": {
49
+ "@stdlib/array-base-filled": "^0.0.x",
50
+ "@stdlib/array-complex64": "^0.0.x",
51
+ "@stdlib/array-filled-by": "^0.0.x",
45
52
  "@stdlib/array-float64": "^0.0.x",
46
53
  "@stdlib/bench": "^0.0.x",
54
+ "@stdlib/complex-float32": "^0.0.x",
55
+ "@stdlib/complex-imagf": "^0.0.x",
56
+ "@stdlib/complex-realf": "^0.0.x",
47
57
  "@stdlib/math-base-assert-is-nan": "^0.0.x",
58
+ "@stdlib/math-base-ops-add": "^0.0.x",
59
+ "@stdlib/math-base-ops-cadd": "^0.0.x",
60
+ "@stdlib/math-base-special-pow": "^0.0.x",
61
+ "@stdlib/ndarray-array": "^0.0.x",
62
+ "@stdlib/ndarray-ctor": "^0.0.x",
63
+ "@stdlib/random-base-discrete-uniform": "^0.0.x",
64
+ "@stdlib/utils-nary-function": "^0.0.x",
48
65
  "@stdlib/utils-noop": "^0.0.x",
49
66
  "tape": "git+https://github.com/kgryte/tape.git#fix/globby",
50
67
  "istanbul": "^0.4.1",
@@ -87,7 +104,11 @@
87
104
  "typed array",
88
105
  "typed",
89
106
  "array",
90
- "array-like"
107
+ "array-like",
108
+ "ndarray",
109
+ "tensor",
110
+ "matrix",
111
+ "vector"
91
112
  ],
92
113
  "funding": {
93
114
  "type": "patreon",
package/lib/reduce.js DELETED
@@ -1,76 +0,0 @@
1
- /**
2
- * @license Apache-2.0
3
- *
4
- * Copyright (c) 2018 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
- 'use strict';
20
-
21
- // MODULES //
22
-
23
- var isCollection = require( '@stdlib/assert-is-collection' );
24
- var isFunction = require( '@stdlib/assert-is-function' );
25
-
26
-
27
- // MAIN //
28
-
29
- /**
30
- * Applies a function against an accumulator and each element in a collection and returns the accumulated result.
31
- *
32
- * @param {Collection} collection - input collection
33
- * @param {*} initial - initial value
34
- * @param {Function} reducer - reduction function
35
- * @param {*} [thisArg] - reduction function execution context
36
- * @throws {TypeError} first argument must be a collection
37
- * @throws {TypeError} third argument must be a function
38
- * @returns {*} accumulated result
39
- *
40
- * @example
41
- * function sum( acc, value ) {
42
- * return acc + value;
43
- * }
44
- *
45
- * var arr = [ 1, 2, 3, 4 ];
46
- *
47
- * var s = reduce( arr, 0, sum );
48
- * // returns 10
49
- */
50
- function reduce( collection, initial, reducer, thisArg ) {
51
- var len;
52
- var acc;
53
- var i;
54
- if ( !isCollection( collection ) ) {
55
- throw new TypeError( 'invalid argument. First argument must be a collection. Value: `'+collection+'`.' );
56
- }
57
- if ( !isFunction( reducer ) ) {
58
- throw new TypeError( 'invalid argument. Third argument must be a function. Value:`'+reducer+'`.' );
59
- }
60
- len = collection.length;
61
- acc = initial;
62
- for ( i = 0; i < len; i++ ) {
63
- acc = reducer.call( thisArg, acc, collection[ i ], i, collection );
64
-
65
- // Account for dynamically resizing a collection...
66
- if ( len !== collection.length ) {
67
- len = collection.length;
68
- }
69
- }
70
- return acc;
71
- }
72
-
73
-
74
- // EXPORTS //
75
-
76
- module.exports = reduce;