@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.
@@ -0,0 +1,184 @@
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
+ import array = require( '@stdlib/ndarray-array' );
20
+ import mapReduce = require( './index' );
21
+
22
+ /**
23
+ * Mapping function.
24
+ *
25
+ * @param value - array element
26
+ * @returns result
27
+ */
28
+ function mapper( value: number ): number {
29
+ return value;
30
+ }
31
+
32
+ /**
33
+ * Reducing function.
34
+ *
35
+ * @param acc - accumulated value
36
+ * @param v - array element
37
+ * @returns result
38
+ */
39
+ function reducer( acc: number, value: number ): number {
40
+ return acc + value;
41
+ }
42
+
43
+
44
+ // TESTS //
45
+
46
+ // The function returns the accumulated value when provided a collection...
47
+ {
48
+ mapReduce( [ 0, 1, 1, NaN, 2 ], 0, mapper, reducer ); // $ExpectType any
49
+ mapReduce( [ -1, 1, 2 ], 100, mapper, reducer ); // $ExpectType any
50
+ mapReduce( [ -1, 1, 2 ], 0, mapper, reducer, {} ); // $ExpectType any
51
+ }
52
+
53
+ // The function returns the accumulated value when provided an ndarray...
54
+ {
55
+ const arr = array( [ 1, 2, 3, 4, 5, 6 ] );
56
+
57
+ mapReduce( arr, 0, mapper, reducer ); // $ExpectType any
58
+ mapReduce( arr, 100, mapper, reducer ); // $ExpectType any
59
+ mapReduce( arr, 0, mapper, reducer, {} ); // $ExpectType any
60
+ }
61
+
62
+ // The compiler throws an error if the function is provided a first argument which is not a collection or ndarray...
63
+ {
64
+ mapReduce( 2, 0, mapper, reducer ); // $ExpectError
65
+ mapReduce( false, 0, mapper, reducer ); // $ExpectError
66
+ mapReduce( true, 0, mapper, reducer ); // $ExpectError
67
+ mapReduce( null, 0, mapper, reducer ); // $ExpectError
68
+ mapReduce( {}, 0, mapper, reducer ); // $ExpectError
69
+
70
+ mapReduce( 2, 0, mapper, reducer, {} ); // $ExpectError
71
+ mapReduce( false, 0, mapper, reducer, {} ); // $ExpectError
72
+ mapReduce( true, 0, mapper, reducer, {} ); // $ExpectError
73
+ mapReduce( null, 0, mapper, reducer, {} ); // $ExpectError
74
+ mapReduce( {}, 0, mapper, reducer, {} ); // $ExpectError
75
+ }
76
+
77
+ // The compiler throws an error if the function is provided a third argument which is not a function with a supported signature...
78
+ {
79
+ const arr1 = [ 0, 1, 1, NaN, 2 ];
80
+
81
+ mapReduce( arr1, 0, 'abc', reducer ); // $ExpectError
82
+ mapReduce( arr1, 0, 2, reducer ); // $ExpectError
83
+ mapReduce( arr1, 0, false, reducer ); // $ExpectError
84
+ mapReduce( arr1, 0, true, reducer ); // $ExpectError
85
+ mapReduce( arr1, 0, null, reducer ); // $ExpectError
86
+ mapReduce( arr1, 0, {}, reducer ); // $ExpectError
87
+ mapReduce( arr1, 0, [], reducer ); // $ExpectError
88
+
89
+ mapReduce( arr1, 0, 'abc', reducer, {} ); // $ExpectError
90
+ mapReduce( arr1, 0, 2, reducer, {} ); // $ExpectError
91
+ mapReduce( arr1, 0, false, reducer, {} ); // $ExpectError
92
+ mapReduce( arr1, 0, true, reducer, {} ); // $ExpectError
93
+ mapReduce( arr1, 0, null, reducer, {} ); // $ExpectError
94
+ mapReduce( arr1, 0, {}, reducer, {} ); // $ExpectError
95
+ mapReduce( arr1, 0, [], reducer, {} ); // $ExpectError
96
+
97
+ mapReduce( arr1, 0, ( x: number, y: number, z: number, w: number ): number => x + y + z + w, reducer ); // $ExpectError
98
+ mapReduce( arr1, 0, ( x: number, y: number, z: number, w: number ): number => x + y + z + w, reducer, {} ); // $ExpectError
99
+
100
+ const arr2 = array( [ 0, 1, 1, NaN, 2 ] );
101
+
102
+ mapReduce( arr2, 0, 'abc', reducer ); // $ExpectError
103
+ mapReduce( arr2, 0, 2, reducer ); // $ExpectError
104
+ mapReduce( arr2, 0, false, reducer ); // $ExpectError
105
+ mapReduce( arr2, 0, true, reducer ); // $ExpectError
106
+ mapReduce( arr2, 0, null, reducer ); // $ExpectError
107
+ mapReduce( arr2, 0, {}, reducer ); // $ExpectError
108
+ mapReduce( arr2, 0, [], reducer ); // $ExpectError
109
+
110
+ mapReduce( arr2, 0, 'abc', reducer, {} ); // $ExpectError
111
+ mapReduce( arr2, 0, 2, reducer, {} ); // $ExpectError
112
+ mapReduce( arr2, 0, false, reducer, {} ); // $ExpectError
113
+ mapReduce( arr2, 0, true, reducer, {} ); // $ExpectError
114
+ mapReduce( arr2, 0, null, reducer, {} ); // $ExpectError
115
+ mapReduce( arr2, 0, {}, reducer, {} ); // $ExpectError
116
+ mapReduce( arr2, 0, [], reducer, {} ); // $ExpectError
117
+
118
+ mapReduce( arr2, 0, ( x: number, y: number, z: number, w: number ): number => x + y + z + w, reducer ); // $ExpectError
119
+ mapReduce( arr2, 0, ( x: number, y: number, z: number, w: number ): number => x + y + z + w, reducer, {} ); // $ExpectError
120
+ }
121
+
122
+ // The compiler throws an error if the function is provided a fourth argument which is not a function with a supported signature...
123
+ {
124
+ const arr1 = [ 0, 1, 1, NaN, 2 ];
125
+
126
+ mapReduce( arr1, 0, mapper, 'abc' ); // $ExpectError
127
+ mapReduce( arr1, 0, mapper, 2 ); // $ExpectError
128
+ mapReduce( arr1, 0, mapper, false ); // $ExpectError
129
+ mapReduce( arr1, 0, mapper, true ); // $ExpectError
130
+ mapReduce( arr1, 0, mapper, null ); // $ExpectError
131
+ mapReduce( arr1, 0, mapper, {} ); // $ExpectError
132
+ mapReduce( arr1, 0, mapper, [] ); // $ExpectError
133
+
134
+ mapReduce( arr1, 0, mapper, 'abc', {} ); // $ExpectError
135
+ mapReduce( arr1, 0, mapper, 2, {} ); // $ExpectError
136
+ mapReduce( arr1, 0, mapper, false, {} ); // $ExpectError
137
+ mapReduce( arr1, 0, mapper, true, {} ); // $ExpectError
138
+ mapReduce( arr1, 0, mapper, null, {} ); // $ExpectError
139
+ mapReduce( arr1, 0, mapper, {}, {} ); // $ExpectError
140
+ mapReduce( arr1, 0, mapper, [], {} ); // $ExpectError
141
+
142
+ mapReduce( arr1, 0, mapper, ( x: number, y: number, z: number, w: number ): number => x + y + z + w ); // $ExpectError
143
+ mapReduce( arr1, 0, mapper, ( x: number, y: number, z: number, w: number ): number => x + y + z + w, {} ); // $ExpectError
144
+
145
+ const arr2 = array( [ 0, 1, 1, NaN, 2 ] );
146
+
147
+ mapReduce( arr2, 0, mapper, 'abc' ); // $ExpectError
148
+ mapReduce( arr2, 0, mapper, 2 ); // $ExpectError
149
+ mapReduce( arr2, 0, mapper, false ); // $ExpectError
150
+ mapReduce( arr2, 0, mapper, true ); // $ExpectError
151
+ mapReduce( arr2, 0, mapper, null ); // $ExpectError
152
+ mapReduce( arr2, 0, mapper, {} ); // $ExpectError
153
+ mapReduce( arr2, 0, mapper, [] ); // $ExpectError
154
+
155
+ mapReduce( arr2, 0, mapper, 'abc', {} ); // $ExpectError
156
+ mapReduce( arr2, 0, mapper, 2, {} ); // $ExpectError
157
+ mapReduce( arr2, 0, mapper, false, {} ); // $ExpectError
158
+ mapReduce( arr2, 0, mapper, true, {} ); // $ExpectError
159
+ mapReduce( arr2, 0, mapper, null, {} ); // $ExpectError
160
+ mapReduce( arr2, 0, mapper, {}, {} ); // $ExpectError
161
+ mapReduce( arr2, 0, mapper, [], {} ); // $ExpectError
162
+
163
+ mapReduce( arr2, 0, mapper, ( x: number, y: number, z: number, w: number ): number => x + y + z + w ); // $ExpectError
164
+ mapReduce( arr2, 0, mapper, ( x: number, y: number, z: number, w: number ): number => x + y + z + w, {} ); // $ExpectError
165
+ }
166
+
167
+ // The compiler throws an error if the function is provided an unsupported number of arguments...
168
+ {
169
+ const arr1 = [ 1, 2, 3 ];
170
+
171
+ mapReduce(); // $ExpectError
172
+ mapReduce( arr1 ); // $ExpectError
173
+ mapReduce( arr1, 0 ); // $ExpectError
174
+ mapReduce( arr1, 0, mapper, ); // $ExpectError
175
+ mapReduce( arr1, 0, mapper, reducer, {}, 3 ); // $ExpectError
176
+
177
+ const arr2 = array( [ 1, 2, 3 ] );
178
+
179
+ mapReduce(); // $ExpectError
180
+ mapReduce( arr2 ); // $ExpectError
181
+ mapReduce( arr2, 0 ); // $ExpectError
182
+ mapReduce( arr2, 0, mapper ); // $ExpectError
183
+ mapReduce( arr2, 0, mapper, reducer, {}, 3 ); // $ExpectError
184
+ }
package/lib/array.js ADDED
@@ -0,0 +1,83 @@
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
+ 'use strict';
20
+
21
+ // MAIN //
22
+
23
+ /**
24
+ * Performs a map-reduce operation against 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} mapper - mapping function
32
+ * @param {Function} reducer - reducing function
33
+ * @param {*} thisArg - reducing function execution context
34
+ * @returns {*} accumulated result
35
+ *
36
+ * @example
37
+ * function square( value ) {
38
+ * return value * value;
39
+ * }
40
+ *
41
+ * function sum( acc, value ) {
42
+ * return acc + value;
43
+ * }
44
+ *
45
+ * // Define a getter:
46
+ * function getter( buf, idx ) {
47
+ * return buf[ idx ];
48
+ * }
49
+ *
50
+ * // Create the input array object:
51
+ * var x = {
52
+ * 'data': [ 1, 2, 3, 4 ],
53
+ * 'getter': getter
54
+ * };
55
+ *
56
+ * // Compute the sum of squared values:
57
+ * var out = mapReduce( x, 0, square, sum );
58
+ * // returns 30
59
+ */
60
+ function mapReduce( x, initial, mapper, reducer, thisArg ) {
61
+ var xbuf;
62
+ var get;
63
+ var acc;
64
+ var i;
65
+
66
+ // Cache reference to the input data:
67
+ xbuf = x.data;
68
+
69
+ // Cache the element accessor:
70
+ get = x.getter;
71
+
72
+ // Iterate over each element...
73
+ acc = initial;
74
+ for ( i = 0; i < xbuf.length; i++ ) {
75
+ acc = reducer.call( thisArg, acc, mapper( get( xbuf, i ), i, xbuf ), i, xbuf ); // eslint-disable-line max-len
76
+ }
77
+ return acc;
78
+ }
79
+
80
+
81
+ // EXPORTS //
82
+
83
+ module.exports = mapReduce;
package/lib/index.js ADDED
@@ -0,0 +1,65 @@
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
+ 'use strict';
20
+
21
+ /**
22
+ * Perform a map-reduce operation against each element in an array and return the accumulated result.
23
+ *
24
+ * @module @stdlib/utils-map-reduce
25
+ *
26
+ * @example
27
+ * var mapReduce = require( '@stdlib/utils-map-reduce' );
28
+ *
29
+ * function square( value ) {
30
+ * return value * value;
31
+ * }
32
+ *
33
+ * function sum( acc, value ) {
34
+ * return acc + value;
35
+ * }
36
+ *
37
+ * var arr = [ 1, 2, 3, 4 ];
38
+ *
39
+ * var out = mapReduce( arr, 0, sum );
40
+ * // returns 30
41
+ *
42
+ * @example
43
+ * var naryFunction = require( '@stdlib/utils-nary-function' );
44
+ * var add = require( '@stdlib/math-base-ops-add' );
45
+ * var abs = require( '@stdlib/math-base-special-abs' );
46
+ * var array = require( '@stdlib/ndarray-array' );
47
+ * var mapReduce = require( '@stdlib/utils-map-reduce' );
48
+ *
49
+ * var opts = {
50
+ * 'dtype': 'generic'
51
+ * };
52
+ * var arr = array( [ [ -1, -2, -3 ], [ -4, -5, -6 ] ], opts );
53
+ *
54
+ * var out = mapReduce( arr, 0, naryFunction( abs, 1 ), naryFunction( add, 2 ) );
55
+ * // returns 21
56
+ */
57
+
58
+ // MODULES //
59
+
60
+ var main = require( './main.js' );
61
+
62
+
63
+ // EXPORTS //
64
+
65
+ module.exports = main;
package/lib/main.js ADDED
@@ -0,0 +1,109 @@
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
+ '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
+ * Performs a map-reduce operation against each element in an array and returns the accumulated result.
36
+ *
37
+ * ## Notes
38
+ *
39
+ * - The mapping function is provided the following arguments:
40
+ *
41
+ * - **value**: array element.
42
+ * - **index**: element index.
43
+ * - **arr**: input array.
44
+ *
45
+ * - The reducing function is provided the following arguments:
46
+ *
47
+ * - **accumulator**: accumulated value.
48
+ * - **value**: result of applying the mapping function against the current array element.
49
+ * - **index**: element index.
50
+ * - **arr**: input array.
51
+ *
52
+ * @param {(ArrayLikeObject|ndarray)} arr - input array
53
+ * @param {*} initial - initial value
54
+ * @param {Function} mapper - mapping function
55
+ * @param {Function} reducer - reducing function
56
+ * @param {*} [thisArg] - reduction function execution context
57
+ * @throws {TypeError} first argument must be an array-like object or an ndarray
58
+ * @throws {TypeError} third argument must be a function
59
+ * @throws {TypeError} fourth argument must be a function
60
+ * @returns {*} accumulated result
61
+ *
62
+ * @example
63
+ * function square( value ) {
64
+ * return value * value;
65
+ * }
66
+ *
67
+ * function sum( acc, value ) {
68
+ * return acc + value;
69
+ * }
70
+ *
71
+ * var arr = [ 1, 2, 3, 4 ];
72
+ *
73
+ * var out = mapReduce( arr, 0, square, sum );
74
+ * // returns 30
75
+ *
76
+ * @example
77
+ * var naryFunction = require( '@stdlib/utils-nary-function' );
78
+ * var add = require( '@stdlib/math-base-ops-add' );
79
+ * var abs = require( '@stdlib/math-base-special-abs' );
80
+ * var array = require( '@stdlib/ndarray-array' );
81
+ *
82
+ * var opts = {
83
+ * 'dtype': 'generic'
84
+ * };
85
+ * var arr = array( [ [ -1, -2, -3 ], [ -4, -5, -6 ] ], opts );
86
+ *
87
+ * var out = mapReduce( arr, 0, naryFunction( abs, 1 ), naryFunction( add, 2 ) );
88
+ * // returns 21
89
+ */
90
+ function mapReduce( arr, initial, mapper, reducer, thisArg ) {
91
+ if ( !isFunction( mapper ) ) {
92
+ throw new TypeError( 'invalid argument. Third argument must be a function. Value:`' + mapper + '`.' );
93
+ }
94
+ if ( !isFunction( reducer ) ) {
95
+ throw new TypeError( 'invalid argument. Fourth argument must be a function. Value:`' + reducer + '`.' );
96
+ }
97
+ if ( isndarrayLike( arr ) ) { // note: assertion order matters here, as an ndarray-like object is also array-like
98
+ return ndarrayFcn( ndarraylike2object( arr ), initial, mapper, reducer, thisArg ); // eslint-disable-line max-len
99
+ }
100
+ if ( isArrayLikeObject( arr ) ) {
101
+ return arrayFcn( arraylike2object( arr ), initial, mapper, reducer, thisArg ); // eslint-disable-line max-len
102
+ }
103
+ throw new TypeError( 'invalid argument. First argument must be an array-like object or an ndarray. Value: `' + arr + '`.' );
104
+ }
105
+
106
+
107
+ // EXPORTS //
108
+
109
+ module.exports = mapReduce;
package/lib/ndarray.js ADDED
@@ -0,0 +1,155 @@
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
+ '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
+ * Performs a map-reduce operation against 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} mapper - mapping function
49
+ * @param {Function} reducer - reducing function
50
+ * @param {*} thisArg - reducing function execution context
51
+ * @returns {*} accumulated result
52
+ *
53
+ * @example
54
+ * var Complex64Array = require( '@stdlib/array-complex64' );
55
+ * var Complex64 = require( '@stdlib/complex-float32' );
56
+ * var realf = require( '@stdlib/complex-realf' );
57
+ * var imagf = require( '@stdlib/complex-imagf' );
58
+ * var cceil = require( '@stdlib/math-base-special-cceil' );
59
+ * var cadd = require( '@stdlib/math-base-ops-cadd' );
60
+ * var naryFunction = require( '@stdlib/utils-nary-function' );
61
+ *
62
+ * // Create a data buffer:
63
+ * var xbuf = new Complex64Array( [ 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5 ] );
64
+ *
65
+ * // Define the shape of the input array:
66
+ * var shape = [ 2, 2 ];
67
+ *
68
+ * // Define the array strides:
69
+ * var sx = [ 2, 1 ];
70
+ *
71
+ * // Define the index offset:
72
+ * var ox = 0;
73
+ *
74
+ * // Define a getter:
75
+ * function getter( buf, idx ) {
76
+ * return buf.get( idx );
77
+ * }
78
+ *
79
+ * // Create the input ndarray-like object:
80
+ * var x = {
81
+ * 'ref': null,
82
+ * 'dtype': 'complex64',
83
+ * 'data': xbuf,
84
+ * 'length': 4,
85
+ * 'shape': shape,
86
+ * 'strides': sx,
87
+ * 'offset': ox,
88
+ * 'order': 'row-major',
89
+ * 'getter': getter
90
+ * };
91
+ * x.ref = x;
92
+ *
93
+ * // Compute the sum:
94
+ * var v = mapReduce( x, new Complex64( 0.0, 0.0 ), naryFunction( cceil, 1 ), naryFunction( cadd, 2 ) );
95
+ *
96
+ * var re = realf( v );
97
+ * // returns 20.0
98
+ *
99
+ * var im = imagf( v );
100
+ * // returns 24.0
101
+ */
102
+ function mapReduce( x, initial, mapper, reducer, thisArg ) {
103
+ var xbuf;
104
+ var ordx;
105
+ var acc;
106
+ var len;
107
+ var get;
108
+ var ref;
109
+ var shx;
110
+ var sx;
111
+ var ox;
112
+ var ix;
113
+ var i;
114
+
115
+ // Cache the total number of elements over which to iterate:
116
+ len = x.length;
117
+
118
+ // Cache the input array shape:
119
+ shx = x.shape;
120
+
121
+ // Cache reference to the input ndarray data buffer:
122
+ xbuf = x.data;
123
+
124
+ // Cache reference to the stride array:
125
+ sx = x.strides;
126
+
127
+ // Cache the index of the first indexed element:
128
+ ox = x.offset;
129
+
130
+ // Cache the array order:
131
+ ordx = x.order;
132
+
133
+ // Cache the element accessor:
134
+ get = x.getter;
135
+
136
+ // Cache the reference to the original input array:
137
+ ref = x.ref;
138
+
139
+ // Check for a zero-dimensional array...
140
+ if ( shx.length === 0 ) {
141
+ return reducer.call( thisArg, initial, mapper( get( xbuf, ox ), 0, ref ), 0, ref ); // eslint-disable-line max-len
142
+ }
143
+ // 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)...
144
+ acc = initial;
145
+ for ( i = 0; i < len; i++ ) {
146
+ ix = vind2bind( shx, sx, ox, ordx, i, MODE );
147
+ acc = reducer.call( thisArg, acc, mapper( get( xbuf, ix ), i, ref ), i, ref ); // eslint-disable-line max-len
148
+ }
149
+ return acc;
150
+ }
151
+
152
+
153
+ // EXPORTS //
154
+
155
+ module.exports = mapReduce;