Skip to content

Commit 3dc813f

Browse files
committed
feat: add array/base/remove-at
1 parent 96c7ddf commit 3dc813f

File tree

10 files changed

+730
-0
lines changed

10 files changed

+730
-0
lines changed
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
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+
# removeAt
22+
23+
> Remove an element from an 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 removeAt = require( '@stdlib/array/base/remove-at' );
41+
```
42+
43+
#### removeAt( x, index )
44+
45+
Removes an element from an array.
46+
47+
```javascript
48+
var x = [ 1, 1, 2, 3, 3 ];
49+
50+
var y = removeAt( x, -3 );
51+
// returns [ 1, 1, 3, 3 ]
52+
53+
var bool = ( x === y );
54+
// returns true
55+
```
56+
57+
The function accepts the following arguments:
58+
59+
- **x**: an input array.
60+
- **index**: element index.
61+
62+
</section>
63+
64+
<!-- /.usage -->
65+
66+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
67+
68+
<section class="notes">
69+
70+
## Notes
71+
72+
- Negative indices are resolved relative to the last array element, with the last element corresponding to `-1`.
73+
- If provided out-of-bounds indices, the function returns the input array unchanged; otherwise, the function mutates the input array.
74+
75+
</section>
76+
77+
<!-- /.notes -->
78+
79+
<!-- Package usage examples. -->
80+
81+
<section class="examples">
82+
83+
## Examples
84+
85+
<!-- eslint no-undef: "error" -->
86+
87+
```javascript
88+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
89+
var randi = require( '@stdlib/random/base/discrete-uniform' );
90+
var removeAt = require( '@stdlib/array/base/remove-at' );
91+
92+
// Create an array of random numbers:
93+
var x = discreteUniform( 30, 0, 5, {
94+
'dtype': 'generic'
95+
});
96+
// returns [...]
97+
98+
// Remove a random element:
99+
var y = removeAt( x, randi( 0, x.length-1 ) );
100+
// returns [...]
101+
102+
console.log( y );
103+
```
104+
105+
</section>
106+
107+
<!-- /.examples -->
108+
109+
<!-- 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. -->
110+
111+
<section class="references">
112+
113+
</section>
114+
115+
<!-- /.references -->
116+
117+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
118+
119+
<section class="related">
120+
121+
</section>
122+
123+
<!-- /.related -->
124+
125+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
126+
127+
<section class="links">
128+
129+
</section>
130+
131+
<!-- /.links -->
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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 isArray = require( '@stdlib/assert/is-array' );
26+
var zeroTo = require( '@stdlib/array/base/zero-to' );
27+
var pkg = require( './../package.json' ).name;
28+
var removeAt = require( './../lib' );
29+
30+
31+
// FUNCTIONS //
32+
33+
/**
34+
* Creates a benchmark function.
35+
*
36+
* @private
37+
* @param {PositiveInteger} len - array length
38+
* @returns {Function} benchmark function
39+
*/
40+
function createBenchmark( len ) {
41+
return benchmark;
42+
43+
/**
44+
* Benchmark function.
45+
*
46+
* @private
47+
* @param {Benchmark} b - benchmark instance
48+
*/
49+
function benchmark( b ) {
50+
var out;
51+
var x;
52+
var i;
53+
54+
x = zeroTo( b.iterations+1 );
55+
56+
b.tic();
57+
for ( i = 0; i < b.iterations; i++ ) {
58+
out = removeAt( x, -1 );
59+
if ( out.length === len ) {
60+
b.fail( 'unexpected length' );
61+
}
62+
}
63+
b.toc();
64+
if ( !isArray( out ) ) {
65+
b.fail( 'should return an array' );
66+
}
67+
b.pass( 'benchmark finished' );
68+
b.end();
69+
}
70+
}
71+
72+
73+
// MAIN //
74+
75+
/**
76+
* Main execution sequence.
77+
*
78+
* @private
79+
*/
80+
function main() {
81+
var len;
82+
var min;
83+
var max;
84+
var f;
85+
var i;
86+
87+
min = 1; // 10^min
88+
max = 6; // 10^max
89+
90+
for ( i = min; i <= max; i++ ) {
91+
len = pow( 10, i );
92+
93+
f = createBenchmark( len );
94+
bench( pkg+':len='+len, f );
95+
}
96+
}
97+
98+
main();
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
{{alias}}( x, index )
3+
Removes an element from an array.
4+
5+
Negative indices are resolved relative to the last array element, with the
6+
last element corresponding to `-1`.
7+
8+
If provided an out-of-bounds index, the function returns the input array
9+
unchanged; otherwise, the function mutates the input array.
10+
11+
Parameters
12+
----------
13+
x: Array
14+
Input array.
15+
16+
index: integer
17+
Element index.
18+
19+
Returns
20+
-------
21+
out: Array
22+
Input array.
23+
24+
Examples
25+
--------
26+
> var x = [ 1, 1, 2, 3, 3 ];
27+
> var out = {{alias}}( x, -3 )
28+
[ 1, 1, 3, 3 ]
29+
> var bool = ( out === x )
30+
true
31+
32+
See Also
33+
--------
34+
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
// TypeScript Version: 4.1
20+
21+
/**
22+
* Removes an element from an array.
23+
*
24+
* ## Notes
25+
*
26+
* - The function mutates the input array.
27+
*
28+
* @param x - input array
29+
* @param index - element index
30+
* @returns input array
31+
*
32+
* @example
33+
* var x = [ 1, 1, 2, 3, 3 ];
34+
*
35+
* var y = removeAt( x, -2 );
36+
* // returns [ 1, 1, 3, 3 ]
37+
*
38+
* var bool = ( x === y );
39+
* // returns true
40+
*/
41+
declare function removeAt<T = unknown>( x: Array<T>, index: number ): Array<T>;
42+
43+
44+
// EXPORTS //
45+
46+
export = removeAt;
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
import removeAt = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns an array...
25+
{
26+
removeAt( [ 1, 2, 3 ], -1 ); // $ExpectType number[]
27+
}
28+
29+
// The compiler throws an error if the function is provided a first argument which is not an array...
30+
{
31+
removeAt( '5', -1 ); // $ExpectError
32+
removeAt( 5, -1 ); // $ExpectError
33+
removeAt( true, -1 ); // $ExpectError
34+
removeAt( false, -1 ); // $ExpectError
35+
removeAt( null, -1 ); // $ExpectError
36+
removeAt( void 0, -1 ); // $ExpectError
37+
removeAt( {}, -1 ); // $ExpectError
38+
removeAt( ( x: number ): number => x, -1 ); // $ExpectError
39+
}
40+
41+
// The compiler throws an error if the function is provided a second argument which is not a number...
42+
{
43+
const x = [ 1, 2, 3 ];
44+
45+
removeAt( x, '5' ); // $ExpectError
46+
removeAt( x, true ); // $ExpectError
47+
removeAt( x, false ); // $ExpectError
48+
removeAt( x, null ); // $ExpectError
49+
removeAt( x, void 0 ); // $ExpectError
50+
removeAt( x, {} ); // $ExpectError
51+
removeAt( x, [] ); // $ExpectError
52+
removeAt( x, ( x: number ): number => x ); // $ExpectError
53+
}
54+
55+
// The compiler throws an error if the function is provided an unsupported number of arguments...
56+
{
57+
const x = [ 1, 2, 3 ];
58+
59+
removeAt(); // $ExpectError
60+
removeAt( x ); // $ExpectError
61+
removeAt( x, -1, {} ); // $ExpectError
62+
}

0 commit comments

Comments
 (0)