@stdlib/utils-reduce 0.0.7 → 0.1.0

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/dist/index.js ADDED
@@ -0,0 +1,10 @@
1
+ "use strict";var d=function(r,e){return function(){return e||r((e={exports:{}}).exports,e),e.exports}};var m=d(function(H,b){
2
+ var j=require('@stdlib/ndarray-base-vind2bind/dist'),F="throw";function x(r,e,a,u){var i,s,t,n,o,c,f,q,l,y,v;if(n=r.length,f=r.shape,i=r.data,q=r.strides,l=r.offset,s=r.order,o=r.accessors[0],c=r.ref,f.length===0)return a.call(u,e,o(i,l),0,c);for(t=e,v=0;v<n;v++)y=j(f,q,l,s,v,F),t=a.call(u,t,o(i,y),v,c);return t}b.exports=x
3
+ });var h=d(function(I,g){
4
+ function E(r,e,a,u){var i,s,t,n;for(i=r.data,s=r.accessors[0],t=e,n=0;n<i.length;n++)t=a.call(u,t,s(i,n),n,i);return t}g.exports=E
5
+ });var w=d(function(J,k){
6
+ var T=require('@stdlib/assert-is-array-like-object/dist'),L=require('@stdlib/assert-is-ndarray-like/dist'),O=require('@stdlib/assert-is-function/dist'),V=require('@stdlib/ndarray-base-ndarraylike2object/dist'),D=require('@stdlib/array-base-arraylike2object/dist'),p=require('@stdlib/error-tools-fmtprodmsg/dist'),M=m(),z=h();function B(r,e,a,u){if(!O(a))throw new TypeError(p('1XH3N',a));if(L(r))return M(V(r),e,a,u);if(T(r))return z(D(r),e,a,u);throw new TypeError(p('1XHBH',r))}k.exports=B
7
+ });var C=w();module.exports=C;
8
+ /** @license Apache-2.0 */
9
+ /** @license Apache-2.0 */
10
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../lib/ndarray.js", "../lib/array.js", "../lib/main.js", "../lib/index.js"],
4
+ "sourcesContent": ["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2021 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar vind2bind = require( '@stdlib/ndarray-base-vind2bind' );\n\n\n// VARIABLES //\n\nvar MODE = 'throw';\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in an array and returns the accumulated result.\n*\n* @private\n* @param {Object} x - object containing input ndarray meta data\n* @param {string} x.ref - reference to original input ndarray-like object\n* @param {string} x.dtype - data type\n* @param {Collection} x.data - data buffer\n* @param {NonNegativeInteger} x.length - number of elements\n* @param {NonNegativeIntegerArray} x.shape - dimensions\n* @param {IntegerArray} x.strides - stride lengths\n* @param {NonNegativeInteger} x.offset - index offset\n* @param {string} x.order - specifies whether `x` is row-major (C-style) or column-major (Fortran-style)\n* @param {Array<Function>} x.accessors - accessors for accessing data buffer elements\n* @param {*} initial - initial value\n* @param {Function} fcn - function to apply\n* @param {*} thisArg - function execution context\n* @returns {*} accumulated result\n*\n* @example\n* var Complex64Array = require( '@stdlib/array-complex64' );\n* var Complex64 = require( '@stdlib/complex-float32' );\n* var realf = require( '@stdlib/complex-realf' );\n* var imagf = require( '@stdlib/complex-imagf' );\n* var cadd = require( '@stdlib/math-base-ops-cadd' );\n* var naryFunction = require( '@stdlib/utils-nary-function' );\n*\n* // Create a data buffer:\n* var xbuf = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] );\n*\n* // Define the shape of the input array:\n* var shape = [ 2, 2 ];\n*\n* // Define the array strides:\n* var sx = [ 2, 1 ];\n*\n* // Define the index offset:\n* var ox = 0;\n*\n* // Define a getter:\n* function getter( buf, idx ) {\n* return buf.get( idx );\n* }\n*\n* // Create the input ndarray-like object:\n* var x = {\n* 'ref': null,\n* 'dtype': 'complex64',\n* 'data': xbuf,\n* 'length': 4,\n* 'shape': shape,\n* 'strides': sx,\n* 'offset': ox,\n* 'order': 'row-major',\n* 'accessors': [ getter ]\n* };\n* x.ref = x;\n*\n* // Compute the sum:\n* var v = reduce( x, new Complex64( 0.0, 0.0 ), naryFunction( cadd, 2 ) );\n*\n* var re = realf( v );\n* // returns 16.0\n*\n* var im = imagf( v );\n* // returns 20.0\n*/\nfunction reduce( x, initial, fcn, thisArg ) {\n\tvar xbuf;\n\tvar ordx;\n\tvar acc;\n\tvar len;\n\tvar get;\n\tvar ref;\n\tvar shx;\n\tvar sx;\n\tvar ox;\n\tvar ix;\n\tvar i;\n\n\t// Cache the total number of elements over which to iterate:\n\tlen = x.length;\n\n\t// Cache the input array shape:\n\tshx = x.shape;\n\n\t// Cache reference to the input ndarray data buffer:\n\txbuf = x.data;\n\n\t// Cache reference to the stride array:\n\tsx = x.strides;\n\n\t// Cache the index of the first indexed element:\n\tox = x.offset;\n\n\t// Cache the array order:\n\tordx = x.order;\n\n\t// Cache the element accessor:\n\tget = x.accessors[ 0 ];\n\n\t// Cache the reference to the original input array:\n\tref = x.ref;\n\n\t// Check for a zero-dimensional array...\n\tif ( shx.length === 0 ) {\n\t\treturn fcn.call( thisArg, initial, get( xbuf, ox ), 0, ref );\n\t}\n\t// 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)...\n\tacc = initial;\n\tfor ( i = 0; i < len; i++ ) {\n\t\tix = vind2bind( shx, sx, ox, ordx, i, MODE );\n\t\tacc = fcn.call( thisArg, acc, get( xbuf, ix ), i, ref );\n\t}\n\treturn acc;\n}\n\n\n// EXPORTS //\n\nmodule.exports = reduce;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2021 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in an array and returns the accumulated result.\n*\n* @private\n* @param {Object} x - object containing input array data\n* @param {ArrayLikeObject} x.data - input array data\n* @param {Array<Function>} x.accessors - input array accessors\n* @param {*} initial - initial value\n* @param {Function} fcn - function to apply\n* @param {*} thisArg - function execution context\n* @returns {*} accumulated result\n*\n* @example\n* function sum( acc, value ) {\n* return acc + value;\n* }\n*\n* // Define a getter:\n* function getter( buf, idx ) {\n* return buf[ idx ];\n* }\n*\n* // Create the input array object:\n* var x = {\n* 'data': [ 1, 2, 3, 4 ],\n* 'accessors': [ getter ]\n* };\n*\n* // Compute the sum:\n* var out = reduce( x, 0, sum );\n* // returns 10\n*/\nfunction reduce( x, initial, fcn, thisArg ) {\n\tvar xbuf;\n\tvar get;\n\tvar acc;\n\tvar i;\n\n\t// Cache reference to the input data:\n\txbuf = x.data;\n\n\t// Cache the element accessor:\n\tget = x.accessors[ 0 ];\n\n\t// Iterate over each element...\n\tacc = initial;\n\tfor ( i = 0; i < xbuf.length; i++ ) {\n\t\tacc = fcn.call( thisArg, acc, get( xbuf, i ), i, xbuf );\n\t}\n\treturn acc;\n}\n\n\n// EXPORTS //\n\nmodule.exports = reduce;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isArrayLikeObject = require( '@stdlib/assert-is-array-like-object' );\nvar isndarrayLike = require( '@stdlib/assert-is-ndarray-like' );\nvar isFunction = require( '@stdlib/assert-is-function' );\nvar ndarraylike2object = require( '@stdlib/ndarray-base-ndarraylike2object' );\nvar arraylike2object = require( '@stdlib/array-base-arraylike2object' );\nvar format = require( '@stdlib/string-format' );\nvar ndarrayFcn = require( './ndarray.js' );\nvar arrayFcn = require( './array.js' );\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in an array and returns the accumulated result.\n*\n* ## Notes\n*\n* - The applied function is provided the following arguments:\n*\n* - **accumulator**: accumulated value.\n* - **value**: array element.\n* - **index**: element index.\n* - **arr**: input array.\n*\n* @param {(ArrayLikeObject|ndarray)} arr - input array\n* @param {*} initial - initial value\n* @param {Function} reducer - reduction function\n* @param {*} [thisArg] - reduction function execution context\n* @throws {TypeError} first argument must be an array-like object or an ndarray\n* @throws {TypeError} third argument must be a function\n* @returns {*} accumulated result\n*\n* @example\n* function sum( acc, value ) {\n* return acc + value;\n* }\n*\n* var arr = [ 1, 2, 3, 4 ];\n*\n* var out = reduce( arr, 0, sum );\n* // returns 10\n*\n* @example\n* var naryFunction = require( '@stdlib/utils-nary-function' );\n* var add = require( '@stdlib/math-base-ops-add' );\n* var array = require( '@stdlib/ndarray-array' );\n*\n* var opts = {\n* 'dtype': 'generic'\n* };\n* var arr = array( [ [ 1, 2, 3 ], [ 4, 5, 6 ] ], opts );\n*\n* var out = reduce( arr, 0, naryFunction( add, 2 ) );\n* // returns 21\n*/\nfunction reduce( arr, initial, reducer, thisArg ) {\n\tif ( !isFunction( reducer ) ) {\n\t\tthrow new TypeError( format( 'invalid argument. Third argument must be a function. Value: `%s`.', reducer ) );\n\t}\n\tif ( isndarrayLike( arr ) ) { // note: assertion order matters here, as an ndarray-like object is also array-like\n\t\treturn ndarrayFcn( ndarraylike2object( arr ), initial, reducer, thisArg ); // eslint-disable-line max-len\n\t}\n\tif ( isArrayLikeObject( arr ) ) {\n\t\treturn arrayFcn( arraylike2object( arr ), initial, reducer, thisArg );\n\t}\n\tthrow new TypeError( format( 'invalid argument. First argument must be an array-like object or an ndarray. Value: `%s`.', arr ) );\n}\n\n\n// EXPORTS //\n\nmodule.exports = reduce;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in an array and return the accumulated result.\n*\n* @module @stdlib/utils-reduce\n*\n* @example\n* var reduce = require( '@stdlib/utils-reduce' );\n*\n* function sum( acc, value ) {\n* return acc + value;\n* }\n*\n* var arr = [ 1, 2, 3, 4 ];\n*\n* var out = reduce( arr, 0, sum );\n* // returns 10\n*\n* @example\n* var naryFunction = require( '@stdlib/utils-nary-function' );\n* var add = require( '@stdlib/math-base-ops-add' );\n* var array = require( '@stdlib/ndarray-array' );\n* var reduce = require( '@stdlib/utils-reduce' );\n*\n* var opts = {\n* 'dtype': 'generic'\n* };\n* var arr = array( [ [ 1, 2, 3 ], [ 4, 5, 6 ] ], opts );\n*\n* var out = reduce( arr, 0, naryFunction( add, 2 ) );\n* // returns 21\n*/\n\n// MODULES //\n\nvar main = require( './main.js' );\n\n\n// EXPORTS //\n\nmodule.exports = main;\n"],
5
+ "mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAY,QAAS,gCAAiC,EAKtDC,EAAO,QAwEX,SAASC,EAAQC,EAAGC,EAASC,EAAKC,EAAU,CAC3C,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EA2BJ,GAxBAP,EAAMP,EAAE,OAGRU,EAAMV,EAAE,MAGRI,EAAOJ,EAAE,KAGTW,EAAKX,EAAE,QAGPY,EAAKZ,EAAE,OAGPK,EAAOL,EAAE,MAGTQ,EAAMR,EAAE,UAAW,CAAE,EAGrBS,EAAMT,EAAE,IAGHU,EAAI,SAAW,EACnB,OAAOR,EAAI,KAAMC,EAASF,EAASO,EAAKJ,EAAMQ,CAAG,EAAG,EAAGH,CAAI,EAI5D,IADAH,EAAML,EACAa,EAAI,EAAGA,EAAIP,EAAKO,IACrBD,EAAKhB,EAAWa,EAAKC,EAAIC,EAAIP,EAAMS,EAAGhB,CAAK,EAC3CQ,EAAMJ,EAAI,KAAMC,EAASG,EAAKE,EAAKJ,EAAMS,CAAG,EAAGC,EAAGL,CAAI,EAEvD,OAAOH,CACR,CAKAV,EAAO,QAAUG,ICxJjB,IAAAgB,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsDA,SAASC,EAAQC,EAAGC,EAASC,EAAKC,EAAU,CAC3C,IAAIC,EACAC,EACAC,EACAC,EAUJ,IAPAH,EAAOJ,EAAE,KAGTK,EAAML,EAAE,UAAW,CAAE,EAGrBM,EAAML,EACAM,EAAI,EAAGA,EAAIH,EAAK,OAAQG,IAC7BD,EAAMJ,EAAI,KAAMC,EAASG,EAAKD,EAAKD,EAAMG,CAAE,EAAGA,EAAGH,CAAK,EAEvD,OAAOE,CACR,CAKAR,EAAO,QAAUC,IC7EjB,IAAAS,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAoB,QAAS,qCAAsC,EACnEC,EAAgB,QAAS,gCAAiC,EAC1DC,EAAa,QAAS,4BAA6B,EACnDC,EAAqB,QAAS,yCAA0C,EACxEC,EAAmB,QAAS,qCAAsC,EAClEC,EAAS,QAAS,uBAAwB,EAC1CC,EAAa,IACbC,EAAW,IAgDf,SAASC,EAAQC,EAAKC,EAASC,EAASC,EAAU,CACjD,GAAK,CAACV,EAAYS,CAAQ,EACzB,MAAM,IAAI,UAAWN,EAAQ,oEAAqEM,CAAQ,CAAE,EAE7G,GAAKV,EAAeQ,CAAI,EACvB,OAAOH,EAAYH,EAAoBM,CAAI,EAAGC,EAASC,EAASC,CAAQ,EAEzE,GAAKZ,EAAmBS,CAAI,EAC3B,OAAOF,EAAUH,EAAkBK,CAAI,EAAGC,EAASC,EAASC,CAAQ,EAErE,MAAM,IAAI,UAAWP,EAAQ,4FAA6FI,CAAI,CAAE,CACjI,CAKAV,EAAO,QAAUS,ICvCjB,IAAIK,EAAO,IAKX,OAAO,QAAUA",
6
+ "names": ["require_ndarray", "__commonJSMin", "exports", "module", "vind2bind", "MODE", "reduce", "x", "initial", "fcn", "thisArg", "xbuf", "ordx", "acc", "len", "get", "ref", "shx", "sx", "ox", "ix", "i", "require_array", "__commonJSMin", "exports", "module", "reduce", "x", "initial", "fcn", "thisArg", "xbuf", "get", "acc", "i", "require_main", "__commonJSMin", "exports", "module", "isArrayLikeObject", "isndarrayLike", "isFunction", "ndarraylike2object", "arraylike2object", "format", "ndarrayFcn", "arrayFcn", "reduce", "arr", "initial", "reducer", "thisArg", "main"]
7
+ }
@@ -16,18 +16,19 @@
16
16
  * limitations under the License.
