Skip to content

Commit 9b2b107

Browse files
headlessNodekgryte
andauthored
feat: add array/base/map
PR-URL: #5346 Closes: stdlib-js/metr-issue-tracker#21 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Signed-off-by: Muhammad Haris <harriskhan047@outlook.com>
1 parent 78cd0c6 commit 9b2b107

File tree

14 files changed

+2056
-0
lines changed

14 files changed

+2056
-0
lines changed
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
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+
# map
22+
23+
> Apply a callback function to elements in an input array and assign results to elements in a new output array.
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<!-- Package usage documentation. -->
34+
35+
<section class="usage">
36+
37+
## Usage
38+
39+
```javascript
40+
var map = require( '@stdlib/array/base/map' );
41+
```
42+
43+
#### map( x, fcn\[, thisArg] )
44+
45+
Applies a callback function to elements in an input array and assigns results to elements in a new output array.
46+
47+
```javascript
48+
var naryFunction = require( '@stdlib/utils/nary-function' );
49+
var abs = require( '@stdlib/math/base/special/abs' );
50+
51+
var x = [ -1.0, -2.0, -3.0, -4.0 ];
52+
53+
var y = map( x, naryFunction( abs, 1 ) );
54+
// returns [ 1.0, 2.0, 3.0, 4.0 ]
55+
```
56+
57+
The function accepts the following arguments:
58+
59+
- **x**: input array.
60+
- **fcn**: callback function.
61+
- **thisArg**: callback execution context (_optional_).
62+
63+
To set the callback function's execution context, provide a `thisArg`.
64+
65+
<!-- eslint-disable no-invalid-this -->
66+
67+
```javascript
68+
function count( x ) {
69+
this.count += 1;
70+
return x;
71+
}
72+
73+
var x = [ 1.0, 2.0, 3.0, 4.0 ];
74+
75+
var ctx = {
76+
'count': 0
77+
};
78+
79+
var y = map( x, count, ctx );
80+
// returns [ 1.0, 2.0, 3.0, 4.0 ]
81+
82+
var v = ctx.count;
83+
// returns 4
84+
```
85+
86+
The callback function is provided the following arguments:
87+
88+
- **value**: current array element.
89+
- **index**: current element index.
90+
- **arr**: input array.
91+
92+
#### map.assign( x, y, stride, offset, fcn\[, thisArg] )
93+
94+
Applies a callback function to elements in an input array and assigns results to elements in an output array.
95+
96+
```javascript
97+
var naryFunction = require( '@stdlib/utils/nary-function' );
98+
var zeros = require( '@stdlib/array/base/zeros' );
99+
var abs = require( '@stdlib/math/base/special/abs' );
100+
101+
var x = [ -1.0, -2.0, -3.0, -4.0 ];
102+
103+
var y = zeros( x.length );
104+
105+
var out = map.assign( x, y, 1, 0, naryFunction( abs, 1 ) );
106+
// returns [ 1.0, 2.0, 3.0, 4.0 ]
107+
108+
var bool = ( out === y );
109+
// returns true
110+
```
111+
112+
The function accepts the following arguments:
113+
114+
- **x**: input array.
115+
- **y**: output array.
116+
- **stride**: stride length for output array.
117+
- **offset**: starting index for output array.
118+
- **fcn**: callback function.
119+
- **thisArg**: callback execution context (_optional_).
120+
121+
</section>
122+
123+
<!-- /.usage -->
124+
125+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
126+
127+
<section class="notes">
128+
129+
## Notes
130+
131+
- 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:
132+
133+
```text
134+
x.map( fcn, thisArg )
135+
```
136+
137+
- 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]).
138+
139+
</section>
140+
141+
<!-- /.notes -->
142+
143+
<section class="examples">
144+
145+
## Examples
146+
147+
<!-- eslint no-undef: "error" -->
148+
149+
```javascript
150+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
151+
var naryFunction = require( '@stdlib/utils/nary-function' );
152+
var abs = require( '@stdlib/math/base/special/abs' );
153+
var map = require( '@stdlib/array/base/map' );
154+
155+
var x = discreteUniform( 10, -10, 10, {
156+
'dtype': 'float64'
157+
});
158+
159+
var y = map( x, naryFunction( abs, 1 ) );
160+
console.log( y );
161+
```
162+
163+
</section>
164+
165+
<!-- /.examples -->
166+
167+
<!-- 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. -->
168+
169+
<section class="references">
170+
171+
</section>
172+
173+
<!-- /.references -->
174+
175+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
176+
177+
<section class="related">
178+
179+
</section>
180+
181+
<!-- /.related -->
182+
183+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
184+
185+
<section class="links">
186+
187+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
188+
189+
</section>
190+
191+
<!-- /.links -->
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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 bench = require( '@stdlib/bench' );
24+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
25+
var pow = require( '@stdlib/math/base/special/pow' );
26+
var uniform = require( '@stdlib/random/array/uniform' );
27+
var identity = require( '@stdlib/number/float64/base/identity' );
28+
var zeros = require( '@stdlib/array/base/zeros' );
29+
var pkg = require( './../package.json' ).name;
30+
var map = require( './../lib' ).assign;
31+
32+
33+
// FUNCTIONS //
34+
35+
/**
36+
* Creates a benchmark function.
37+
*
38+
* @private
39+
* @param {PositiveInteger} len - array length
40+
* @returns {Function} benchmark function
41+
*/
42+
function createBenchmark( len ) {
43+
var x;
44+
var y;
45+
46+
x = uniform( len, -100.0, 100.0, {
47+
'dtype': 'generic'
48+
});
49+
y = zeros( len );
50+
51+
return benchmark;
52+
53+
/**
54+
* Benchmark function.
55+
*
56+
* @private
57+
* @param {Benchmark} b - benchmark instance
58+
*/
59+
function benchmark( b ) {
60+
var out;
61+
var i;
62+
63+
b.tic();
64+
for ( i = 0; i < b.iterations; i++ ) {
65+
out = map( x, y, 1, 0, identity );
66+
if ( isnan( out[ i%len ] ) ) {
67+
b.fail( 'should not return NaN' );
68+
}
69+
}
70+
b.toc();
71+
72+
if ( isnan( out[ i % len ] ) ) {
73+
b.fail( 'should not return NaN' );
74+
}
75+
b.pass( 'benchmark finished' );
76+
b.end();
77+
}
78+
}
79+
80+
81+
// MAIN //
82+
83+
/**
84+
* Main execution sequence.
85+
*
86+
* @private
87+
*/
88+
function main() {
89+
var len;
90+
var min;
91+
var max;
92+
var f;
93+
var i;
94+
95+
min = 1; // 10^min
96+
max = 6; // 10^max
97+
98+
for ( i = min; i <= max; i++ ) {
99+
len = pow( 10, i );
100+
101+
f = createBenchmark( len );
102+
bench( pkg+':assign:dtype=generic,len='+len, f );
103+
}
104+
}
105+
106+
main();
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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 bench = require( '@stdlib/bench' );
24+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
25+
var pow = require( '@stdlib/math/base/special/pow' );
26+
var uniform = require( '@stdlib/random/array/uniform' );
27+
var identity = require( '@stdlib/number/float64/base/identity' );
28+
var pkg = require( './../package.json' ).name;
29+
var map = require( './../lib' );
30+
31+
32+
// FUNCTIONS //
33+
34+
/**
35+
* Creates a benchmark function.
36+
*
37+
* @private
38+
* @param {PositiveInteger} len - array length
39+
* @returns {Function} benchmark function
40+
*/
41+
function createBenchmark( len ) {
42+
var x = uniform( len, -100.0, 100.0, {
43+
'dtype': 'generic'
44+
});
45+
return benchmark;
46+
47+
/**
48+
* Benchmark function.
49+
*
50+
* @private
51+
* @param {Benchmark} b - benchmark instance
52+
*/
53+
function benchmark( b ) {
54+
var out;
55+
var i;
56+
57+
b.tic();
58+
for ( i = 0; i < b.iterations; i++ ) {
59+
out = map( x, identity );
60+
if ( isnan( out[ i%len ] ) ) {
61+
b.fail( 'should not return NaN' );
62+
}
63+
}
64+
b.toc();
65+
66+
if ( isnan( out[ i % len ] ) ) {
67+
b.fail( 'should not return NaN' );
68+
}
69+
b.pass( 'benchmark finished' );
70+
b.end();
71+
}
72+
}
73+
74+
75+
// MAIN //
76+
77+
/**
78+
* Main execution sequence.
79+
*
80+
* @private
81+
*/
82+
function main() {
83+
var len;
84+
var min;
85+
var max;
86+
var f;
87+
var i;
88+
89+
min = 1; // 10^min
90+
max = 6; // 10^max
91+
92+
for ( i = min; i <= max; i++ ) {
93+
len = pow( 10, i );
94+
95+
f = createBenchmark( len );
96+
bench( pkg+':dtype=generic,len='+len, f );
97+
}
98+
}
99+
100+
main();

0 commit comments

Comments
 (0)