@stdlib/array-base-map 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/LICENSE ADDED
@@ -0,0 +1,177 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
package/NOTICE ADDED
@@ -0,0 +1 @@
1
+ Copyright (c) 2016-2026 The Stdlib Authors.
package/README.md ADDED
@@ -0,0 +1,282 @@
1
+ <!--
2
+
3
+ @license Apache-2.0
4
+
5
+ Copyright (c) 2025 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
+
22
+ <details>
23
+ <summary>
24
+ About stdlib...
25
+ </summary>
26
+ <p>We believe in a future in which the web is a preferred environment for numerical computation. To help realize this future, we've built stdlib. stdlib is a standard library, with an emphasis on numerical and scientific computation, written in JavaScript (and C) for execution in browsers and in Node.js.</p>
27
+ <p>The library is fully decomposable, being architected in such a way that you can swap out and mix and match APIs and functionality to cater to your exact preferences and use cases.</p>
28
+ <p>When you use stdlib, you can be absolutely certain that you are using the most thorough, rigorous, well-written, studied, documented, tested, measured, and high-quality code out there.</p>
29
+ <p>To join us in bringing numerical computing to the web, get started by checking us out on <a href="https://github.com/stdlib-js/stdlib">GitHub</a>, and please consider <a href="https://opencollective.com/stdlib">financially supporting stdlib</a>. We greatly appreciate your continued support!</p>
30
+ </details>
31
+
32
+ # map
33
+
34
+ [![NPM version][npm-image]][npm-url] [![Build Status][test-image]][test-url] [![Coverage Status][coverage-image]][coverage-url] <!-- [![dependencies][dependencies-image]][dependencies-url] -->
35
+
36
+ > Apply a callback function to elements in an input array and assign results to elements in a new output array.
37
+
38
+ <!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
39
+
40
+ <section class="intro">
41
+
42
+ </section>
43
+
44
+ <!-- /.intro -->
45
+
46
+ <!-- Package usage documentation. -->
47
+
48
+ <section class="installation">
49
+
50
+ ## Installation
51
+
52
+ ```bash
53
+ npm install @stdlib/array-base-map
54
+ ```
55
+
56
+ </section>
57
+
58
+ <section class="usage">
59
+
60
+ ## Usage
61
+
62
+ ```javascript
63
+ var map = require( '@stdlib/array-base-map' );
64
+ ```
65
+
66
+ #### map( x, fcn\[, thisArg] )
67
+
68
+ Applies a callback function to elements in an input array and assigns results to elements in a new output array.
69
+
70
+ ```javascript
71
+ var naryFunction = require( '@stdlib/utils-nary-function' );
72
+ var abs = require( '@stdlib/math-base-special-abs' );
73
+
74
+ var x = [ -1.0, -2.0, -3.0, -4.0 ];
75
+
76
+ var y = map( x, naryFunction( abs, 1 ) );
77
+ // returns [ 1.0, 2.0, 3.0, 4.0 ]
78
+ ```
79
+
80
+ The function accepts the following arguments:
81
+
82
+ - **x**: input array.
83
+ - **fcn**: callback function.
84
+ - **thisArg**: callback execution context (_optional_).
85
+
86
+ To set the callback function's execution context, provide a `thisArg`.
87
+
88
+ <!-- eslint-disable no-invalid-this -->
89
+
90
+ ```javascript
91
+ function count( x ) {
92
+ this.count += 1;
93
+ return x;
94
+ }
95
+
96
+ var x = [ 1.0, 2.0, 3.0, 4.0 ];
97
+
98
+ var ctx = {
99
+ 'count': 0
100
+ };
101
+
102
+ var y = map( x, count, ctx );
103
+ // returns [ 1.0, 2.0, 3.0, 4.0 ]
104
+
105
+ var v = ctx.count;
106
+ // returns 4
107
+ ```
108
+
109
+ The callback function is provided the following arguments:
110
+
111
+ - **value**: current array element.
112
+ - **index**: current element index.
113
+ - **arr**: input array.
114
+
115
+ #### map.assign( x, y, stride, offset, fcn\[, thisArg] )
116
+
117
+ Applies a callback function to elements in an input array and assigns results to elements in an output array.
118
+
119
+ ```javascript
120
+ var naryFunction = require( '@stdlib/utils-nary-function' );
121
+ var zeros = require( '@stdlib/array-base-zeros' );
122
+ var abs = require( '@stdlib/math-base-special-abs' );
123
+
124
+ var x = [ -1.0, -2.0, -3.0, -4.0 ];
125
+
126
+ var y = zeros( x.length );
127
+
128
+ var out = map.assign( x, y, 1, 0, naryFunction( abs, 1 ) );
129
+ // returns [ 1.0, 2.0, 3.0, 4.0 ]
130
+
131
+ var bool = ( out === y );
132
+ // returns true
133
+ ```
134
+
135
+ The function accepts the following arguments:
136
+
137
+ - **x**: input array.
138
+ - **y**: output array.
139
+ - **stride**: stride length for output array.
140
+ - **offset**: starting index for output array.
141
+ - **fcn**: callback function.
142
+ - **thisArg**: callback execution context (_optional_).
143
+
144
+ </section>
145
+
146
+ <!-- /.usage -->
147
+
148
+ <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
149
+
150
+ <section class="notes">
151
+
152
+ ## Notes
153
+
154
+ - If provided an array-like object having a `map` method, the function defers execution to that method and assumes that the method API has the following signature:
155
+
156
+ ```text
157
+ x.map( fcn, thisArg )
158
+ ```
159
+
160
+ - The function supports array-like objects having getter and setter accessors for array element access (e.g., [`@stdlib/array-base/accessor`][@stdlib/array/base/accessor]).
161
+
162
+ </section>
163
+
164
+ <!-- /.notes -->
165
+
166
+ <section class="examples">
167
+
168
+ ## Examples
169
+
170
+ <!-- eslint no-undef: "error" -->
171
+
172
+ ```javascript
173
+ var discreteUniform = require( '@stdlib/random-array-discrete-uniform' );
174
+ var naryFunction = require( '@stdlib/utils-nary-function' );
175
+ var abs = require( '@stdlib/math-base-special-abs' );
176
+ var map = require( '@stdlib/array-base-map' );
177
+
178
+ var x = discreteUniform( 10, -10, 10, {
179
+ 'dtype': 'float64'
180
+ });
181
+
182
+ var y = map( x, naryFunction( abs, 1 ) );
183
+ console.log( y );
184
+ ```
185
+
186
+ </section>
187
+
188
+ <!-- /.examples -->
189
+
190
+ <!-- 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. -->
191
+
192
+ <section class="references">
193
+
194
+ </section>
195
+
196
+ <!-- /.references -->
197
+
198
+ <!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
199
+
200
+ <section class="related">
201
+
202
+ </section>
203
+
204
+ <!-- /.related -->
205
+
206
+ <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
207
+
208
+
209
+ <section class="main-repo" >
210
+
211
+ * * *
212
+
213
+ ## Notice
214
+
215
+ 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.
216
+
217
+ 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].
218
+
219
+ #### Community
220
+
221
+ [![Chat][chat-image]][chat-url]
222
+
223
+ ---
224
+
225
+ ## License
226
+
227
+ See [LICENSE][stdlib-license].
228
+
229
+
230
+ ## Copyright
231
+
232
+ Copyright &copy; 2016-2026. The Stdlib [Authors][stdlib-authors].
233
+
234
+ </section>
235
+
236
+ <!-- /.stdlib -->
237
+
238
+ <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
239
+
240
+ <section class="links">
241
+
242
+ [npm-image]: http://img.shields.io/npm/v/@stdlib/array-base-map.svg
243
+ [npm-url]: https://npmjs.org/package/@stdlib/array-base-map
244
+
245
+ [test-image]: https://github.com/stdlib-js/array-base-map/actions/workflows/test.yml/badge.svg?branch=v0.1.0
246
+ [test-url]: https://github.com/stdlib-js/array-base-map/actions/workflows/test.yml?query=branch:v0.1.0
247
+
248
+ [coverage-image]: https://img.shields.io/codecov/c/github/stdlib-js/array-base-map/main.svg
249
+ [coverage-url]: https://codecov.io/github/stdlib-js/array-base-map?branch=main
250
+
251
+ <!--
252
+
253
+ [dependencies-image]: https://img.shields.io/david/stdlib-js/array-base-map.svg
254
+ [dependencies-url]: https://david-dm.org/stdlib-js/array-base-map/main
255
+
256
+ -->
257
+
258
+ [chat-image]: https://img.shields.io/badge/zulip-join_chat-brightgreen.svg
259
+ [chat-url]: https://stdlib.zulipchat.com
260
+
261
+ [stdlib]: https://github.com/stdlib-js/stdlib
262
+
263
+ [stdlib-authors]: https://github.com/stdlib-js/stdlib/graphs/contributors
264
+
265
+ [umd]: https://github.com/umdjs/umd
266
+ [es-module]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules
267
+
268
+ [deno-url]: https://github.com/stdlib-js/array-base-map/tree/deno
269
+ [deno-readme]: https://github.com/stdlib-js/array-base-map/blob/deno/README.md
270
+ [umd-url]: https://github.com/stdlib-js/array-base-map/tree/umd
271
+ [umd-readme]: https://github.com/stdlib-js/array-base-map/blob/umd/README.md
272
+ [esm-url]: https://github.com/stdlib-js/array-base-map/tree/esm
273
+ [esm-readme]: https://github.com/stdlib-js/array-base-map/blob/esm/README.md
274
+ [branches-url]: https://github.com/stdlib-js/array-base-map/blob/main/branches.md
275
+
276
+ [stdlib-license]: https://raw.githubusercontent.com/stdlib-js/array-base-map/main/LICENSE
277
+
278
+ [@stdlib/array/base/accessor]: https://www.npmjs.com/package/@stdlib/array-base-accessor
279
+
280
+ </section>
281
+
282
+ <!-- /.links -->
package/SECURITY.md ADDED
@@ -0,0 +1,5 @@
1
+ # Security
2
+
3
+ > Policy for reporting security vulnerabilities.
4
+
5
+ See the security policy [in the main project repository](https://github.com/stdlib-js/stdlib/security).
@@ -0,0 +1,3 @@
1
+ /// <reference path="../docs/types/index.d.ts" />
2
+ import map from '../docs/types/index';
3
+ export = map;
package/dist/index.js ADDED
@@ -0,0 +1,7 @@
1
+ "use strict";var p=function(a,r){return function(){return r||a((r={exports:{}}).exports,r),r.exports}};var v=p(function(B,q){
2
+ var g=require('@stdlib/array-base-arraylike2object/dist');function h(a,r,s,o,n,i){var e,t;for(e=o,t=0;t<a.length;t++)r[e]=n.call(i,a[t],t,a),e+=s;return r}function j(a,r,s,o,n,i){var e,t,l,f,u,c;for(e=a.data,t=r.data,l=a.accessors[0],f=r.accessors[1],u=o,c=0;c<e.length;c++)f(t,u,n.call(i,l(e,c),c,e)),u+=s;return r}function P(a,r,s,o,n,i){var e,t;return e=g(a),t=g(r),e.accessorProtocol||t.accessorProtocol?(j(e,t,s,o,n,i),r):h(a,r,s,o,n,i)}q.exports=P
3
+ });var m=p(function(C,d){
4
+ var k=require('@stdlib/array-base-zeros/dist'),y=v();function z(a,r){return typeof a[r]=="function"}function M(a,r,s){return z(a,"map")?a.map(r,s):y(a,k(a.length),1,0,r,s)}d.exports=M
5
+ });var O=require('@stdlib/utils-define-nonenumerable-read-only-property/dist'),b=m(),R=v();O(b,"assign",R);module.exports=b;
6
+ /** @license Apache-2.0 */
7
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../lib/assign.js", "../lib/main.js", "../lib/index.js"],
4
+ "sourcesContent": ["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2025 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 arraylike2object = require( '@stdlib/array-base-arraylike2object' );\n\n\n// FUNCTIONS //\n\n/**\n* Applies a callback function to elements in an input array and assigns results to elements in an output array.\n*\n* @private\n* @param {Collection} x - input array\n* @param {Collection} y - output array\n* @param {integer} stride - stride length for output array\n* @param {NonNegativeInteger} offset - starting index for output array\n* @param {Function} fcn - callback function\n* @param {*} thisArg - callback execution context\n* @returns {Collection} output array\n*\n* @example\n* var ones = require( '@stdlib/array-base-ones' );\n* var zeros = require( '@stdlib/array-base-zeros' );\n*\n* function scale( x ) {\n* return x * 10.0;\n* }\n*\n* var x = ones( 4 );\n* var y = zeros( x.length );\n* var out = internal( x, y, 1, 0, scale );\n* // returns [ 10.0, 10.0, 10.0, 10.0 ]\n*\n* var bool = ( out === y );\n* // returns true\n*/\nfunction internal( x, y, stride, offset, fcn, thisArg ) {\n\tvar io;\n\tvar i;\n\n\tio = offset;\n\tfor ( i = 0; i < x.length; i++ ) {\n\t\ty[ io ] = fcn.call( thisArg, x[ i ], i, x );\n\t\tio += stride;\n\t}\n\treturn y;\n}\n\n/**\n* Applies a callback function to elements in an input array and assigns results to elements in an output array.\n*\n* @private\n* @param {Object} x - input array object\n* @param {Object} y - output array object\n* @param {integer} stride - stride length for output array\n* @param {NonNegativeInteger} offset - starting index for output array\n* @param {Function} fcn - callback function\n* @param {*} thisArg - callback execution context\n* @returns {Object} output array object\n*\n* @example\n* var toAccessorArray = require( '@stdlib/array-base-to-accessor-array' );\n* var arraylike2object = require( '@stdlib/array-base-arraylike2object' );\n* var ones = require( '@stdlib/array-base-ones' );\n* var zeros = require( '@stdlib/array-base-zeros' );\n*\n* function scale( x ) {\n* return x * 10.0;\n* }\n*\n* var x = ones( 4 );\n* var y = zeros( x.length );\n*\n* var out = accessors( arraylike2object( toAccessorArray( x ) ), arraylike2object( toAccessorArray( y ) ), 1, 0, scale );\n* // y => [ 10.0, 10.0, 10.0, 10.0 ]\n*/\nfunction accessors( x, y, stride, offset, fcn, thisArg ) {\n\tvar xdata;\n\tvar ydata;\n\tvar xget;\n\tvar yset;\n\tvar io;\n\tvar i;\n\n\txdata = x.data;\n\tydata = y.data;\n\txget = x.accessors[ 0 ];\n\tyset = y.accessors[ 1 ];\n\n\tio = offset;\n\tfor ( i = 0; i < xdata.length; i++ ) {\n\t\tyset( ydata, io, fcn.call( thisArg, xget( xdata, i ), i, xdata ) );\n\t\tio += stride;\n\t}\n\treturn y;\n}\n\n\n// MAIN //\n\n/**\n* Applies a callback function to elements in an input array and assigns results to elements in an output array.\n*\n* @param {Collection} x - input array\n* @param {Collection} y - output array\n* @param {integer} stride - stride length for output array\n* @param {NonNegativeInteger} offset - starting index for output array\n* @param {Function} fcn - callback function\n* @param {*} [thisArg] - callback execution context\n* @returns {Collection} output array\n*\n* @example\n* var ones = require( '@stdlib/array-base-ones' );\n* var zeros = require( '@stdlib/array-base-zeros' );\n*\n* function scale( x ) {\n* return x * 10.0;\n* }\n*\n* var x = ones( 4 );\n* var y = zeros( x.length );\n*\n* var out = assign( x, y, 1, 0, scale );\n* // returns [ 10.0, 10.0, 10.0, 10.0 ]\n*\n* var bool = ( out === y );\n* // returns true\n*/\nfunction assign( x, y, stride, offset, fcn, thisArg ) {\n\tvar xobj;\n\tvar yobj;\n\n\txobj = arraylike2object( x );\n\tyobj = arraylike2object( y );\n\tif ( xobj.accessorProtocol || yobj.accessorProtocol ) {\n\t\taccessors( xobj, yobj, stride, offset, fcn, thisArg );\n\t\treturn y;\n\t}\n\treturn internal( x, y, stride, offset, fcn, thisArg );\n}\n\n\n// EXPORTS //\n\nmodule.exports = assign;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2025 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 zeros = require( '@stdlib/array-base-zeros' );\nvar assign = require( './assign.js' );\n\n\n// FUNCTIONS //\n\n/**\n* Tests whether an object has a specified method.\n*\n* @private\n* @param {Object} obj - input object\n* @param {string} method - method name\n* @returns {boolean} boolean indicating whether an object has a specified method\n*\n* @example\n* var bool = hasMethod( [], 'map' );\n* // returns true\n*\n* @example\n* var bool = hasMethod( [], 'beep' );\n* // returns false\n*/\nfunction hasMethod( obj, method ) {\n\treturn ( typeof obj[ method ] === 'function' );\n}\n\n\n// MAIN //\n\n/**\n* Applies a callback function to elements in an input array and assigns results to elements in a new output array.\n*\n* @param {Collection} x - input array\n* @param {Function} fcn - callback function\n* @param {*} [thisArg] - callback execution context\n* @returns {Collection} output array\n*\n* @example\n* var ones = require( '@stdlib/array-base-ones' );\n*\n* function scale( x ) {\n* return x * 10.0;\n* }\n*\n* var x = ones( 4 );\n* var y = map( x, scale );\n* // returns [ 10.0, 10.0, 10.0, 10.0 ]\n*/\nfunction map( x, fcn, thisArg ) {\n\tif ( hasMethod( x, 'map' ) ) {\n\t\treturn x.map( fcn, thisArg );\n\t}\n\treturn assign( x, zeros( x.length ), 1, 0, fcn, thisArg );\n}\n\n\n// EXPORTS //\n\nmodule.exports = map;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2025 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 callback function to elements in an input array and assign results to elements in a new output array.\n*\n* @module @stdlib/array-base-map\n*\n* @example\n* var ones = require( '@stdlib/array-base-ones' );\n* var map = require( '@stdlib/array-base-map' );\n*\n* function scale( x ) {\n* return x * 10.0;\n* }\n*\n* var x = ones( 4 );\n* var y = map( x, scale );\n* // returns [ 10.0, 10.0, 10.0, 10.0 ]\n*\n* @example\n* var ones = require( '@stdlib/array-base-ones' );\n* var zeros = require( '@stdlib/array-base-zeros' );\n* var map = require( '@stdlib/array-base-map' );\n*\n* function scale( x ) {\n* return x * 10.0;\n* }\n*\n* var x = ones( 4 );\n* var y = zeros( x.length );\n*\n* var out = map.assign( x, y, 1, 0, scale );\n* // returns [ 10.0, 10.0, 10.0, 10.0 ]\n*\n* var bool = ( out === y );\n* // returns true\n*/\n\n// MODULES //\n\nvar setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' );\nvar main = require( './main.js' );\nvar assign = require( './assign.js' );\n\n\n// MAIN //\n\nsetReadOnly( main, 'assign', assign );\n\n\n// EXPORTS //\n\nmodule.exports = main;\n\n// exports: { \"assign\": \"main.assign\" }\n"],
5
+ "mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAmB,QAAS,qCAAsC,EAiCtE,SAASC,EAAUC,EAAGC,EAAGC,EAAQC,EAAQC,EAAKC,EAAU,CACvD,IAAIC,EACAC,EAGJ,IADAD,EAAKH,EACCI,EAAI,EAAGA,EAAIP,EAAE,OAAQO,IAC1BN,EAAGK,CAAG,EAAIF,EAAI,KAAMC,EAASL,EAAGO,CAAE,EAAGA,EAAGP,CAAE,EAC1CM,GAAMJ,EAEP,OAAOD,CACR,CA8BA,SAASO,EAAWR,EAAGC,EAAGC,EAAQC,EAAQC,EAAKC,EAAU,CACxD,IAAII,EACAC,EACAC,EACAC,EACAN,EACAC,EAQJ,IANAE,EAAQT,EAAE,KACVU,EAAQT,EAAE,KACVU,EAAOX,EAAE,UAAW,CAAE,EACtBY,EAAOX,EAAE,UAAW,CAAE,EAEtBK,EAAKH,EACCI,EAAI,EAAGA,EAAIE,EAAM,OAAQF,IAC9BK,EAAMF,EAAOJ,EAAIF,EAAI,KAAMC,EAASM,EAAMF,EAAOF,CAAE,EAAGA,EAAGE,CAAM,CAAE,EACjEH,GAAMJ,EAEP,OAAOD,CACR,CAiCA,SAASY,EAAQb,EAAGC,EAAGC,EAAQC,EAAQC,EAAKC,EAAU,CACrD,IAAIS,EACAC,EAIJ,OAFAD,EAAOhB,EAAkBE,CAAE,EAC3Be,EAAOjB,EAAkBG,CAAE,EACtBa,EAAK,kBAAoBC,EAAK,kBAClCP,EAAWM,EAAMC,EAAMb,EAAQC,EAAQC,EAAKC,CAAQ,EAC7CJ,GAEDF,EAAUC,EAAGC,EAAGC,EAAQC,EAAQC,EAAKC,CAAQ,CACrD,CAKAR,EAAO,QAAUgB,ICnKjB,IAAAG,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAQ,QAAS,0BAA2B,EAC5CC,EAAS,IAqBb,SAASC,EAAWC,EAAKC,EAAS,CACjC,OAAS,OAAOD,EAAKC,CAAO,GAAM,UACnC,CAwBA,SAASC,EAAKC,EAAGC,EAAKC,EAAU,CAC/B,OAAKN,EAAWI,EAAG,KAAM,EACjBA,EAAE,IAAKC,EAAKC,CAAQ,EAErBP,EAAQK,EAAGN,EAAOM,EAAE,MAAO,EAAG,EAAG,EAAGC,EAAKC,CAAQ,CACzD,CAKAT,EAAO,QAAUM,ICtBjB,IAAII,EAAc,QAAS,uDAAwD,EAC/EC,EAAO,IACPC,EAAS,IAKbF,EAAaC,EAAM,SAAUC,CAAO,EAKpC,OAAO,QAAUD",
6
+ "names": ["require_assign", "__commonJSMin", "exports", "module", "arraylike2object", "internal", "x", "y", "stride", "offset", "fcn", "thisArg", "io", "i", "accessors", "xdata", "ydata", "xget", "yset", "assign", "xobj", "yobj", "require_main", "__commonJSMin", "exports", "module", "zeros", "assign", "hasMethod", "obj", "method", "map", "x", "fcn", "thisArg", "setReadOnly", "main", "assign"]
7
+ }
@@ -0,0 +1,288 @@
1
+ /*
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2025 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: 4.1
20
+
21
+ /// <reference types="@stdlib/types"/>
22
+
23
+ import { Collection, AccessorArrayLike, TypedArray } from '@stdlib/types/array';
24
+
25
+ /**
26
+ * Input array.
27
+ */
28
+ type InputArray<T> = Collection<T> | AccessorArrayLike<T>;
29
+
30
+ /**
31
+ * Output array.
32
+ */
33
+ type OutputArray<T> = Collection<T> | AccessorArrayLike<T>;
34
+
35
+ /**
36
+ * Interface for an object having a `map` method.
37
+ */
38
+ interface ObjectWithMap<T> {
39
+ /**
40
+ * Invokes a callback for each element in an array and assigns results to a new output array.
41
+ *
42
+ * @param clbk - callback to apply
43
+ * @param thisArg - callback execution context
44
+ * @returns output array
45
+ */
46
+ map<V>( clbk: Callback<T, InputArray<T>, V, unknown>, thisArg?: unknown ): ObjectWithMap<V>;
47
+ }
48
+
49
+ /**
50
+ * An array having a `map` method.
51
+ */
52
+ type ArrayWithMap<T> = InputArray<T> & ObjectWithMap<T>;
53
+
54
+ /**
55
+ * Callback invoked for each array element.
56
+ *
57
+ * @returns result
58
+ */
59
+ type Nullary<V, ThisArg> = ( this: ThisArg ) => V;
60
+
61
+ /**
62
+ * Callback invoked for each array element.
63
+ *
64
+ * @param value - current array element
65
+ * @returns result
66
+ */
67
+ type Unary<T, V, ThisArg> = ( this: ThisArg, value: T ) => V;
68
+
69
+ /**
70
+ * Callback invoked for each array element.
71
+ *
72
+ * @param value - current array element
73
+ * @param index - current array element index
74
+ * @returns result
75
+ */
76
+ type Binary<T, V, ThisArg> = ( this: ThisArg, value: T, index: number ) => V;
77
+
78
+ /**
79
+ * Callback invoked for each array element.
80
+ *
81
+ * @param value - current array element
82
+ * @param index - current array element index
83
+ * @param arr - input array
84
+ * @returns result
85
+ */
86
+ type Ternary<T, U, V, ThisArg> = ( this: ThisArg, value: T, index: number, arr: U ) => V;
87
+
88
+ /**
89
+ * Callback invoked for each array element.
90
+ *
91
+ * @param value - current array element
92
+ * @param index - current array element index
93
+ * @param arr - input array
94
+ * @returns result
95
+ */
96
+ type Callback<T, U, V, ThisArg> = Nullary<V, ThisArg> | Unary<T, V, ThisArg> | Binary<T, V, ThisArg> | Ternary<T, U, V, ThisArg>;
97
+
98
+ /**
99
+ * Interface describing the main export.
100
+ */
101
+ interface Routine {
102
+ /**
103
+ * Applies a callback function to elements in an input array and assigns results to elements in a new output array.
104
+ *
105
+ * @param x - input array object
106
+ * @param fcn - callback function
107
+ * @param thisArg - callback execution context
108
+ * @returns output array
109
+ *
110
+ * @example
111
+ * var ones = require( '@stdlib/array-base-ones' );
112
+ *
113
+ * function scale( v ) {
114
+ * return v * 10;
115
+ * }
116
+ *
117
+ * var x = ones( 4 );
118
+ * // returns [ 0, 0, 0, 0 ]
119
+ *
120
+ * var y = map( x, scale );
121
+ * // returns [ 10, 10, 10, 10 ]
122
+ */
123
+ <T = unknown, U extends Array<T> = Array<T>, V = unknown, W extends Array<V> = Array<V>, ThisArg = unknown>( x: U, fcn: Callback<T, U, V, ThisArg>, thisArg?: ThisParameterType<Callback<T, U, V, ThisArg>> ): W;
124
+
125
+ /**
126
+ * Applies a callback function to elements in an input array and assigns results to elements in a new output array.
127
+ *
128
+ * @param x - input array object
129
+ * @param fcn - callback function
130
+ * @param thisArg - callback execution context
131
+ * @returns output array
132
+ *
133
+ * @example
134
+ * var ones = require( '@stdlib/array-ones' );
135
+ *
136
+ * function scale( v ) {
137
+ * return v * 10;
138
+ * }
139
+ *
140
+ * var x = ones( 4, 'int32' );
141
+ * // returns <Int32Array>[ 0, 0, 0, 0 ]
142
+ *
143
+ * var y = map( x, scale );
144
+ * // returns <Int32Array>[ 10, 10, 10, 10 ]
145
+ */
146
+ <T = unknown, U extends TypedArray = TypedArray, V = unknown, ThisArg = unknown>( x: U, fcn: Callback<T, U, V, ThisArg>, thisArg?: ThisParameterType<Callback<T, U, V, ThisArg>> ): U;
147
+
148
+ /**
149
+ * Applies a callback function to elements in an input array and assigns results to elements in a new output array.
150
+ *
151
+ * ## Notes
152
+ *
153
+ * - We assume that an input array having a `map` method returns an array of the same class, and, thus, the output array should also have a `map` method.
154
+ *
155
+ * @param x - input array object
156
+ * @param fcn - callback function
157
+ * @param thisArg - callback execution context
158
+ * @returns output array
159
+ *
160
+ * @example
161
+ * var ones = require( '@stdlib/array-base-ones' );
162
+ *
163
+ * function scale( v ) {
164
+ * return v * 10;
165
+ * }
166
+ *
167
+ * var x = ones( 4 );
168
+ * // returns [ 0, 0, 0, 0 ]
169
+ *
170
+ * var y = map( x, scale );
171
+ * // returns [ 10, 10, 10, 10 ]
172
+ */
173
+ <T = unknown, U extends ArrayWithMap<T> = ArrayWithMap<T>, V = unknown, W extends ArrayWithMap<V> = ArrayWithMap<V>, ThisArg = unknown>( x: U, fcn: Callback<T, U, V, ThisArg>, thisArg?: ThisParameterType<Callback<T, U, V, ThisArg>> ): W;
174
+
175
+ /**
176
+ * Applies a callback function to elements in an input array and assigns results to elements in a new output array.
177
+ *
178
+ * ## Notes
179
+ *
180
+ * - When an input array does not have a `map` method, we always return a new "generic" array.
181
+ *
182
+ * @param x - input array object
183
+ * @param fcn - callback function
184
+ * @param thisArg - callback execution context
185
+ * @returns output array
186
+ *
187
+ * @example
188
+ * var toAccessorArray = require( '@stdlib/array-to-accessor-array' );
189
+ * var ones = require( '@stdlib/array-base-ones' );
190
+ *
191
+ * function scale( v ) {
192
+ * return v * 10;
193
+ * }
194
+ *
195
+ * var x = ones( 4 );
196
+ * // returns [ 0, 0, 0, 0 ]
197
+ *
198
+ * var y = map( toAccessorArray( x ), scale );
199
+ * // returns [ 10, 10, 10, 10 ]
200
+ */
201
+ <T = unknown, U extends InputArray<T> = InputArray<T>, V = unknown, ThisArg = unknown>( x: U, fcn: Callback<T, U, V, ThisArg>, thisArg?: ThisParameterType<Callback<T, U, V, ThisArg>> ): Array<V>;
202
+
203
+ /**
204
+ * Applies a callback function to elements in an input array and assigns results to elements in an output array.
205
+ *
206
+ * @param x - input array object
207
+ * @param y - output array object
208
+ * @param stride - stride length for output array
209
+ * @param offset - starting index for output array
210
+ * @param fcn - callback function
211
+ * @param thisArg - callback execution context
212
+ * @returns output array object
213
+ *
214
+ * @example
215
+ * var ones = require( '@stdlib/array-base-ones' );
216
+ * var zeros = require( '@stdlib/array-base-zeros' );
217
+ *
218
+ * function scale( v ) {
219
+ * return v * 10;
220
+ * }
221
+ * var x = ones( 4 );
222
+ * var y = zeros( x.length );
223
+ *
224
+ * var out = map.assign( x, y, 1, 0, scale );
225
+ * // returns [ 10, 10, 10, 10 ]
226
+ *
227
+ * var bool = ( out === y );
228
+ * // returns true
229
+ *
230
+ * @example
231
+ * var toAccessorArray = require( '@stdlib/array-to-accessor-array' );
232
+ * var ones = require( '@stdlib/array-base-ones' );
233
+ * var zeros = require( '@stdlib/array-base-zeros' );
234
+ *
235
+ * function scale( v ) {
236
+ * return v * 10;
237
+ * }
238
+ * var x = ones( 4 );
239
+ * var y = zeros( x.length );
240
+ *
241
+ * var out = map.assign( toAccessorArray( x ), toAccessorArray( y ), 1, 0 scale );
242
+ * // y => [ 10, 10, 10, 10 ]
243
+ */
244
+ assign<T = unknown, U extends InputArray<T> = InputArray<T>, V = unknown, W extends OutputArray<V> = OutputArray<V>, ThisArg = unknown>( x: U, y: W, stride: number, offset: number, fcn: Callback<T, U, V, ThisArg>, thisArg?: ThisParameterType<Callback<T, U, V, ThisArg>> ): W;
245
+ }
246
+
247
+ /**
248
+ * Applies a callback function to elements in an input array and assigns results to elements in a new output array.
249
+ *
250
+ * @param x - input array
251
+ * @param fcn - callback function
252
+ * @param thisArg - callback execution context
253
+ * @returns output array
254
+ *
255
+ * @example
256
+ * var ones = require( '@stdlib/array-base-ones' );
257
+ *
258
+ * function scale( v ) {
259
+ * return v * 10;
260
+ * }
261
+ *
262
+ * var x = ones( 4 );
263
+ * var y = map( x, scale );
264
+ * // returns [ 10, 10, 10, 10 ]
265
+ *
266
+ * @example
267
+ * var ones = require( '@stdlib/array-base-ones' );
268
+ * var zeros = require( '@stdlib/array-base-zeros' );
269
+ *
270
+ * function scale( v ) {
271
+ * return v * 10;
272
+ * }
273
+ *
274
+ * var x = ones( 4 );
275
+ * var y = zeros( x.length );
276
+ *
277
+ * var out = map.assign( x, y, 1, 0, scale );
278
+ * // y => [ 10, 10, 10, 10 ]
279
+ *
280
+ * var bool = ( out === y );
281
+ * // returns true
282
+ */
283
+ declare var map: Routine;
284
+
285
+
286
+ // EXPORTS //
287
+
288
+ export = map;
package/lib/assign.js ADDED
@@ -0,0 +1,164 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2025 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 arraylike2object = require( '@stdlib/array-base-arraylike2object' );
24
+
25
+
26
+ // FUNCTIONS //
27
+
28
+ /**
29
+ * Applies a callback function to elements in an input array and assigns results to elements in an output array.
30
+ *
31
+ * @private
32
+ * @param {Collection} x - input array
33
+ * @param {Collection} y - output array
34
+ * @param {integer} stride - stride length for output array
35
+ * @param {NonNegativeInteger} offset - starting index for output array
36
+ * @param {Function} fcn - callback function
37
+ * @param {*} thisArg - callback execution context
38
+ * @returns {Collection} output array
39
+ *
40
+ * @example
41
+ * var ones = require( '@stdlib/array-base-ones' );
42
+ * var zeros = require( '@stdlib/array-base-zeros' );
43
+ *
44
+ * function scale( x ) {
45
+ * return x * 10.0;
46
+ * }
47
+ *
48
+ * var x = ones( 4 );
49
+ * var y = zeros( x.length );
50
+ * var out = internal( x, y, 1, 0, scale );
51
+ * // returns [ 10.0, 10.0, 10.0, 10.0 ]
52
+ *
53
+ * var bool = ( out === y );
54
+ * // returns true
55
+ */
56
+ function internal( x, y, stride, offset, fcn, thisArg ) {
57
+ var io;
58
+ var i;
59
+
60
+ io = offset;
61
+ for ( i = 0; i < x.length; i++ ) {
62
+ y[ io ] = fcn.call( thisArg, x[ i ], i, x );
63
+ io += stride;
64
+ }
65
+ return y;
66
+ }
67
+
68
+ /**
69
+ * Applies a callback function to elements in an input array and assigns results to elements in an output array.
70
+ *
71
+ * @private
72
+ * @param {Object} x - input array object
73
+ * @param {Object} y - output array object
74
+ * @param {integer} stride - stride length for output array
75
+ * @param {NonNegativeInteger} offset - starting index for output array
76
+ * @param {Function} fcn - callback function
77
+ * @param {*} thisArg - callback execution context
78
+ * @returns {Object} output array object
79
+ *
80
+ * @example
81
+ * var toAccessorArray = require( '@stdlib/array-base-to-accessor-array' );
82
+ * var arraylike2object = require( '@stdlib/array-base-arraylike2object' );
83
+ * var ones = require( '@stdlib/array-base-ones' );
84
+ * var zeros = require( '@stdlib/array-base-zeros' );
85
+ *
86
+ * function scale( x ) {
87
+ * return x * 10.0;
88
+ * }
89
+ *
90
+ * var x = ones( 4 );
91
+ * var y = zeros( x.length );
92
+ *
93
+ * var out = accessors( arraylike2object( toAccessorArray( x ) ), arraylike2object( toAccessorArray( y ) ), 1, 0, scale );
94
+ * // y => [ 10.0, 10.0, 10.0, 10.0 ]
95
+ */
96
+ function accessors( x, y, stride, offset, fcn, thisArg ) {
97
+ var xdata;
98
+ var ydata;
99
+ var xget;
100
+ var yset;
101
+ var io;
102
+ var i;
103
+
104
+ xdata = x.data;
105
+ ydata = y.data;
106
+ xget = x.accessors[ 0 ];
107
+ yset = y.accessors[ 1 ];
108
+
109
+ io = offset;
110
+ for ( i = 0; i < xdata.length; i++ ) {
111
+ yset( ydata, io, fcn.call( thisArg, xget( xdata, i ), i, xdata ) );
112
+ io += stride;
113
+ }
114
+ return y;
115
+ }
116
+
117
+
118
+ // MAIN //
119
+
120
+ /**
121
+ * Applies a callback function to elements in an input array and assigns results to elements in an output array.
122
+ *
123
+ * @param {Collection} x - input array
124
+ * @param {Collection} y - output array
125
+ * @param {integer} stride - stride length for output array
126
+ * @param {NonNegativeInteger} offset - starting index for output array
127
+ * @param {Function} fcn - callback function
128
+ * @param {*} [thisArg] - callback execution context
129
+ * @returns {Collection} output array
130
+ *
131
+ * @example
132
+ * var ones = require( '@stdlib/array-base-ones' );
133
+ * var zeros = require( '@stdlib/array-base-zeros' );
134
+ *
135
+ * function scale( x ) {
136
+ * return x * 10.0;
137
+ * }
138
+ *
139
+ * var x = ones( 4 );
140
+ * var y = zeros( x.length );
141
+ *
142
+ * var out = assign( x, y, 1, 0, scale );
143
+ * // returns [ 10.0, 10.0, 10.0, 10.0 ]
144
+ *
145
+ * var bool = ( out === y );
146
+ * // returns true
147
+ */
148
+ function assign( x, y, stride, offset, fcn, thisArg ) {
149
+ var xobj;
150
+ var yobj;
151
+
152
+ xobj = arraylike2object( x );
153
+ yobj = arraylike2object( y );
154
+ if ( xobj.accessorProtocol || yobj.accessorProtocol ) {
155
+ accessors( xobj, yobj, stride, offset, fcn, thisArg );
156
+ return y;
157
+ }
158
+ return internal( x, y, stride, offset, fcn, thisArg );
159
+ }
160
+
161
+
162
+ // EXPORTS //
163
+
164
+ module.exports = assign;
package/lib/index.js ADDED
@@ -0,0 +1,73 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2025 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
+ * Apply a callback function to elements in an input array and assign results to elements in a new output array.
23
+ *
24
+ * @module @stdlib/array-base-map
25
+ *
26
+ * @example
27
+ * var ones = require( '@stdlib/array-base-ones' );
28
+ * var map = require( '@stdlib/array-base-map' );
29
+ *
30
+ * function scale( x ) {
31
+ * return x * 10.0;
32
+ * }
33
+ *
34
+ * var x = ones( 4 );
35
+ * var y = map( x, scale );
36
+ * // returns [ 10.0, 10.0, 10.0, 10.0 ]
37
+ *
38
+ * @example
39
+ * var ones = require( '@stdlib/array-base-ones' );
40
+ * var zeros = require( '@stdlib/array-base-zeros' );
41
+ * var map = require( '@stdlib/array-base-map' );
42
+ *
43
+ * function scale( x ) {
44
+ * return x * 10.0;
45
+ * }
46
+ *
47
+ * var x = ones( 4 );
48
+ * var y = zeros( x.length );
49
+ *
50
+ * var out = map.assign( x, y, 1, 0, scale );
51
+ * // returns [ 10.0, 10.0, 10.0, 10.0 ]
52
+ *
53
+ * var bool = ( out === y );
54
+ * // returns true
55
+ */
56
+
57
+ // MODULES //
58
+
59
+ var setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' );
60
+ var main = require( './main.js' );
61
+ var assign = require( './assign.js' );
62
+
63
+
64
+ // MAIN //
65
+
66
+ setReadOnly( main, 'assign', assign );
67
+
68
+
69
+ // EXPORTS //
70
+
71
+ module.exports = main;
72
+
73
+ // exports: { "assign": "main.assign" }
package/lib/main.js ADDED
@@ -0,0 +1,81 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2025 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 zeros = require( '@stdlib/array-base-zeros' );
24
+ var assign = require( './assign.js' );
25
+
26
+
27
+ // FUNCTIONS //
28
+
29
+ /**
30
+ * Tests whether an object has a specified method.
31
+ *
32
+ * @private
33
+ * @param {Object} obj - input object
34
+ * @param {string} method - method name
35
+ * @returns {boolean} boolean indicating whether an object has a specified method
36
+ *
37
+ * @example
38
+ * var bool = hasMethod( [], 'map' );
39
+ * // returns true
40
+ *
41
+ * @example
42
+ * var bool = hasMethod( [], 'beep' );
43
+ * // returns false
44
+ */
45
+ function hasMethod( obj, method ) {
46
+ return ( typeof obj[ method ] === 'function' );
47
+ }
48
+
49
+
50
+ // MAIN //
51
+
52
+ /**
53
+ * Applies a callback function to elements in an input array and assigns results to elements in a new output array.
54
+ *
55
+ * @param {Collection} x - input array
56
+ * @param {Function} fcn - callback function
57
+ * @param {*} [thisArg] - callback execution context
58
+ * @returns {Collection} output array
59
+ *
60
+ * @example
61
+ * var ones = require( '@stdlib/array-base-ones' );
62
+ *
63
+ * function scale( x ) {
64
+ * return x * 10.0;
65
+ * }
66
+ *
67
+ * var x = ones( 4 );
68
+ * var y = map( x, scale );
69
+ * // returns [ 10.0, 10.0, 10.0, 10.0 ]
70
+ */
71
+ function map( x, fcn, thisArg ) {
72
+ if ( hasMethod( x, 'map' ) ) {
73
+ return x.map( fcn, thisArg );
74
+ }
75
+ return assign( x, zeros( x.length ), 1, 0, fcn, thisArg );
76
+ }
77
+
78
+
79
+ // EXPORTS //
80
+
81
+ module.exports = map;
package/package.json ADDED
@@ -0,0 +1,71 @@
1
+ {
2
+ "name": "@stdlib/array-base-map",
3
+ "version": "0.1.0",
4
+ "description": "Apply a callback function to elements in an input array and assign results to elements in a new output array.",
5
+ "license": "Apache-2.0",
6
+ "author": {
7
+ "name": "The Stdlib Authors",
8
+ "url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
9
+ },
10
+ "contributors": [
11
+ {
12
+ "name": "The Stdlib Authors",
13
+ "url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
14
+ }
15
+ ],
16
+ "main": "./lib",
17
+ "directories": {
18
+ "doc": "./docs",
19
+ "lib": "./lib",
20
+ "dist": "./dist"
21
+ },
22
+ "types": "./docs/types",
23
+ "scripts": {},
24
+ "homepage": "https://stdlib.io",
25
+ "repository": {
26
+ "type": "git",
27
+ "url": "git://github.com/stdlib-js/array-base-map.git"
28
+ },
29
+ "bugs": {
30
+ "url": "https://github.com/stdlib-js/stdlib/issues"
31
+ },
32
+ "dependencies": {
33
+ "@stdlib/array-base-arraylike2object": "^0.2.1",
34
+ "@stdlib/array-base-zeros": "^0.2.2",
35
+ "@stdlib/utils-define-nonenumerable-read-only-property": "^0.2.2"
36
+ },
37
+ "devDependencies": {},
38
+ "engines": {
39
+ "node": ">=0.10.0",
40
+ "npm": ">2.7.0"
41
+ },
42
+ "os": [
43
+ "aix",
44
+ "darwin",
45
+ "freebsd",
46
+ "linux",
47
+ "macos",
48
+ "openbsd",
49
+ "sunos",
50
+ "win32",
51
+ "windows"
52
+ ],
53
+ "keywords": [
54
+ "stdlib",
55
+ "stdtypes",
56
+ "types",
57
+ "data",
58
+ "structure",
59
+ "array",
60
+ "foreach",
61
+ "for-each",
62
+ "generic",
63
+ "callback",
64
+ "map",
65
+ "array.map"
66
+ ],
67
+ "funding": {
68
+ "type": "opencollective",
69
+ "url": "https://opencollective.com/stdlib"
70
+ }
71
+ }