17
17
  */
18
18
 
19
- // TypeScript Version: 2.0
19
+ // TypeScript Version: 4.1
20
20
 
21
21
  /// <reference types="@stdlib/types"/>
22
22
 
23
- import { Collection } from '@stdlib/types/object';
23
+ import { Collection } from '@stdlib/types/array';
24
+ import { typedndarray } from '@stdlib/types/ndarray';
24
25
 
25
26
  /**
26
27
  * Function applied against an accumulator.
27
28
  *
28
29
  * @returns accumulator value
29
30
  */
30
- type Nullary = () => void;
31
+ type Nullary<U, V> = ( this: V ) => U;
31
32
 
32
33
  /**
33
34
  * Function applied against an accumulator.
@@ -35,64 +36,121 @@ type Nullary = () => void;
35
36
  * @param accumulator - accumulated value
36
37
  * @returns accumulator value
37
38
  */
38
- type Unary = ( accumulator: any ) => any;
39
+ type Unary<U, V> = ( this: V, accumulator: U ) => U;
39
40
 
40
41
  /**
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
- type Binary = ( accumulator: any, value: any ) => any;
48
+ type Binary<T, U, V> = ( this: V, accumulator: U, value: T ) => U;
48
49
 
49
50
  /**
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<T, U, V> = ( this: V, accumulator: U, value: T, index: number ) => U;
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<T, U, V> = ( this: V, accumulator: U, value: T, index: number, arr: typedndarray<T> ) => U;
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<T, U, V> = ( this: V, accumulator: U, value: T, index: number, arr: Collection<T> ) => U;
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<T, U, V> = Nullary<U, V> | Unary<U, V> | Binary<T, U, V> | Ternary<T, U, V> | Quaternary<T, U, V>;
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<T, U, V> = Nullary<U, V> | Unary<U, V> | Binary<T, U, V> | Ternary<T, U, V> | ArrayQuaternary<T, U, V>;
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<T = unknown, U = unknown, V = unknown>( arr: typedndarray<T>, initial: U, reducer: Reducer<T, U, V>, thisArg?: ThisParameterType<Reducer<T, U, V>> ): U;
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<T = unknown, U = unknown, V = unknown>( arr: Collection<T>, initial: U, reducer: ArrayReducer<T, U, V>, thisArg?: ThisParameterType<ArrayReducer<T, U, V>> ): U;
112
170
 
113
171
 
114
172
  // EXPORTS //
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 {Array<Function>} x.accessors - input array accessors
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
+ * 'accessors': [ 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.accessors[ 0 ];
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,94 @@
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 format = require( '@stdlib/string-format' );
29
+ var ndarrayFcn = require( './ndarray.js' );
30
+ var arrayFcn = require( './array.js' );
31
+
32
+
33
+ // MAIN //
34
+
35
+ /**
36
+ * Applies a function against an accumulator and each element in an array and returns the accumulated result.
37
+ *
38
+ * ## Notes
39
+ *
40
+ * - The applied function is provided the following arguments:
41
+ *
42
+ * - **accumulator**: accumulated value.
43
+ * - **value**: array element.
44
+ * - **index**: element index.
45
+ * - **arr**: input array.
46
+ *
47
+ * @param {(ArrayLikeObject|ndarray)} arr - input array
48
+ * @param {*} initial - initial value
49
+ * @param {Function} reducer - reduction function
50
+ * @param {*} [thisArg] - reduction function execution context
51
+ * @throws {TypeError} first argument must be an array-like object or an ndarray
52
+ * @throws {TypeError} third argument must be a function
53
+ * @returns {*} accumulated result
54
+ *
55
+ * @example
56
+ * function sum( acc, value ) {
57
+ * return acc + value;
58
+ * }
59
+ *
60
+ * var arr = [ 1, 2, 3, 4 ];
61
+ *
62
+ * var out = reduce( arr, 0, sum );
63
+ * // returns 10
64
+ *
65
+ * @example
66
+ * var naryFunction = require( '@stdlib/utils-nary-function' );
67
+ * var add = require( '@stdlib/math-base-ops-add' );
68
+ * var array = require( '@stdlib/ndarray-array' );
69
+ *
70
+ * var opts = {
71
+ * 'dtype': 'generic'
72
+ * };
73
+ * var arr = array( [ [ 1, 2, 3 ], [ 4, 5, 6 ] ], opts );
74
+ *
75
+ * var out = reduce( arr, 0, naryFunction( add, 2 ) );
76
+ * // returns 21
77
+ */
78
+ function reduce( arr, initial, reducer, thisArg ) {
79
+ if ( !isFunction( reducer ) ) {
80
+ throw new TypeError( format( 'invalid argument. Third argument must be a function. Value: `%s`.', reducer ) );
81
+ }
82
+ if ( isndarrayLike( arr ) ) { // note: assertion order matters here, as an ndarray-like object is also array-like
83
+ return ndarrayFcn( ndarraylike2object( arr ), initial, reducer, thisArg ); // eslint-disable-line max-len
84
+ }
85
+ if ( isArrayLikeObject( arr ) ) {
86
+ return arrayFcn( arraylike2object( arr ), initial, reducer, thisArg );
87
+ }
88
+ throw new TypeError( format( 'invalid argument. First argument must be an array-like object or an ndarray. Value: `%s`.', arr ) );
89
+ }
90
+
91
+
92
+ // EXPORTS //
93
+
94
+ 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 {Array<Function>} x.accessors - accessors for accessing 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
+ * 'accessors': [ 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.accessors[ 0 ];
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.1.0",
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,18 +37,37 @@
37
37
  "url": "https://github.com/stdlib-js/stdlib/issues"
38
38
  },
