-
-
Notifications
You must be signed in to change notification settings - Fork 907
feat: add array/base/map
#5346
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
feat: add array/base/map
#5346
Changes from 3 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
4f479e6
feat: add array/base/map
headlessNode 2ea05a5
docs: apply suggestions from code review
headlessNode bc93f68
refactor: apply suggestions from code review
headlessNode 79409d3
refactor: apply suggestions from code review
headlessNode 6173534
refactor: apply suggestions from code review
headlessNode 968cc8b
docs: apply suggestions from code review
headlessNode b208157
refactor: apply suggestions from code review
headlessNode f24a5d3
Merge branch 'array-base-map' of https://github.com/headlessNode/stdl…
headlessNode dde5891
refactor: apply suggestions from code review
headlessNode 65d27ec
refactor: apply suggestions from code review
headlessNode fdc476c
Merge branch 'develop' of https://github.com/stdlib-js/stdlib into pr…
kgryte e756b5b
chore: clean-up
kgryte File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,191 @@ | ||
<!-- | ||
|
||
@license Apache-2.0 | ||
|
||
Copyright (c) 2025 The Stdlib Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
|
||
--> | ||
|
||
# map | ||
|
||
> Apply a callback function to elements in an input array and assign results to elements in a new output array. | ||
|
||
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> | ||
|
||
<section class="intro"> | ||
|
||
</section> | ||
|
||
<!-- /.intro --> | ||
|
||
<!-- Package usage documentation. --> | ||
|
||
<section class="usage"> | ||
|
||
## Usage | ||
|
||
```javascript | ||
var map = require( '@stdlib/array/base/map' ); | ||
``` | ||
|
||
#### map( x, fcn\[, thisArg] ) | ||
|
||
Applies a callback function to elements in an input array and assigns results to elements in a new output array. | ||
|
||
```javascript | ||
var naryFunction = require( '@stdlib/utils/nary-function' ); | ||
var abs = require( '@stdlib/math/base/special/abs' ); | ||
|
||
var x = [ -1.0, -2.0, -3.0, -4.0 ]; | ||
|
||
var y = map( x, naryFunction( abs, 1 ) ); | ||
// returns [ 1.0, 2.0, 3.0, 4.0 ] | ||
``` | ||
|
||
The function accepts the following arguments: | ||
|
||
- **x**: input array. | ||
- **fcn**: callback function. | ||
- **thisArg**: callback execution context (_optional_). | ||
|
||
To set the callback function's execution context, provide a `thisArg`. | ||
|
||
<!-- eslint-disable no-invalid-this --> | ||
|
||
```javascript | ||
function count( x ) { | ||
this.count += 1; | ||
return x; | ||
} | ||
|
||
var x = [ 1.0, 2.0, 3.0, 4.0 ]; | ||
|
||
var ctx = { | ||
'count': 0 | ||
}; | ||
|
||
var y = map( x, count, ctx ); | ||
// returns [ 1.0, 2.0, 3.0, 4.0 ] | ||
|
||
var v = ctx.count; | ||
// returns 4 | ||
``` | ||
|
||
The callback function is provided the following arguments: | ||
|
||
- **value**: current array element. | ||
- **index**: current element index. | ||
- **arr**: input array. | ||
|
||
#### map.assign( x, y, strideX, offsetX, fcn\[, thisArg] ) | ||
|
||
Applies a callback function to elements in an input array and assigns results to elements in an output array. | ||
|
||
```javascript | ||
var naryFunction = require( '@stdlib/utils/nary-function' ); | ||
var zeros = require( '@stdlib/array/base/zeros' ); | ||
var abs = require( '@stdlib/math/base/special/abs' ); | ||
|
||
var x = [ -1.0, -2.0, -3.0, -4.0 ]; | ||
|
||
var y = zeros( x.length ); | ||
|
||
var out = map.assign( x, y, 1, 0, naryFunction( abs, 1 ) ); | ||
// returns [ 1.0, 2.0, 3.0, 4.0 ] | ||
|
||
var bool = ( out === y ); | ||
// returns true | ||
``` | ||
|
||
The function accepts the following arguments: | ||
|
||
- **x**: input array. | ||
- **y**: output array. | ||
- **strideX**: stride length for x. | ||
- **offsetX**: starting index for x. | ||
- **fcn**: callback function. | ||
- **thisArg**: callback execution context (_optional_). | ||
|
||
</section> | ||
|
||
<!-- /.usage --> | ||
|
||
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||
|
||
<section class="notes"> | ||
|
||
## Notes | ||
|
||
- 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: | ||
|
||
```text | ||
x.map( fcn, thisArg ) | ||
``` | ||
|
||
- 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]). | ||
|
||
</section> | ||
|
||
<!-- /.notes --> | ||
|
||
<section class="examples"> | ||
|
||
## Examples | ||
|
||
<!-- eslint no-undef: "error" --> | ||
|
||
```javascript | ||
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); | ||
var naryFunction = require( '@stdlib/utils/nary-function' ); | ||
var abs = require( '@stdlib/math/base/special/abs' ); | ||
var map = require( '@stdlib/array/base/map' ); | ||
|
||
var x = discreteUniform( 10, -10, 10, { | ||
'dtype': 'float64' | ||
}); | ||
|
||
var y = map( x, naryFunction( abs, 1 ) ); | ||
console.log( y ); | ||
``` | ||
|
||
</section> | ||
|
||
<!-- /.examples --> | ||
|
||
<!-- 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. --> | ||
|
||
<section class="references"> | ||
|
||
</section> | ||
|
||
<!-- /.references --> | ||
|
||
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> | ||
|
||
<section class="related"> | ||
|
||
</section> | ||
|
||
<!-- /.related --> | ||
|
||
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||
|
||
<section class="links"> | ||
|
||
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor | ||
|
||
</section> | ||
|
||
<!-- /.links --> |
106 changes: 106 additions & 0 deletions
106
lib/node_modules/@stdlib/array/base/map/benchmark/benchmark.assign.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/** | ||
* @license Apache-2.0 | ||
* | ||
* Copyright (c) 2025 The Stdlib Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
// MODULES // | ||
|
||
var bench = require( '@stdlib/bench' ); | ||
var isnan = require( '@stdlib/math/base/assert/is-nan' ); | ||
var pow = require( '@stdlib/math/base/special/pow' ); | ||
var uniform = require( '@stdlib/random/array/uniform' ); | ||
var identity = require( '@stdlib/math/base/special/identity' ); | ||
var zeros = require( '@stdlib/array/base/zeros' ); | ||
var pkg = require( './../package.json' ).name; | ||
var map = require( './../lib' ).assign; | ||
|
||
|
||
// FUNCTIONS // | ||
|
||
/** | ||
* Creates a benchmark function. | ||
* | ||
* @private | ||
* @param {PositiveInteger} len - array length | ||
* @returns {Function} benchmark function | ||
*/ | ||
function createBenchmark( len ) { | ||
var x; | ||
var y; | ||
|
||
x = uniform( len, -100.0, 100.0, { | ||
'dtype': 'generic' | ||
}); | ||
y = zeros( len ); | ||
|
||
return benchmark; | ||
|
||
/** | ||
* Benchmark function. | ||
* | ||
* @private | ||
* @param {Benchmark} b - benchmark instance | ||
*/ | ||
function benchmark( b ) { | ||
var out; | ||
var i; | ||
|
||
b.tic(); | ||
for ( i = 0; i < b.iterations; i++ ) { | ||
out = map( x, y, identity ); | ||
headlessNode marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
if ( isnan( out[ i%len ] ) ) { | ||
b.fail( 'should not return NaN' ); | ||
} | ||
} | ||
b.toc(); | ||
|
||
if ( isnan( out[ i % len ] ) ) { | ||
b.fail( 'should not return NaN' ); | ||
} | ||
b.pass( 'benchmark finished' ); | ||
b.end(); | ||
} | ||
} | ||
|
||
|
||
// MAIN // | ||
|
||
/** | ||
* Main execution sequence. | ||
* | ||
* @private | ||
*/ | ||
function main() { | ||
var len; | ||
var min; | ||
var max; | ||
var f; | ||
var i; | ||
|
||
min = 1; // 10^min | ||
max = 6; // 10^max | ||
|
||
for ( i = min; i <= max; i++ ) { | ||
len = pow( 10, i ); | ||
|
||
f = createBenchmark( len ); | ||
bench( pkg+':assign:dtype=generic,len='+len, f ); | ||
} | ||
} | ||
|
||
main(); |
100 changes: 100 additions & 0 deletions
100
lib/node_modules/@stdlib/array/base/map/benchmark/benchmark.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/** | ||
* @license Apache-2.0 | ||
* | ||
* Copyright (c) 2025 The Stdlib Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
// MODULES // | ||
|
||
var bench = require( '@stdlib/bench' ); | ||
var isnan = require( '@stdlib/math/base/assert/is-nan' ); | ||
var pow = require( '@stdlib/math/base/special/pow' ); | ||
var uniform = require( '@stdlib/random/array/uniform' ); | ||
var identity = require( '@stdlib/math/base/special/identity' ); | ||
var pkg = require( './../package.json' ).name; | ||
var map = require( './../lib' ); | ||
|
||
|
||
// FUNCTIONS // | ||
|
||
/** | ||
* Creates a benchmark function. | ||
* | ||
* @private | ||
* @param {PositiveInteger} len - array length | ||
* @returns {Function} benchmark function | ||
*/ | ||
function createBenchmark( len ) { | ||
var x = uniform( len, -100.0, 100.0, { | ||
'dtype': 'generic' | ||
}); | ||
return benchmark; | ||
|
||
/** | ||
* Benchmark function. | ||
* | ||
* @private | ||
* @param {Benchmark} b - benchmark instance | ||
*/ | ||
function benchmark( b ) { | ||
var out; | ||
var i; | ||
|
||
b.tic(); | ||
for ( i = 0; i < b.iterations; i++ ) { | ||
out = map( x, identity ); | ||
if ( isnan( out[ i%len ] ) ) { | ||
b.fail( 'should not return NaN' ); | ||
} | ||
} | ||
b.toc(); | ||
|
||
if ( isnan( out[ i % len ] ) ) { | ||
b.fail( 'should not return NaN' ); | ||
} | ||
b.pass( 'benchmark finished' ); | ||
b.end(); | ||
} | ||
} | ||
|
||
|
||
// MAIN // | ||
|
||
/** | ||
* Main execution sequence. | ||
* | ||
* @private | ||
*/ | ||
function main() { | ||
var len; | ||
var min; | ||
var max; | ||
var f; | ||
var i; | ||
|
||
min = 1; // 10^min | ||
max = 6; // 10^max | ||
|
||
for ( i = min; i <= max; i++ ) { | ||
len = pow( 10, i ); | ||
|
||
f = createBenchmark( len ); | ||
bench( pkg+':dtype=generic,len='+len, f ); | ||
} | ||
} | ||
|
||
main(); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.