@stdlib/utils-map 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +481 -0
- package/NOTICE +1 -0
- package/README.md +386 -0
- package/docs/repl.txt +106 -0
- package/docs/types/index.d.ts +300 -0
- package/docs/types/test.ts +270 -0
- package/lib/array.js +89 -0
- package/lib/assign.js +128 -0
- package/lib/index.js +99 -0
- package/lib/main.js +104 -0
- package/lib/ndarray.js +197 -0
- package/package.json +118 -0
|
@@ -0,0 +1,300 @@
|
|
|
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
|
+
// TypeScript Version: 2.0
|
|
20
|
+
|
|
21
|
+
/// <reference types="@stdlib/types"/>
|
|
22
|
+
|
|
23
|
+
import { Collection } from '@stdlib/types/object';
|
|
24
|
+
import { ndarray } from '@stdlib/types/ndarray';
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Callback invoked for each array element.
|
|
28
|
+
*
|
|
29
|
+
* @returns result
|
|
30
|
+
*/
|
|
31
|
+
type Nullary = () => any;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Callback invoked for each array element.
|
|
35
|
+
*
|
|
36
|
+
* @param value - array element
|
|
37
|
+
* @returns result
|
|
38
|
+
*/
|
|
39
|
+
type Unary = ( value: any ) => any;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Callback invoked for each array element.
|
|
43
|
+
*
|
|
44
|
+
* @param value - array element
|
|
45
|
+
* @param index - element index
|
|
46
|
+
* @returns result
|
|
47
|
+
*/
|
|
48
|
+
type Binary = ( value: any, index: number ) => any;
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Callback invoked for each array element.
|
|
52
|
+
*
|
|
53
|
+
* @param value - array element
|
|
54
|
+
* @param index - element index
|
|
55
|
+
* @param arr - array
|
|
56
|
+
* @returns result
|
|
57
|
+
*/
|
|
58
|
+
type Ternary = ( value: any, index: number, arr: ndarray ) => any;
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Callback invoked for each array element.
|
|
62
|
+
*
|
|
63
|
+
* @param value - array element
|
|
64
|
+
* @param index - element index
|
|
65
|
+
* @param arr - array
|
|
66
|
+
* @returns result
|
|
67
|
+
*/
|
|
68
|
+
type ArrayTernary = ( value: any, index: number, arr: Collection ) => any;
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Callback invoked for each array element.
|
|
72
|
+
*
|
|
73
|
+
* @param value - array element
|
|
74
|
+
* @param index - element index
|
|
75
|
+
* @param arr - array
|
|
76
|
+
* @returns result
|
|
77
|
+
*/
|
|
78
|
+
type Callback = Nullary | Unary | Binary | Ternary;
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Callback invoked for each array element.
|
|
82
|
+
*
|
|
83
|
+
* @param value - array element
|
|
84
|
+
* @param index - element index
|
|
85
|
+
* @param arr - array
|
|
86
|
+
* @returns result
|
|
87
|
+
*/
|
|
88
|
+
type ArrayCallback = Nullary | Unary | Binary | ArrayTernary;
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Interface describing the main export.
|
|
92
|
+
*/
|
|
93
|
+
interface Routine {
|
|
94
|
+
// NOTE: signature order matters here, as an ndarray is an array-like object...
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Applies a function to each element in an array and assigns the result to an element in a new array.
|
|
98
|
+
*
|
|
99
|
+
* ## Notes
|
|
100
|
+
*
|
|
101
|
+
* - The applied function is provided the following arguments:
|
|
102
|
+
*
|
|
103
|
+
* - **value**: array element.
|
|
104
|
+
* - **index**: element index.
|
|
105
|
+
* - **arr**: input array.
|
|
106
|
+
*
|
|
107
|
+
* @param arr - input array
|
|
108
|
+
* @param fcn - function to apply
|
|
109
|
+
* @param thisArg - input function context
|
|
110
|
+
* @returns output array
|
|
111
|
+
*
|
|
112
|
+
* @example
|
|
113
|
+
* var naryFunction = require( `@stdlib/utils/nary-function` );
|
|
114
|
+
* var abs = require( `@stdlib/math/base/special/abs` );
|
|
115
|
+
* var array = require( `@stdlib/ndarray/array` );
|
|
116
|
+
*
|
|
117
|
+
* var opts = {
|
|
118
|
+
* 'dtype': 'generic'
|
|
119
|
+
* };
|
|
120
|
+
* var arr = array( [ [ -1, -2, -3 ], [ -4, -5, -6 ] ], opts );
|
|
121
|
+
*
|
|
122
|
+
* var out = map( arr, naryFunction( abs, 1 ) );
|
|
123
|
+
* // returns <ndarray>
|
|
124
|
+
*
|
|
125
|
+
* var data = out.data;
|
|
126
|
+
* // returns [ 1, 2, 3, 4, 5, 6 ]
|
|
127
|
+
*/
|
|
128
|
+
( arr: ndarray, fcn: Callback, thisArg?: any ): ndarray;
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Applies a function to each element in an array and assigns the result to an element in a new array.
|
|
132
|
+
*
|
|
133
|
+
* ## Notes
|
|
134
|
+
*
|
|
135
|
+
* - The applied function is provided the following arguments:
|
|
136
|
+
*
|
|
137
|
+
* - **value**: array element.
|
|
138
|
+
* - **index**: element index.
|
|
139
|
+
* - **arr**: input array.
|
|
140
|
+
*
|
|
141
|
+
* @param arr - input array
|
|
142
|
+
* @param fcn - function to apply
|
|
143
|
+
* @param thisArg - input function context
|
|
144
|
+
* @returns output array
|
|
145
|
+
*
|
|
146
|
+
* @example
|
|
147
|
+
* var naryFunction = require( `@stdlib/utils/nary-function` );
|
|
148
|
+
* var abs = require( `@stdlib/math/base/special/abs` );
|
|
149
|
+
*
|
|
150
|
+
* var arr = [ -1, -2, -3, -4, -5, -6 ];
|
|
151
|
+
*
|
|
152
|
+
* var out = map( arr, naryFunction( abs, 1 ) );
|
|
153
|
+
* // returns [ 1, 2, 3, 4, 5, 6 ]
|
|
154
|
+
*/
|
|
155
|
+
( arr: Collection, fcn: ArrayCallback, thisArg?: any ): Collection;
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Applies a function to each element in an array and assigns the result to an element in an output array.
|
|
159
|
+
*
|
|
160
|
+
* ## Notes
|
|
161
|
+
*
|
|
162
|
+
* - The applied function is provided the following arguments:
|
|
163
|
+
*
|
|
164
|
+
* - **value**: array element.
|
|
165
|
+
* - **index**: element index.
|
|
166
|
+
* - **arr**: input array.
|
|
167
|
+
*
|
|
168
|
+
* @param arr - input array
|
|
169
|
+
* @param out - output array
|
|
170
|
+
* @param fcn - function to apply
|
|
171
|
+
* @param thisArg - input function context
|
|
172
|
+
* @returns output array
|
|
173
|
+
*
|
|
174
|
+
* @example
|
|
175
|
+
* var naryFunction = require( `@stdlib/utils/nary-function` );
|
|
176
|
+
* var abs = require( `@stdlib/math/base/special/abs` );
|
|
177
|
+
* var array = require( `@stdlib/ndarray/array` );
|
|
178
|
+
*
|
|
179
|
+
* var opts = {
|
|
180
|
+
* 'dtype': 'generic',
|
|
181
|
+
* 'shape': [ 2, 3 ]
|
|
182
|
+
* };
|
|
183
|
+
* var arr = array( [ [ -1, -2, -3 ], [ -4, -5, -6 ] ], opts );
|
|
184
|
+
* var out = array( opts );
|
|
185
|
+
*
|
|
186
|
+
* map.assign( arr, out, naryFunction( abs, 1 ) );
|
|
187
|
+
*
|
|
188
|
+
* var data = out.data;
|
|
189
|
+
* // returns [ 1, 2, 3, 4, 5, 6 ]
|
|
190
|
+
*/
|
|
191
|
+
assign( arr: ndarray, out: ndarray, fcn: Callback, thisArg?: any ): ndarray;
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Applies a function to each element in an array and assigns the result to an element in an output array.
|
|
195
|
+
*
|
|
196
|
+
* ## Notes
|
|
197
|
+
*
|
|
198
|
+
* - The applied function is provided the following arguments:
|
|
199
|
+
*
|
|
200
|
+
* - **value**: array element.
|
|
201
|
+
* - **index**: element index.
|
|
202
|
+
* - **arr**: input array.
|
|
203
|
+
*
|
|
204
|
+
* @param arr - input array
|
|
205
|
+
* @param out - output array
|
|
206
|
+
* @param fcn - function to apply
|
|
207
|
+
* @param thisArg - input function context
|
|
208
|
+
* @returns output array
|
|
209
|
+
*
|
|
210
|
+
* @example
|
|
211
|
+
* var naryFunction = require( `@stdlib/utils/nary-function` );
|
|
212
|
+
* var abs = require( `@stdlib/math/base/special/abs` );
|
|
213
|
+
*
|
|
214
|
+
* var arr = [ -1, -2, -3, -4, -5, -6 ];
|
|
215
|
+
* var out = [ 0, 0, 0, 0, 0, 0 ];
|
|
216
|
+
*
|
|
217
|
+
* map.assign( arr, out, naryFunction( abs, 1 ) );
|
|
218
|
+
*
|
|
219
|
+
* console.log( out );
|
|
220
|
+
* // => [ 1, 2, 3, 4, 5, 6 ]
|
|
221
|
+
*/
|
|
222
|
+
assign( arr: Collection, out: Collection, fcn: ArrayCallback, thisArg?: any ): Collection; // tslint:disable-line:max-line-length
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Applies a function to each element in an array and assigns the result to an element in a new array.
|
|
227
|
+
*
|
|
228
|
+
* ## Notes
|
|
229
|
+
*
|
|
230
|
+
* - The applied function is provided the following arguments:
|
|
231
|
+
*
|
|
232
|
+
* - **value**: array element.
|
|
233
|
+
* - **index**: element index.
|
|
234
|
+
* - **arr**: input array.
|
|
235
|
+
*
|
|
236
|
+
* @param arr - input array
|
|
237
|
+
* @param fcn - function to apply
|
|
238
|
+
* @param thisArg - input function context
|
|
239
|
+
* @returns output array
|
|
240
|
+
*
|
|
241
|
+
* @example
|
|
242
|
+
* var naryFunction = require( `@stdlib/utils/nary-function` );
|
|
243
|
+
* var abs = require( `@stdlib/math/base/special/abs` );
|
|
244
|
+
*
|
|
245
|
+
* var arr = [ -1, -2, -3, -4, -5, -6 ];
|
|
246
|
+
*
|
|
247
|
+
* var out = map( arr, naryFunction( abs, 1 ) );
|
|
248
|
+
* // returns [ 1, 2, 3, 4, 5, 6 ]
|
|
249
|
+
*
|
|
250
|
+
* @example
|
|
251
|
+
* var naryFunction = require( `@stdlib/utils/nary-function` );
|
|
252
|
+
* var abs = require( `@stdlib/math/base/special/abs` );
|
|
253
|
+
* var array = require( `@stdlib/ndarray/array` );
|
|
254
|
+
*
|
|
255
|
+
* var opts = {
|
|
256
|
+
* 'dtype': 'generic'
|
|
257
|
+
* };
|
|
258
|
+
* var arr = array( [ [ -1, -2, -3 ], [ -4, -5, -6 ] ], opts );
|
|
259
|
+
*
|
|
260
|
+
* var out = map( arr, naryFunction( abs, 1 ) );
|
|
261
|
+
* // returns <ndarray>
|
|
262
|
+
*
|
|
263
|
+
* var data = out.data;
|
|
264
|
+
* // returns [ 1, 2, 3, 4, 5, 6 ]
|
|
265
|
+
*
|
|
266
|
+
* @example
|
|
267
|
+
* var naryFunction = require( `@stdlib/utils/nary-function` );
|
|
268
|
+
* var abs = require( `@stdlib/math/base/special/abs` );
|
|
269
|
+
*
|
|
270
|
+
* var arr = [ -1, -2, -3, -4, -5, -6 ];
|
|
271
|
+
* var out = [ 0, 0, 0, 0, 0, 0 ];
|
|
272
|
+
*
|
|
273
|
+
* map.assign( arr, out, naryFunction( abs, 1 ) );
|
|
274
|
+
*
|
|
275
|
+
* console.log( out );
|
|
276
|
+
* // => [ 1, 2, 3, 4, 5, 6 ]
|
|
277
|
+
*
|
|
278
|
+
* @example
|
|
279
|
+
* var naryFunction = require( `@stdlib/utils/nary-function` );
|
|
280
|
+
* var abs = require( `@stdlib/math/base/special/abs` );
|
|
281
|
+
* var array = require( `@stdlib/ndarray/array` );
|
|
282
|
+
*
|
|
283
|
+
* var opts = {
|
|
284
|
+
* 'dtype': 'generic',
|
|
285
|
+
* 'shape': [ 2, 3 ]
|
|
286
|
+
* };
|
|
287
|
+
* var arr = array( [ [ -1, -2, -3 ], [ -4, -5, -6 ] ], opts );
|
|
288
|
+
* var out = array( opts );
|
|
289
|
+
*
|
|
290
|
+
* map.assign( arr, out, naryFunction( abs, 1 ) );
|
|
291
|
+
*
|
|
292
|
+
* var data = out.data;
|
|
293
|
+
* // returns [ 1, 2, 3, 4, 5, 6 ]
|
|
294
|
+
*/
|
|
295
|
+
declare var map: Routine;
|
|
296
|
+
|
|
297
|
+
|
|
298
|
+
// EXPORTS //
|
|
299
|
+
|
|
300
|
+
export = map;
|
|
@@ -0,0 +1,270 @@
|
|
|
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
|
+
import array = require( '@stdlib/ndarray-array' );
|
|
20
|
+
import map = require( './index' );
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Callback function.
|
|
24
|
+
*
|
|
25
|
+
* @param v - argument
|
|
26
|
+
* @returns result
|
|
27
|
+
*/
|
|
28
|
+
function clbk( v: any ): any {
|
|
29
|
+
return v;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
// TESTS //
|
|
34
|
+
|
|
35
|
+
// The function returns a collection when provided a collection...
|
|
36
|
+
{
|
|
37
|
+
const arr = [ 1, 2, 3, 4, 5, 6 ];
|
|
38
|
+
|
|
39
|
+
map( arr, clbk ); // $ExpectType Collection
|
|
40
|
+
map( arr, clbk, {} ); // $ExpectType Collection
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// The function returns an ndarray when provided an ndarray...
|
|
44
|
+
{
|
|
45
|
+
const arr = array( [ 1, 2, 3, 4, 5, 6 ] );
|
|
46
|
+
|
|
47
|
+
map( arr, clbk ); // $ExpectType ndarray
|
|
48
|
+
map( arr, clbk, {} ); // $ExpectType ndarray
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// The compiler throws an error if the function is provided a first argument other than a collection or ndarray...
|
|
52
|
+
{
|
|
53
|
+
map( 5, clbk ); // $ExpectError
|
|
54
|
+
map( true, clbk ); // $ExpectError
|
|
55
|
+
map( false, clbk ); // $ExpectError
|
|
56
|
+
map( null, clbk, {} ); // $ExpectError
|
|
57
|
+
map( {}, clbk ); // $ExpectError
|
|
58
|
+
|
|
59
|
+
map( 5, clbk, {} ); // $ExpectError
|
|
60
|
+
map( true, clbk, {} ); // $ExpectError
|
|
61
|
+
map( false, clbk, {} ); // $ExpectError
|
|
62
|
+
map( null, clbk, {} ); // $ExpectError
|
|
63
|
+
map( {}, clbk, {} ); // $ExpectError
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// The compiler throws an error if the function is provided a second argument other than a function with a supported signature...
|
|
67
|
+
{
|
|
68
|
+
const arr1 = [ 1, 2, 3, 4, 5, 6 ];
|
|
69
|
+
|
|
70
|
+
map( arr1, '5' ); // $ExpectError
|
|
71
|
+
map( arr1, true ); // $ExpectError
|
|
72
|
+
map( arr1, false ); // $ExpectError
|
|
73
|
+
map( arr1, 123 ); // $ExpectError
|
|
74
|
+
map( arr1, null ); // $ExpectError
|
|
75
|
+
map( arr1, {} ); // $ExpectError
|
|
76
|
+
map( arr1, [] ); // $ExpectError
|
|
77
|
+
|
|
78
|
+
map( arr1, '5', {} ); // $ExpectError
|
|
79
|
+
map( arr1, true, {} ); // $ExpectError
|
|
80
|
+
map( arr1, false, {} ); // $ExpectError
|
|
81
|
+
map( arr1, 123, {} ); // $ExpectError
|
|
82
|
+
map( arr1, null, {} ); // $ExpectError
|
|
83
|
+
map( arr1, {}, {} ); // $ExpectError
|
|
84
|
+
map( arr1, [], {} ); // $ExpectError
|
|
85
|
+
|
|
86
|
+
map( arr1, ( x: number, y: number, z: number ): number => x + y + z ); // $ExpectError
|
|
87
|
+
map( arr1, ( x: number, y: number, z: number ): number => x + y + z, {} ); // $ExpectError
|
|
88
|
+
|
|
89
|
+
const arr2 = array( [ 1, 2, 3, 4, 5, 6 ] );
|
|
90
|
+
|
|
91
|
+
map( arr2, '5' ); // $ExpectError
|
|
92
|
+
map( arr2, true ); // $ExpectError
|
|
93
|
+
map( arr2, false ); // $ExpectError
|
|
94
|
+
map( arr2, 123 ); // $ExpectError
|
|
95
|
+
map( arr2, null ); // $ExpectError
|
|
96
|
+
map( arr2, {} ); // $ExpectError
|
|
97
|
+
map( arr2, [] ); // $ExpectError
|
|
98
|
+
|
|
99
|
+
map( arr2, '5', {} ); // $ExpectError
|
|
100
|
+
map( arr2, true, {} ); // $ExpectError
|
|
101
|
+
map( arr2, false, {} ); // $ExpectError
|
|
102
|
+
map( arr2, 123, {} ); // $ExpectError
|
|
103
|
+
map( arr2, null, {} ); // $ExpectError
|
|
104
|
+
map( arr2, {}, {} ); // $ExpectError
|
|
105
|
+
map( arr2, [], {} ); // $ExpectError
|
|
106
|
+
|
|
107
|
+
map( arr2, ( x: number, y: number, z: number ): number => x + y + z ); // $ExpectError
|
|
108
|
+
map( arr2, ( x: number, y: number, z: number ): number => x + y + z, {} ); // $ExpectError
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// The compiler throws an error if the function is provided an unsupported number of arguments...
|
|
112
|
+
{
|
|
113
|
+
const arr1 = [ 1, 2, 3, 4, 5, 6 ];
|
|
114
|
+
|
|
115
|
+
map(); // $ExpectError
|
|
116
|
+
map( arr1 ); // $ExpectError
|
|
117
|
+
map( arr1, clbk, {}, 4 ); // $ExpectError
|
|
118
|
+
|
|
119
|
+
const arr2 = array( [ 1, 2, 3, 4, 5, 6 ] );
|
|
120
|
+
|
|
121
|
+
map(); // $ExpectError
|
|
122
|
+
map( arr2 ); // $ExpectError
|
|
123
|
+
map( arr2, clbk, {}, 4 ); // $ExpectError
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// Attached to the main export is an `assign` method which returns a collection when provided a collection...
|
|
127
|
+
{
|
|
128
|
+
const arr = [ 1, 2, 3, 4, 5, 6 ];
|
|
129
|
+
const out = [ 0, 0, 0, 0, 0, 0 ];
|
|
130
|
+
|
|
131
|
+
map.assign( arr, out, clbk ); // $ExpectType Collection
|
|
132
|
+
map.assign( arr, out, clbk, {} ); // $ExpectType Collection
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// The `assign` method returns an ndarray when provided an ndarray...
|
|
136
|
+
{
|
|
137
|
+
const arr = array( [ 1, 2, 3, 4, 5, 6 ] );
|
|
138
|
+
const out = array( [ 0, 0, 0, 0, 0, 0 ] );
|
|
139
|
+
|
|
140
|
+
map.assign( arr, out, clbk ); // $ExpectType ndarray
|
|
141
|
+
map.assign( arr, out, clbk, {} ); // $ExpectType ndarray
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// The compiler throws an error if the `assign` method is provided a first argument other than a collection or ndarray...
|
|
145
|
+
{
|
|
146
|
+
const out1 = [ 0, 0, 0, 0, 0, 0 ];
|
|
147
|
+
|
|
148
|
+
map.assign( 5, out1, clbk ); // $ExpectError
|
|
149
|
+
map.assign( true, out1, clbk ); // $ExpectError
|
|
150
|
+
map.assign( false, out1, clbk ); // $ExpectError
|
|
151
|
+
map.assign( null, out1, clbk, {} ); // $ExpectError
|
|
152
|
+
map.assign( {}, out1, clbk ); // $ExpectError
|
|
153
|
+
|
|
154
|
+
map.assign( 5, out1, clbk, {} ); // $ExpectError
|
|
155
|
+
map.assign( true, out1, clbk, {} ); // $ExpectError
|
|
156
|
+
map.assign( false, out1, clbk, {} ); // $ExpectError
|
|
157
|
+
map.assign( null, out1, clbk, {} ); // $ExpectError
|
|
158
|
+
map.assign( {}, out1, clbk, {} ); // $ExpectError
|
|
159
|
+
|
|
160
|
+
const out2 = array( [ 0, 0, 0, 0, 0, 0 ] );
|
|
161
|
+
|
|
162
|
+
map.assign( 5, out2, clbk ); // $ExpectError
|
|
163
|
+
map.assign( true, out2, clbk ); // $ExpectError
|
|
164
|
+
map.assign( false, out2, clbk ); // $ExpectError
|
|
165
|
+
map.assign( null, out2, clbk, {} ); // $ExpectError
|
|
166
|
+
map.assign( {}, out2, clbk ); // $ExpectError
|
|
167
|
+
|
|
168
|
+
map.assign( 5, out2, clbk, {} ); // $ExpectError
|
|
169
|
+
map.assign( true, out2, clbk, {} ); // $ExpectError
|
|
170
|
+
map.assign( false, out2, clbk, {} ); // $ExpectError
|
|
171
|
+
map.assign( null, out2, clbk, {} ); // $ExpectError
|
|
172
|
+
map.assign( {}, out2, clbk, {} ); // $ExpectError
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// The compiler throws an error if the `assign` method is provided a second argument other than a collection or ndarray...
|
|
176
|
+
{
|
|
177
|
+
const arr1 = [ 0, 0, 0, 0, 0, 0 ];
|
|
178
|
+
|
|
179
|
+
map.assign( arr1, 5, clbk ); // $ExpectError
|
|
180
|
+
map.assign( arr1, true, clbk ); // $ExpectError
|
|
181
|
+
map.assign( arr1, false, clbk ); // $ExpectError
|
|
182
|
+
map.assign( arr1, null, clbk, {} ); // $ExpectError
|
|
183
|
+
map.assign( arr1, {}, clbk ); // $ExpectError
|
|
184
|
+
|
|
185
|
+
map.assign( arr1, 5, clbk, {} ); // $ExpectError
|
|
186
|
+
map.assign( arr1, true, clbk, {} ); // $ExpectError
|
|
187
|
+
map.assign( arr1, false, clbk, {} ); // $ExpectError
|
|
188
|
+
map.assign( arr1, null, clbk, {} ); // $ExpectError
|
|
189
|
+
map.assign( arr1, {}, clbk, {} ); // $ExpectError
|
|
190
|
+
|
|
191
|
+
const arr2 = array( [ 0, 0, 0, 0, 0, 0 ] );
|
|
192
|
+
|
|
193
|
+
map.assign( arr2, 5, clbk ); // $ExpectError
|
|
194
|
+
map.assign( arr2, true, clbk ); // $ExpectError
|
|
195
|
+
map.assign( arr2, false, clbk ); // $ExpectError
|
|
196
|
+
map.assign( arr2, null, clbk, {} ); // $ExpectError
|
|
197
|
+
map.assign( arr2, {}, clbk ); // $ExpectError
|
|
198
|
+
|
|
199
|
+
map.assign( arr2, 5, clbk, {} ); // $ExpectError
|
|
200
|
+
map.assign( arr2, true, clbk, {} ); // $ExpectError
|
|
201
|
+
map.assign( arr2, false, clbk, {} ); // $ExpectError
|
|
202
|
+
map.assign( arr2, null, clbk, {} ); // $ExpectError
|
|
203
|
+
map.assign( arr2, {}, clbk, {} ); // $ExpectError
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// The compiler throws an error if the `assign` method is provided a third argument other than a function with a supported signature...
|
|
207
|
+
{
|
|
208
|
+
const arr1 = [ 1, 2, 3, 4, 5, 6 ];
|
|
209
|
+
const out1 = [ 0, 0, 0, 0, 0, 0 ];
|
|
210
|
+
|
|
211
|
+
map.assign( arr1, out1, '5' ); // $ExpectError
|
|
212
|
+
map.assign( arr1, out1, true ); // $ExpectError
|
|
213
|
+
map.assign( arr1, out1, false ); // $ExpectError
|
|
214
|
+
map.assign( arr1, out1, 123 ); // $ExpectError
|
|
215
|
+
map.assign( arr1, out1, null ); // $ExpectError
|
|
216
|
+
map.assign( arr1, out1, {} ); // $ExpectError
|
|
217
|
+
map.assign( arr1, out1, [] ); // $ExpectError
|
|
218
|
+
|
|
219
|
+
map.assign( arr1, out1, '5', {} ); // $ExpectError
|
|
220
|
+
map.assign( arr1, out1, true, {} ); // $ExpectError
|
|
221
|
+
map.assign( arr1, out1, false, {} ); // $ExpectError
|
|
222
|
+
map.assign( arr1, out1, 123, {} ); // $ExpectError
|
|
223
|
+
map.assign( arr1, out1, null, {} ); // $ExpectError
|
|
224
|
+
map.assign( arr1, out1, {}, {} ); // $ExpectError
|
|
225
|
+
map.assign( arr1, out1, [], {} ); // $ExpectError
|
|
226
|
+
|
|
227
|
+
map.assign( arr1, out1, ( x: number, y: number, z: number ): number => x + y + z ); // $ExpectError
|
|
228
|
+
map.assign( arr1, out1, ( x: number, y: number, z: number ): number => x + y + z, {} ); // $ExpectError
|
|
229
|
+
|
|
230
|
+
const arr2 = array( [ 1, 2, 3, 4, 5, 6 ] );
|
|
231
|
+
const out2 = array( [ 0, 0, 0, 0, 0, 0 ] );
|
|
232
|
+
|
|
233
|
+
map.assign( arr2, out2, '5' ); // $ExpectError
|
|
234
|
+
map.assign( arr2, out2, true ); // $ExpectError
|
|
235
|
+
map.assign( arr2, out2, false ); // $ExpectError
|
|
236
|
+
map.assign( arr2, out2, 123 ); // $ExpectError
|
|
237
|
+
map.assign( arr2, out2, null ); // $ExpectError
|
|
238
|
+
map.assign( arr2, out2, {} ); // $ExpectError
|
|
239
|
+
map.assign( arr2, out2, [] ); // $ExpectError
|
|
240
|
+
|
|
241
|
+
map.assign( arr2, out2, '5', {} ); // $ExpectError
|
|
242
|
+
map.assign( arr2, out2, true, {} ); // $ExpectError
|
|
243
|
+
map.assign( arr2, out2, false, {} ); // $ExpectError
|
|
244
|
+
map.assign( arr2, out2, 123, {} ); // $ExpectError
|
|
245
|
+
map.assign( arr2, out2, null, {} ); // $ExpectError
|
|
246
|
+
map.assign( arr2, out2, {}, {} ); // $ExpectError
|
|
247
|
+
map.assign( arr2, out2, [], {} ); // $ExpectError
|
|
248
|
+
|
|
249
|
+
map.assign( arr2, out2, ( x: number, y: number, z: number ): number => x + y + z ); // $ExpectError
|
|
250
|
+
map.assign( arr2, out2, ( x: number, y: number, z: number ): number => x + y + z, {} ); // $ExpectError
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
// The compiler throws an error if the `assign` method is provided an unsupported number of arguments...
|
|
254
|
+
{
|
|
255
|
+
const arr1 = [ 1, 2, 3, 4, 5, 6 ];
|
|
256
|
+
const out1 = [ 0, 0, 0, 0, 0, 0 ];
|
|
257
|
+
|
|
258
|
+
map.assign(); // $ExpectError
|
|
259
|
+
map.assign( arr1 ); // $ExpectError
|
|
260
|
+
map.assign( arr1, out1 ); // $ExpectError
|
|
261
|
+
map.assign( arr1, out1, clbk, {}, 4 ); // $ExpectError
|
|
262
|
+
|
|
263
|
+
const arr2 = array( [ 1, 2, 3, 4, 5, 6 ] );
|
|
264
|
+
const out2 = array( [ 0, 0, 0, 0, 0, 0 ] );
|
|
265
|
+
|
|
266
|
+
map.assign(); // $ExpectError
|
|
267
|
+
map.assign( arr2 ); // $ExpectError
|
|
268
|
+
map.assign( arr2, out2 ); // $ExpectError
|
|
269
|
+
map.assign( arr2, out2, clbk, {}, 4 ); // $ExpectError
|
|
270
|
+
}
|
package/lib/array.js
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
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 to each element in an array and assigns the result to an element in an output array.
|
|
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 {Object} y - object containing output array data
|
|
31
|
+
* @param {ArrayLikeObject} y.data - output array data
|
|
32
|
+
* @param {Function} y.setter - callback for setting output array data elements
|
|
33
|
+
* @param {Function} fcn - function to apply
|
|
34
|
+
* @param {*} thisArg - function execution context
|
|
35
|
+
* @returns {void}
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* var naryFunction = require( '@stdlib/utils-nary-function' );
|
|
39
|
+
* var abs = require( '@stdlib/math-base-special-abs' );
|
|
40
|
+
*
|
|
41
|
+
* // Define getters and setters:
|
|
42
|
+
* function getter( buf, idx ) {
|
|
43
|
+
* return buf[ idx ];
|
|
44
|
+
* }
|
|
45
|
+
*
|
|
46
|
+
* function setter( buf, idx, value ) {
|
|
47
|
+
* buf[ idx ] = value;
|
|
48
|
+
* }
|
|
49
|
+
*
|
|
50
|
+
* // Create the input and output array objects:
|
|
51
|
+
* var x = {
|
|
52
|
+
* 'data': [ -1, -2, -3, -4, -5, -6 ],
|
|
53
|
+
* 'getter': getter
|
|
54
|
+
* };
|
|
55
|
+
* var y = {
|
|
56
|
+
* 'data': [ 0, 0, 0, 0, 0, 0 ],
|
|
57
|
+
* 'setter': setter
|
|
58
|
+
* };
|
|
59
|
+
*
|
|
60
|
+
* map( x, y, naryFunction( abs, 1 ) );
|
|
61
|
+
*
|
|
62
|
+
* var data = y.data;
|
|
63
|
+
* // returns [ 1, 2, 3, 4, 5, 6 ]
|
|
64
|
+
*/
|
|
65
|
+
function map( x, y, fcn, thisArg ) {
|
|
66
|
+
var xbuf;
|
|
67
|
+
var ybuf;
|
|
68
|
+
var get;
|
|
69
|
+
var set;
|
|
70
|
+
var i;
|
|
71
|
+
|
|
72
|
+
// Cache references to the input and output data:
|
|
73
|
+
xbuf = x.data;
|
|
74
|
+
ybuf = y.data;
|
|
75
|
+
|
|
76
|
+
// Cache accessors:
|
|
77
|
+
get = x.getter;
|
|
78
|
+
set = y.setter;
|
|
79
|
+
|
|
80
|
+
// Iterate over each element...
|
|
81
|
+
for ( i = 0; i < xbuf.length; i++ ) {
|
|
82
|
+
set( ybuf, i, fcn.call( thisArg, get( xbuf, i ), i, xbuf ) );
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
// EXPORTS //
|
|
88
|
+
|
|
89
|
+
module.exports = map;
|