39
39
  "dependencies": {
40
- "@stdlib/assert-is-collection": "^0.0.x",
41
- "@stdlib/assert-is-function": "^0.0.x",
42
- "@stdlib/types": "^0.0.x"
40
+ "@stdlib/array-base-arraylike2object": "^0.1.0",
41
+ "@stdlib/assert-is-array-like-object": "^0.1.0",
42
+ "@stdlib/assert-is-function": "^0.1.0",
43
+ "@stdlib/assert-is-ndarray-like": "^0.1.0",
44
+ "@stdlib/ndarray-base-ndarraylike2object": "^0.1.0",
45
+ "@stdlib/ndarray-base-vind2bind": "^0.1.0",
46
+ "@stdlib/string-format": "^0.1.0",
47
+ "@stdlib/types": "^0.1.0",
48
+ "@stdlib/error-tools-fmtprodmsg": "^0.1.0"
43
49
  },
44
50
  "devDependencies": {
45
- "@stdlib/array-float64": "^0.0.x",
46
- "@stdlib/bench": "^0.0.x",
47
- "@stdlib/math-base-assert-is-nan": "^0.0.x",
48
- "@stdlib/utils-noop": "^0.0.x",
51
+ "@stdlib/array-base-filled": "^0.1.0",
52
+ "@stdlib/array-complex64": "^0.1.0",
53
+ "@stdlib/array-filled-by": "^0.0.2",
54
+ "@stdlib/array-float64": "^0.1.0",
55
+ "@stdlib/bench": "^0.1.0",
56
+ "@stdlib/complex-float32": "^0.1.0",
57
+ "@stdlib/complex-imagf": "^0.1.0",
58
+ "@stdlib/complex-realf": "^0.1.0",
59
+ "@stdlib/math-base-assert-is-nan": "^0.1.0",
60
+ "@stdlib/math-base-ops-add": "^0.1.0",
61
+ "@stdlib/math-base-ops-cadd": "^0.1.0",
62
+ "@stdlib/math-base-special-pow": "^0.1.0",
63
+ "@stdlib/ndarray-array": "^0.0.9",
64
+ "@stdlib/ndarray-ctor": "^0.1.0",
65
+ "@stdlib/random-base-discrete-uniform": "^0.0.6",
66
+ "@stdlib/utils-nary-function": "^0.1.0",
67
+ "@stdlib/utils-noop": "^0.1.0",
49
68
  "tape": "git+https://github.com/kgryte/tape.git#fix/globby",
50
69
  "istanbul": "^0.4.1",
51
- "tap-spec": "5.x.x"
70
+ "tap-min": "git+https://github.com/Planeshifter/tap-min.git"
52
71
  },
53
72
  "engines": {
54
73
  "node": ">=0.10.0",
@@ -87,10 +106,14 @@
87
106
  "typed array",
88
107
  "typed",
89
108
  "array",
90
- "array-like"
109
+ "array-like",
110
+ "ndarray",
111
+ "tensor",
112
+ "matrix",
113
+ "vector"
91
114
  ],
92
115
  "funding": {
93
- "type": "patreon",
94
- "url": "https://www.patreon.com/athan"
116
+ "type": "opencollective",
117
+ "url": "https://opencollective.com/stdlib"
95
118
  }
96
119
  }