-
-
Notifications
You must be signed in to change notification settings - Fork 907
feat: add array/base/pluck
#5376
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
Open
headlessNode
wants to merge
6
commits into
stdlib-js:develop
Choose a base branch
from
headlessNode:array-base-pluck
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
cfab7bf
feat: add array/base/pluck
headlessNode 172380f
bench: apply review suggestion
headlessNode 9b01b13
refactor: apply suggestions from code review
headlessNode 67b6a3a
refactor: add missing typescript tests
headlessNode 2acd65b
refactor: main loop and tests
headlessNode 42ad68c
test: add missing tests
headlessNode 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,211 @@ | ||
<!-- | ||
|
||
@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. | ||
|
||
--> | ||
|
||
# pluck | ||
|
||
> Extract a property value from elements in an input object 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 pluck = require( '@stdlib/array/base/pluck' ); | ||
``` | ||
|
||
#### pluck( x, prop ) | ||
|
||
Extracts a property value from elements in an input object array and assigns results to elements in a new output array. | ||
|
||
```javascript | ||
var x = [ | ||
{ | ||
'a': 1 | ||
}, | ||
{ | ||
'a': 2 | ||
}, | ||
{ | ||
'a': 3 | ||
} | ||
]; | ||
|
||
var y = pluck( x, 'a' ); | ||
// returns [ 1, 2, 3 ] | ||
``` | ||
|
||
The function accepts the following arguments: | ||
|
||
- **x**: input array. | ||
- **prop**: property to extract values from. | ||
|
||
#### pluck.assign( x, prop, out, stride, offset ) | ||
|
||
Extracts a property value from elements in an input object array and assigns results to elements in an output array. | ||
|
||
```javascript | ||
var x = [ | ||
{ | ||
'a': 1 | ||
}, | ||
{ | ||
'a': 2 | ||
}, | ||
{ | ||
'a': 3 | ||
} | ||
]; | ||
|
||
var out = [ 0, 0, 0 ]; | ||
var y = pluck.assign( x, 'a', out, 1, 0 ); | ||
// returns [ 1, 2, 3 ] | ||
``` | ||
|
||
The function accepts the following arguments: | ||
|
||
- **x**: input array. | ||
- **prop**: property to extract values from. | ||
headlessNode marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
- **out**: output array. | ||
- **stride**: stride length for output array. | ||
- **offset**: starting index for output array. | ||
|
||
</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 | ||
|
||
- The function skips `undefined` and `null` array elements. | ||
|
||
<!-- eslint-disable object-property-newline, object-curly-newline, object-curly-spacing --> | ||
|
||
```javascript | ||
var arr = [ | ||
{ 'a': 1, 'b': 2 }, | ||
null, | ||
void 0, | ||
{ 'a': 0.5, 'b': 3 } | ||
]; | ||
|
||
var out = pluck( arr, 'a' ); | ||
// returns [ 1, , , 0.5 ] | ||
``` | ||
|
||
- Extracted values are **not** cloned. | ||
|
||
<!-- eslint-disable object-property-newline, object-curly-newline, object-curly-spacing --> | ||
|
||
```javascript | ||
var arr = [ | ||
{ 'a': { 'b': 2 } }, | ||
{ 'a': { 'b': 3 } } | ||
]; | ||
|
||
var out = pluck( arr, 'a' ); | ||
// returns [ { 'b': 2 }, { 'b': 3 } ] | ||
|
||
var bool = ( arr[ 0 ].a === out[ 0 ] ); | ||
// returns true | ||
``` | ||
|
||
- 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 --> | ||
|
||
<!-- Package usage examples. --> | ||
|
||
<section class="examples"> | ||
|
||
## Examples | ||
|
||
<!-- eslint no-undef: "error" --> | ||
|
||
```javascript | ||
var randu = require( '@stdlib/random/base/randu' ); | ||
var round = require( '@stdlib/math/base/special/round' ); | ||
var pluck = require( '@stdlib/array/base/pluck' ); | ||
|
||
var arr; | ||
var tmp; | ||
var out; | ||
var i; | ||
var j; | ||
|
||
// Generate a 100x5 2d-array... | ||
arr = new Array( 100 ); | ||
for ( i = 0; i < arr.length; i++ ) { | ||
tmp = new Array( 5 ); | ||
for ( j = 0; j < tmp.length; j++ ) { | ||
tmp[ j ] = round( randu()*100.0*(j+1.0) ); | ||
} | ||
arr[ i ] = tmp; | ||
} | ||
|
||
// Pluck the 3rd column: | ||
out = pluck( arr, 2 ); | ||
console.log( out ); | ||
``` | ||
|
||
</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. Do not manually edit this section, as it is automatically populated. --> | ||
|
||
<section class="links"> | ||
|
||
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor | ||
|
||
</section> | ||
|
||
<!-- /.links --> |
64 changes: 64 additions & 0 deletions
64
lib/node_modules/@stdlib/array/base/pluck/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,64 @@ | ||
/** | ||
* @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 isArray = require( '@stdlib/assert/is-array' ); | ||
var randu = require( '@stdlib/random/base/randu' ); | ||
var round = require( '@stdlib/math/base/special/round' ); | ||
var pkg = require( './../package.json' ).name; | ||
var pluck = require( './../lib' ); | ||
|
||
|
||
// MAIN // | ||
|
||
bench( pkg+':assign', function benchmark( b ) { | ||
var out; | ||
var tmp; | ||
var x; | ||
var i; | ||
var j; | ||
|
||
x = new Array( 100 ); | ||
for ( i = 0; i < x.length; i++ ) { | ||
tmp = new Array( 5 ); | ||
for ( j = 0; j < tmp.length; j++ ) { | ||
tmp[ j ] = round( randu()*100.0*(j+1.0) ); | ||
} | ||
x[ i ] = tmp; | ||
} | ||
out = new Array( x.length ); | ||
|
||
b.tic(); | ||
for ( i = 0; i < b.iterations; i++ ) { | ||
x[ i % x.length ].y = randu(); | ||
j = pluck.assign( x, 'y', out, 1, 0 ); | ||
if ( typeof j !== 'object' ) { | ||
b.fail( 'should return an array' ); | ||
} | ||
} | ||
b.toc(); | ||
if ( !isArray( j ) ) { | ||
b.fail( 'should return an array' ); | ||
} | ||
b.pass( 'benchmark finished' ); | ||
b.end(); | ||
}); | ||
headlessNode marked this conversation as resolved.
Show resolved
Hide resolved
|
62 changes: 62 additions & 0 deletions
62
lib/node_modules/@stdlib/array/base/pluck/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,62 @@ | ||
/** | ||
* @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 isArray = require( '@stdlib/assert/is-array' ); | ||
var randu = require( '@stdlib/random/base/randu' ); | ||
var round = require( '@stdlib/math/base/special/round' ); | ||
var pkg = require( './../package.json' ).name; | ||
var pluck = require( './../lib' ); | ||
|
||
|
||
// MAIN // | ||
|
||
bench( pkg, function benchmark( b ) { | ||
var out; | ||
var tmp; | ||
var x; | ||
var i; | ||
var j; | ||
|
||
x = new Array( 100 ); | ||
for ( i = 0; i < x.length; i++ ) { | ||
tmp = new Array( 5 ); | ||
for ( j = 0; j < tmp.length; j++ ) { | ||
tmp[ j ] = round( randu()*100.0*(j+1.0) ); | ||
} | ||
x[ i ] = tmp; | ||
} | ||
b.tic(); | ||
for ( i = 0; i < b.iterations; i++ ) { | ||
x[ i % x.length ][ i % 5 ] = randu(); | ||
out = pluck( x, 2 ); | ||
if ( typeof out !== 'object' ) { | ||
b.fail( 'should return an array' ); | ||
} | ||
} | ||
b.toc(); | ||
if ( !isArray( out ) ) { | ||
b.fail( 'should return an array' ); | ||
} | ||
b.pass( 'benchmark finished' ); | ||
b.end(); | ||
}); |
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.