Skip to content

Commit 0c87f3e

Browse files
vr-varadPlaneshifter
authored andcommitted
feat: add array/base/take-map
PR-URL: stdlib-js#1349 Closes: stdlib-js#1321 --------- Signed-off-by: Philipp Burckhardt <[email protected]> Co-authored-by: Philipp Burckhardt <[email protected]> Reviewed-by: Athan Reines <[email protected]> Reviewed-by: Pranav Goswami <[email protected]> Reviewed-by: Philipp Burckhardt <[email protected]>
1 parent 3ce6a63 commit 0c87f3e

File tree

15 files changed

+2289
-0
lines changed

15 files changed

+2289
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2024 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+
# takeMap
22+
23+
> Take elements from an array and return a new array after applying a mapping function.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var takeMap = require( '@stdlib/array/base/take-map' );
31+
```
32+
33+
### takeMap( x, indices, mode, clbk )
34+
35+
Takes elements from an array and returns a new array after applying a mapping function.
36+
37+
```javascript
38+
var x = [ 1, 2, 3, 4 ];
39+
40+
function customMapping( value ) {
41+
return value * 2;
42+
}
43+
44+
var y = takeMap( x, [ 1, 3 ], 'throw', customMapping );
45+
// returns [ 4, 8 ]
46+
```
47+
48+
The function supports the following parameters:
49+
50+
- **x**: input array.
51+
- **indices**: list of indices.
52+
- **mode**: index [mode][@stdlib/ndarray/base/ind].
53+
- **clbk**: function to apply.
54+
55+
### takeMap.assign( x, indices, mode, out, stride, offset, clbk )
56+
57+
> Takes elements from an array and assigns the values to elements in a provided output array.
58+
59+
```javascript
60+
var x = [ 1, 2, 3, 4 ];
61+
62+
var out = [ 0, 0, 0, 0, 0, 0 ];
63+
var indices = [ 0, 0, 1, 1, 3, 3 ];
64+
65+
function clbk( val ) {
66+
return val * 2;
67+
}
68+
69+
var arr = takeMap.assign( x, indices, 'throw', out, -1, out.length-1, clbk );
70+
// returns [ 8, 8, 4, 4, 2, 2 ]
71+
72+
var bool = ( arr === out );
73+
// returns true
74+
```
75+
76+
The function supports the following parameters:
77+
78+
- **x**: input array.
79+
- **indices**: list of indices.
80+
- **mode**: index [mode][@stdlib/ndarray/base/ind].
81+
- **out**: output array.
82+
- **stride**: output array stride.
83+
- **offset**: output array offset.
84+
- **clbk**: callback function.
85+
86+
</section>
87+
88+
<!-- /.usage -->
89+
90+
<section class="notes">
91+
92+
</section>
93+
94+
<!-- /.notes -->
95+
96+
<section class="examples">
97+
98+
## Examples
99+
100+
<!-- eslint no-undef: "error" -->
101+
102+
```javascript
103+
var filledBy = require( '@stdlib/array/base/filled-by' );
104+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
105+
var linspace = require( '@stdlib/array/base/linspace' );
106+
var takeMap = require( '@stdlib/array/base/take-map' );
107+
108+
// Generate a linearly spaced array:
109+
var x = linspace( 0, 100, 11 );
110+
console.log( x );
111+
112+
// Generate an array of random indices:
113+
var N = discreteUniform( 5, 15 );
114+
var indices = filledBy( N, discreteUniform.factory( 0, x.length-1 ) );
115+
console.log( indices );
116+
117+
// Define a mapping function (e.g., square the value):
118+
function square( val ) {
119+
return val * val;
120+
}
121+
122+
// Take a random sample of elements from `x` and apply the mapping function:
123+
var y = takeMap( x, indices, 'throw', square );
124+
console.log( y );
125+
```
126+
127+
</section>
128+
129+
<!-- /.examples -->
130+
131+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
132+
133+
<section class="related">
134+
135+
</section>
136+
137+
<!-- /.related -->
138+
139+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
140+
141+
<section class="links">
142+
143+
[@stdlib/ndarray/base/ind]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/ind
144+
145+
</section>
146+
147+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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 pow = require( '@stdlib/math/base/special/pow' );
25+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
26+
var zeros = require( '@stdlib/array/zeros' );
27+
var isArray = require( '@stdlib/assert/is-array' );
28+
var pkg = require( './../package.json' ).name;
29+
var takeMap = 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 out;
43+
var idx;
44+
45+
idx = discreteUniform( len, 0, 3, {
46+
'dtype': 'generic'
47+
});
48+
out = zeros( len, 'generic' );
49+
50+
return benchmark;
51+
52+
/**
53+
* Benchmark function.
54+
*
55+
* @private
56+
* @param {Benchmark} b - benchmark instance
57+
*/
58+
function benchmark( b ) {
59+
var x;
60+
var v;
61+
var i;
62+
63+
x = [ 1, 2, 3, 4 ];
64+
function clbk( val ) {
65+
return val;
66+
}
67+
68+
b.tic();
69+
for ( i = 0; i < b.iterations; i++ ) {
70+
v = takeMap.assign( x, idx, 'throw', out, 1, 0, clbk );
71+
if ( typeof v !== 'object' ) {
72+
b.fail( 'should return an array' );
73+
}
74+
}
75+
b.toc();
76+
if ( !isArray( v ) ) {
77+
b.fail( 'should return an array' );
78+
}
79+
b.pass( 'benchmark finished' );
80+
b.end();
81+
}
82+
}
83+
84+
85+
// MAIN //
86+
87+
/**
88+
* Main execution sequence.
89+
*
90+
* @private
91+
*/
92+
function main() {
93+
var len;
94+
var min;
95+
var max;
96+
var f;
97+
var i;
98+
99+
min = 1; // 10^min
100+
max = 6; // 10^max
101+
102+
for ( i = min; i <= max; i++ ) {
103+
len = pow( 10, i );
104+
f = createBenchmark( len );
105+
bench( pkg+':assign:len='+len, f );
106+
}
107+
}
108+
109+
main();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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 isArray = require( '@stdlib/assert/is-array' );
25+
var zeroTo = require( '@stdlib/array/base/zero-to' );
26+
var pkg = require( './../package.json' ).name;
27+
var takeMap = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg+'::copy:len=100', function benchmark( b ) {
33+
var x;
34+
var i;
35+
var v;
36+
37+
x = zeroTo( 100 );
38+
39+
function clbk( val ) {
40+
return val;
41+
}
42+
43+
b.tic();
44+
for ( i = 0; i < b.iterations; i++ ) {
45+
v = takeMap( x, x, 'throw', clbk );
46+
if ( typeof v !== 'object' ) {
47+
b.fail( 'should return an array' );
48+
}
49+
}
50+
b.toc();
51+
if ( !isArray( v ) ) {
52+
b.fail( 'should return an array' );
53+
}
54+
b.pass( 'benchmark finished' );
55+
b.end();
56+
});

0 commit comments

Comments
 (0)