@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/README.md ADDED
@@ -0,0 +1,386 @@
1
+ <!--
2
+
3
+ @license Apache-2.0
4
+
5
+ Copyright (c) 2021 The Stdlib Authors.
6
+
7
+ Licensed under the Apache License, Version 2.0 (the "License");
8
+ you may not use this file except in compliance with the License.
9
+ You may obtain a copy of the License at
10
+
11
+ http://www.apache.org/licenses/LICENSE-2.0
12
+
13
+ Unless required by applicable law or agreed to in writing, software
14
+ distributed under the License is distributed on an "AS IS" BASIS,
15
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ See the License for the specific language governing permissions and
17
+ limitations under the License.
18
+
19
+ -->
20
+
21
+ # map
22
+
23
+ [![NPM version][npm-image]][npm-url] [![Build Status][test-image]][test-url] [![Coverage Status][coverage-image]][coverage-url] <!-- [![dependencies][dependencies-image]][dependencies-url] -->
24
+
25
+ > Apply a function to each element in an array and assign the result to an element in an output array.
26
+
27
+ <!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
28
+
29
+ <section class="intro">
30
+
31
+ </section>
32
+
33
+ <!-- /.intro -->
34
+
35
+ <!-- Package usage documentation. -->
36
+
37
+ <section class="installation">
38
+
39
+ ## Installation
40
+
41
+ ```bash
42
+ npm install @stdlib/utils-map
43
+ ```
44
+
45
+ </section>
46
+
47
+ <section class="usage">
48
+
49
+ ## Usage
50
+
51
+ ```javascript
52
+ var map = require( '@stdlib/utils-map' );
53
+ ```
54
+
55
+ <a name="fcn-map"></a>
56
+
57
+ #### map( arr, fcn\[, thisArg] )
58
+
59
+ Applies a function to each element in an array and assigns the result to an element in a new array.
60
+
61
+ ```javascript
62
+ var naryFunction = require( '@stdlib/utils-nary-function' );
63
+ var abs = require( '@stdlib/math-base-special-abs' );
64
+
65
+ var arr = [ -1, -2, -3, -4, -5, -6 ];
66
+
67
+ var out = map( arr, naryFunction( abs, 1 ) );
68
+ // returns [ 1, 2, 3, 4, 5, 6 ]
69
+ ```
70
+
71
+ The function accepts both array-like objects and [`ndarray`][@stdlib/ndarray/ctor]-like objects.
72
+
73
+ ```javascript
74
+ var naryFunction = require( '@stdlib/utils-nary-function' );
75
+ var abs = require( '@stdlib/math-base-special-abs' );
76
+ var array = require( '@stdlib/ndarray-array' );
77
+
78
+ var opts = {
79
+ 'dtype': 'generic'
80
+ };
81
+ var arr = array( [ [ -1, -2, -3 ], [ -4, -5, -6 ] ], opts );
82
+
83
+ var out = map( arr, naryFunction( abs, 1 ) );
84
+ // returns <ndarray>
85
+
86
+ var v = out.get( 1, 1 );
87
+ // returns 5
88
+ ```
89
+
90
+ The applied function is provided the following arguments:
91
+
92
+ - **value**: array element.
93
+ - **index**: element index.
94
+ - **arr**: input array.
95
+
96
+ To set the `this` context when invoking the input function, provide a `thisArg`.
97
+
98
+ <!-- eslint-disable no-invalid-this -->
99
+
100
+ ```javascript
101
+ var abs = require( '@stdlib/math-base-special-abs' );
102
+
103
+ function fcn( v ) {
104
+ this.count += 1;
105
+ return abs( v );
106
+ }
107
+
108
+ var arr = [ -1, -2, -3, -4, -5, -6 ];
109
+
110
+ var ctx = {
111
+ 'count': 0
112
+ };
113
+
114
+ var out = map( arr, fcn, ctx );
115
+ // returns [ 1, 2, 3, 4, 5, 6 ]
116
+
117
+ var cnt = ctx.count;
118
+ // returns 6
119
+ ```
120
+
121
+ <a name="method-map-assign"></a>
122
+
123
+ #### map.assign( arr, out, fcn\[, thisArg] )
124
+
125
+ Applies a function to each element in an array and assigns the result to an element in an output array.
126
+
127
+ ```javascript
128
+ var naryFunction = require( '@stdlib/utils-nary-function' );
129
+ var abs = require( '@stdlib/math-base-special-abs' );
130
+
131
+ var arr = [ -1, -2, -3, -4, -5, -6 ];
132
+ var out = [ 0, 0, 0, 0, 0, 0 ];
133
+
134
+ map.assign( arr, out, naryFunction( abs, 1 ) );
135
+
136
+ console.log( out );
137
+ // => [ 1, 2, 3, 4, 5, 6 ]
138
+ ```
139
+
140
+ The method accepts both array-like objects and [`ndarray`][@stdlib/ndarray/ctor]-like objects.
141
+
142
+ ```javascript
143
+ var naryFunction = require( '@stdlib/utils-nary-function' );
144
+ var abs = require( '@stdlib/math-base-special-abs' );
145
+ var array = require( '@stdlib/ndarray-array' );
146
+
147
+ var opts = {
148
+ 'dtype': 'generic',
149
+ 'shape': [ 2, 3 ]
150
+ };
151
+ var arr = array( [ [ -1, -2, -3 ], [ -4, -5, -6 ] ], opts );
152
+ var out = array( opts );
153
+
154
+ map.assign( arr, out, naryFunction( abs, 1 ) );
155
+
156
+ var v = out.get( 1, 1 );
157
+ // returns 5
158
+ ```
159
+
160
+ Input and output arrays must be either both array-like objects or both [`ndarray`][@stdlib/ndarray/ctor]-like objects. If input and output arrays are both array-like objects, both arrays **must** have the same number of elements.
161
+
162
+ If input and output arrays are both [`ndarray`][@stdlib/ndarray/ctor]-like objects, the arrays **must** be [broadcast compatible][@stdlib/ndarray/base/broadcast-shapes]. To map from an input [`ndarray`][@stdlib/ndarray/ctor] to an output [`ndarray`][@stdlib/ndarray/ctor] which has the same rank (i.e., dimensionality) and the same number of elements, but which is not [broadcast compatible][@stdlib/ndarray/base/broadcast-shapes], reshape the arrays prior to invocation.
163
+
164
+ ```javascript
165
+ var naryFunction = require( '@stdlib/utils-nary-function' );
166
+ var abs = require( '@stdlib/math-base-special-abs' );
167
+ var array = require( '@stdlib/ndarray-array' );
168
+
169
+ var opts = {
170
+ 'dtype': 'generic',
171
+ 'shape': [ 2, 3 ]
172
+ };
173
+ var arr = array( [ [ -1, -2, -3 ], [ -4, -5, -6 ] ], opts );
174
+
175
+ opts = {
176
+ 'dtype': 'generic',
177
+ 'shape': [ 2, 2, 3 ] // broadcast compatible shape
178
+ };
179
+ var out = array( opts );
180
+
181
+ map.assign( arr, out, naryFunction( abs, 1 ) );
182
+
183
+ var v = out.get( 0, 1, 1 );
184
+ // returns 5
185
+
186
+ v = out.get( 1, 1, 1 );
187
+ // returns 5
188
+ ```
189
+
190
+ In general, avoid providing output [`ndarray`][@stdlib/ndarray/ctor]-like objects which are [non-contiguous][@stdlib/ndarray/base/assert/is-contiguous] views containing one or more elements referring to the **same** memory location. Writing to an overlapping [non-contiguous][@stdlib/ndarray/base/assert/is-contiguous] view is likely to simultaneously affect multiple elements and yield unexpected results.
191
+
192
+ The applied function is provided the same arguments as with [`map`](#fcn-map).
193
+
194
+ </section>
195
+
196
+ <!-- /.usage -->
197
+
198
+ <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
199
+
200
+ <section class="notes">
201
+
202
+ ## Notes
203
+
204
+ - The [`map`](#fcn-map) function **always** returns an output value having a "generic" data type. For example, if provided an array-like object, the function returns a generic `array`. If provided an [`ndarray`][@stdlib/ndarray/ctor], the function returns an [`ndarray`][@stdlib/ndarray/ctor] having a "generic" data type.
205
+
206
+ Accordingly, in contrast to [`TypedArray.prototype.map()`][mdn-typedarray-map], when provided a typed array, the [`map`](#fcn-map) function does **not** return a typed array of the same type. To assign results to a typed array, use the [`map.assign`](#method-map-assign) method.
207
+
208
+ - Both [`map`](#fcn-map) and [`map.assign`](#method-map-assign) accept array-like objects exposing getters and setters for array element access (e.g., [`Complex64Array`][@stdlib/array/complex64], [`Complex128Array`][@stdlib/array/complex128], etc).
209
+
210
+ ```javascript
211
+ var Complex64Array = require( '@stdlib/array-complex64' );
212
+ var Complex64 = require( '@stdlib/complex-float32' );
213
+ var realf = require( '@stdlib/complex-realf' );
214
+ var imagf = require( '@stdlib/complex-imagf' );
215
+
216
+ function scale( z ) {
217
+ return new Complex64( realf(z)*10.0, imagf(z)*10.0 );
218
+ }
219
+
220
+ var x = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] );
221
+ var y = new Complex64Array( 4 );
222
+
223
+ map.assign( x, y, scale );
224
+
225
+ var v = y.get( 0 );
226
+
227
+ var re = realf( v );
228
+ // returns 10.0
229
+
230
+ var im = imagf( v );
231
+ // returns 20.0
232
+ ```
233
+
234
+ - 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. For non-contiguous arrays, see [`@stdlib/ndarray/base/unary`][@stdlib/ndarray/base/unary].
235
+
236
+ - Both [`map`](#fcn-map) and [`map.assign`](#method-map-assign) do **not** skip `undefined` elements.
237
+
238
+ </section>
239
+
240
+ <!-- /.notes -->
241
+
242
+ <!-- Package usage examples. -->
243
+
244
+ <section class="examples">
245
+
246
+ ## Examples
247
+
248
+ <!-- eslint no-undef: "error" -->
249
+
250
+ ```javascript
251
+ var filledarrayBy = require( '@stdlib/array-filled-by' );
252
+ var discreteUniform = require( '@stdlib/random-base-discrete-uniform' ).factory;
253
+ var naryFunction = require( '@stdlib/utils-nary-function' );
254
+ var abs2 = require( '@stdlib/math-base-special-abs2' );
255
+ var array = require( '@stdlib/ndarray-array' );
256
+ var map = require( '@stdlib/utils-map' );
257
+
258
+ function fill( i ) {
259
+ var rand = discreteUniform( -10*(i+1), 10*(i+1) );
260
+ return filledarrayBy( 10, 'generic', rand );
261
+ }
262
+
263
+ // Create a two-dimensional ndarray (i.e., a matrix):
264
+ var x = array( filledarrayBy( 10, 'generic', fill ), {
265
+ 'dtype': 'generic',
266
+ 'flatten': true
267
+ });
268
+
269
+ // Create an explicit unary function:
270
+ var f = naryFunction( abs2, 1 );
271
+
272
+ // Compute the element-wise squared absolute value...
273
+ var y = map( x, f );
274
+
275
+ console.log( 'x:' );
276
+ console.log( x.data );
277
+
278
+ console.log( 'y:' );
279
+ console.log( y.data );
280
+ ```
281
+
282
+ </section>
283
+
284
+ <!-- /.examples -->
285
+
286
+ <!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
287
+
288
+ <section class="references">
289
+
290
+ </section>
291
+
292
+ <!-- /.references -->
293
+
294
+ <!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
295
+
296
+ <section class="related">
297
+
298
+ </section>
299
+
300
+ <!-- /.related -->
301
+
302
+ <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
303
+
304
+
305
+ <section class="main-repo" >
306
+
307
+ * * *
308
+
309
+ ## Notice
310
+
311
+ This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more.
312
+
313
+ For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib].
314
+
315
+ #### Community
316
+
317
+ [![Chat][chat-image]][chat-url]
318
+
319
+ ---
320
+
321
+ ## License
322
+
323
+ See [LICENSE][stdlib-license].
324
+
325
+
326
+ ## Copyright
327
+
328
+ Copyright &copy; 2016-2022. The Stdlib [Authors][stdlib-authors].
329
+
330
+ </section>
331
+
332
+ <!-- /.stdlib -->
333
+
334
+ <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
335
+
336
+ <section class="links">
337
+
338
+ [npm-image]: http://img.shields.io/npm/v/@stdlib/utils-map.svg
339
+ [npm-url]: https://npmjs.org/package/@stdlib/utils-map
340
+
341
+ [test-image]: https://github.com/stdlib-js/utils-map/actions/workflows/test.yml/badge.svg
342
+ [test-url]: https://github.com/stdlib-js/utils-map/actions/workflows/test.yml
343
+
344
+ [coverage-image]: https://img.shields.io/codecov/c/github/stdlib-js/utils-map/main.svg
345
+ [coverage-url]: https://codecov.io/github/stdlib-js/utils-map?branch=main
346
+
347
+ <!--
348
+
349
+ [dependencies-image]: https://img.shields.io/david/stdlib-js/utils-map.svg
350
+ [dependencies-url]: https://david-dm.org/stdlib-js/utils-map/main
351
+
352
+ -->
353
+
354
+ [umd]: https://github.com/umdjs/umd
355
+ [es-module]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules
356
+
357
+ [deno-url]: https://github.com/stdlib-js/utils-map/tree/deno
358
+ [umd-url]: https://github.com/stdlib-js/utils-map/tree/umd
359
+ [esm-url]: https://github.com/stdlib-js/utils-map/tree/esm
360
+
361
+ [chat-image]: https://img.shields.io/gitter/room/stdlib-js/stdlib.svg
362
+ [chat-url]: https://gitter.im/stdlib-js/stdlib/
363
+
364
+ [stdlib]: https://github.com/stdlib-js/stdlib
365
+
366
+ [stdlib-authors]: https://github.com/stdlib-js/stdlib/graphs/contributors
367
+
368
+ [stdlib-license]: https://raw.githubusercontent.com/stdlib-js/utils-map/main/LICENSE
369
+
370
+ [@stdlib/ndarray/ctor]: https://www.npmjs.com/package/@stdlib/stdlib
371
+
372
+ [@stdlib/ndarray/base/unary]: https://www.npmjs.com/package/@stdlib/stdlib
373
+
374
+ [@stdlib/ndarray/base/broadcast-shapes]: https://www.npmjs.com/package/@stdlib/stdlib
375
+
376
+ [@stdlib/ndarray/base/assert/is-contiguous]: https://www.npmjs.com/package/@stdlib/stdlib
377
+
378
+ [@stdlib/array/complex64]: https://www.npmjs.com/package/@stdlib/stdlib
379
+
380
+ [@stdlib/array/complex128]: https://www.npmjs.com/package/@stdlib/stdlib
381
+
382
+ [mdn-typedarray-map]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/map
383
+
384
+ </section>
385
+
386
+ <!-- /.links -->
package/docs/repl.txt ADDED
@@ -0,0 +1,106 @@
1
+
2
+ {{alias}}( arr, fcn[, thisArg] )
3
+ Applies a function to each element in an array and assigns the result to an
4
+ element in a new array.
5
+
6
+ The applied function is provided the following arguments:
7
+
8
+ - value: array element.
9
+ - index: element index.
10
+ - arr: input array.
11
+
12
+ The returned output array always has a "generic" data type. For example, if
13
+ provided an array-like object, the function returns a generic array. If
14
+ provided an ndarray, the function returns an ndarray having a "generic" data
15
+ type.
16
+
17
+ Parameters
18
+ ----------
19
+ arr: ArrayLikeObject|ndarray
20
+ Input array.
21
+
22
+ fcn: Function
23
+ Function to apply.
24
+
25
+ thisArg: any (optional)
26
+ Input function context.
27
+
28
+ Returns
29
+ -------
30
+ out: Array|ndarray
31
+ Output array.
32
+
33
+ Examples
34
+ --------
35
+ // array-like object:
36
+ > var f = {{alias:@stdlib/utils/nary-function}}( {{alias:@stdlib/math/base/special/abs}}, 1 );
37
+ > var arr = [ -1, -2, -3, -4, -5, -6 ];
38
+ > var out = {{alias}}( arr, f )
39
+ [ 1, 2, 3, 4, 5, 6 ]
40
+
41
+ // ndarray:
42
+ > arr = {{alias:@stdlib/ndarray/array}}( arr, { 'shape': [ 2, 3 ] } );
43
+ > out = {{alias}}( arr, f );
44
+ > var v = out.get( 1, 1 )
45
+ 5
46
+
47
+
48
+ {{alias}}.assign( arr, out, fcn[, thisArg] )
49
+ Applies a function to each element in an array and assigns the result to an
50
+ element in an output array.
51
+
52
+ The applied function is provided the following arguments:
53
+
54
+ - value: array element.
55
+ - index: element index.
56
+ - arr: input array.
57
+
58
+ Input and output arrays must be either both array-like objects or both
59
+ ndarray-like objects.
60
+
61
+ If input and output arrays are array-like objects, the arrays must have the
62
+ same number of elements.
63
+
64
+ If input and output arrays are both ndarray-like objects, the arrays *must*
65
+ be broadcast compatible.
66
+
67
+ Parameters
68
+ ----------
69
+ arr: ArrayLikeObject|ndarray
70
+ Input array.
71
+
72
+ out: ArrayLikeObject|ndarray
73
+ Output array.
74
+
75
+ fcn: Function
76
+ Function to apply.
77
+
78
+ thisArg: any (optional)
79
+ Input function context.
80
+
81
+ Returns
82
+ -------
83
+ out: Array|ndarray
84
+ Output array.
85
+
86
+ Examples
87
+ --------
88
+ // array-like object:
89
+ > var f = {{alias:@stdlib/utils/nary-function}}( {{alias:@stdlib/math/base/special/abs}}, 1 );
90
+ > var arr = [ -1, -2, -3, -4, -5, -6 ];
91
+ > var out = [ 0, 0, 0, 0, 0, 0 ];
92
+ > {{alias}}.assign( arr, out, f );
93
+ > out
94
+ [ 1, 2, 3, 4, 5, 6 ]
95
+
96
+ // ndarray:
97
+ > var opts = { 'shape': [ 2, 3 ] };
98
+ > arr = {{alias:@stdlib/ndarray/array}}( arr, opts );
99
+ > out = {{alias:@stdlib/ndarray/array}}( [ 0, 0, 0, 0, 0, 0 ], opts );
100
+ > {{alias}}.assign( arr, out, f );
101
+ > var v = out.get( 1, 1 )
102
+ 5
103
+
104
+ See Also
105
+ --------
106
+