Skip to content

Commit 3497425

Browse files
InfinageNaresh Jagadeesankgryte
authored
feat: add array/base/last
PR-URL: #1023 Closes: #858 Co-authored-by: Naresh Jagadeesan <naresh.naresh000@gmail.com> Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent 32cd19b commit 3497425

File tree

10 files changed

+616
-0
lines changed

10 files changed

+616
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2022 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+
# last
22+
23+
> Return the last element of an array-like object.
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 last = require( '@stdlib/array/base/last' );
41+
```
42+
43+
#### last( x )
44+
45+
Returns the last element of an array-like object.
46+
47+
```javascript
48+
var x = [ 1, 2, 3 ];
49+
50+
var out = last( x );
51+
// returns 3
52+
```
53+
54+
</section>
55+
56+
<!-- /.usage -->
57+
58+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
59+
60+
<section class="notes">
61+
62+
</section>
63+
64+
<!-- /.notes -->
65+
66+
<!-- Package usage examples. -->
67+
68+
<section class="examples">
69+
70+
## Examples
71+
72+
<!-- eslint no-undef: "error" -->
73+
74+
```javascript
75+
var Complex64Array = require( '@stdlib/array/complex64' );
76+
var realf = require( '@stdlib/complex/realf' );
77+
var imagf = require( '@stdlib/complex/imagf' );
78+
var last = require( '@stdlib/array/base/last' );
79+
80+
// Create a complex number array:
81+
var arr = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
82+
83+
// Return the last element:
84+
var out = last( arr );
85+
// returns <Complex64>
86+
87+
var re = realf( out );
88+
// returns 5.0
89+
90+
var im = imagf( out );
91+
// returns 6.0
92+
93+
console.log( '%d + %di', re, im );
94+
// => '5 + 6i'
95+
```
96+
97+
</section>
98+
99+
<!-- /.examples -->
100+
101+
<!-- 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. -->
102+
103+
<section class="references">
104+
105+
</section>
106+
107+
<!-- /.references -->
108+
109+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
110+
111+
<section class="related">
112+
113+
</section>
114+
115+
<!-- /.related -->
116+
117+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
118+
119+
<section class="links">
120+
121+
</section>
122+
123+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2022 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 discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory;
25+
var Complex64Array = require( '@stdlib/array/complex64' );
26+
var pow = require( '@stdlib/math/base/special/pow' );
27+
var filledBy = require( '@stdlib/array/filled-by' );
28+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
29+
var pkg = require( './../package.json' ).name;
30+
var last = require( './../lib' );
31+
32+
33+
// VARIABLES //
34+
35+
var rand = discreteUniform( 0, 127 );
36+
37+
38+
// FUNCTIONS //
39+
40+
/**
41+
* Creates a benchmark function.
42+
*
43+
* @private
44+
* @param {PositiveInteger} len - array length
45+
* @returns {Function} benchmark function
46+
*/
47+
function createBenchmark( len ) {
48+
var buf = filledBy( len*2, 'float32', rand );
49+
var arr = new Complex64Array( buf.buffer );
50+
return benchmark;
51+
52+
/**
53+
* Benchmark function.
54+
*
55+
* @private
56+
* @param {Benchmark} b - benchmark instance
57+
*/
58+
function benchmark( b ) {
59+
var out;
60+
var i;
61+
62+
b.tic();
63+
for ( i = 0; i < b.iterations; i++ ) {
64+
out = last( arr );
65+
if ( isnanf( out ) ) {
66+
b.fail( 'should not return NaN' );
67+
}
68+
}
69+
b.toc();
70+
if ( isnanf( out ) ) {
71+
b.fail( 'should not return NaN' );
72+
}
73+
b.pass( 'benchmark finished' );
74+
b.end();
75+
}
76+
}
77+
78+
79+
// MAIN //
80+
81+
/**
82+
* Main execution sequence.
83+
*
84+
* @private
85+
*/
86+
function main() {
87+
var len;
88+
var min;
89+
var max;
90+
var f;
91+
var i;
92+
93+
min = 1; // 10^min
94+
max = 6; // 10^max
95+
96+
for ( i = min; i <= max; i++ ) {
97+
len = pow( 10, i );
98+
99+
f = createBenchmark( len );
100+
bench( pkg+':len='+len, f );
101+
}
102+
}
103+
104+
main();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
{{alias}}( arr )
3+
Returns the last element of an array-like object.
4+
5+
Parameters
6+
----------
7+
arr: ArrayLikeObject
8+
Input array.
9+
10+
Returns
11+
-------
12+
out: any
13+
Last element.
14+
15+
Examples
16+
--------
17+
> var out = {{alias}}( [ 1, 2, 3 ] )
18+
3
19+
20+
See Also
21+
--------
22+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2022 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: 2.0
20+
21+
/// <reference types="@stdlib/types"/>
22+
23+
import { Collection } from '@stdlib/types/object';
24+
25+
/**
26+
* Returns the last element of an array-like object.
27+
*
28+
* @param arr - input array
29+
* @returns last element
30+
*
31+
* @example
32+
* var arr = [ 1, 2, 3 ];
33+
*
34+
* var out = last( x );
35+
* // returns 3
36+
*/
37+
declare function last( arr: Collection ): any;
38+
39+
40+
// EXPORTS //
41+
42+
export = last;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2022 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 last = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns the last element in array...
25+
{
26+
last( [ 1, 2, 3 ] ); // $ExpectType any
27+
}
28+
29+
// The compiler throws an error if the function is provided an argument which is not a collection...
30+
{
31+
last( 5 ); // $ExpectError
32+
last( true ); // $ExpectError
33+
last( false ); // $ExpectError
34+
last( null ); // $ExpectError
35+
last( {} ); // $ExpectError
36+
}
37+
38+
// The compiler throws an error if the function is provided an unsupported number of arguments...
39+
{
40+
last(); // $ExpectError
41+
last( [ 1, 2, 3 ], 2 ); // $ExpectError
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2022 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+
var Complex64Array = require( '@stdlib/array/complex64' );
22+
var realf = require( '@stdlib/complex/realf' );
23+
var imagf = require( '@stdlib/complex/imagf' );
24+
var last = require( './../lib' );
25+
26+
// Create a complex number array:
27+
var arr = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
28+
29+
// Return the last element:
30+
var out = last( arr );
31+
// returns <Complex64>
32+
33+
var re = realf( out );
34+
// returns 5.0
35+
36+
var im = imagf( out );
37+
// returns 6.0
38+
39+
console.log( '%d + %di', re, im );
40+
// => '5 + 6i'

0 commit comments

Comments
 (0)