Skip to content

Commit b86681d

Browse files
committed
Auto-generated commit
1 parent 96cd064 commit b86681d

File tree

13 files changed

+885
-5
lines changed

13 files changed

+885
-5
lines changed

base/at/README.md

+142
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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+
# at
22+
23+
> Return 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 at = require( '@stdlib/array/base/at' );
41+
```
42+
43+
#### at( x, index )
44+
45+
Return an element from an array.
46+
47+
```javascript
48+
var x = [ 1, 2, 3, 4 ];
49+
50+
var out = at( x, 0 );
51+
// returns 1
52+
53+
out = at( x, 1 );
54+
// returns 2
55+
56+
out = at( x, -2 );
57+
// returns 3
58+
```
59+
60+
The function accepts the following arguments:
61+
62+
- **x**: an input array.
63+
- **index**: element index.
64+
65+
</section>
66+
67+
<!-- /.usage -->
68+
69+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
70+
71+
<section class="notes">
72+
73+
## Notes
74+
75+
- If provided an array-like object having an `at` method , the function defers execution to that method and assumes that the method has the following signature:
76+
77+
```text
78+
x.at( index )
79+
```
80+
81+
If provided an array-like object without an `at` method, the function normalizes a provided index and returns a specified element.
82+
83+
- Negative indices are resolved relative to the last array element, with the last element corresponding to `-1`.
84+
85+
- If provided out-of-bounds indices, the function always returns `undefined`.
86+
87+
</section>
88+
89+
<!-- /.notes -->
90+
91+
<!-- Package usage examples. -->
92+
93+
<section class="examples">
94+
95+
## Examples
96+
97+
<!-- eslint no-undef: "error" -->
98+
99+
```javascript
100+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
101+
var at = require( '@stdlib/array/base/at' );
102+
103+
// Define an array:
104+
var x = discreteUniform( 10, -100, 100 );
105+
106+
// Define an array containing random index values:
107+
var indices = discreteUniform( 100, -x.length, x.length-1 );
108+
109+
// Randomly selected values from the input array:
110+
var i;
111+
for ( i = 0; i < indices.length; i++ ) {
112+
console.log( 'x[%d] = %d', indices[ i ], at( x, indices[ i ] ) );
113+
}
114+
```
115+
116+
</section>
117+
118+
<!-- /.examples -->
119+
120+
<!-- 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. -->
121+
122+
<section class="references">
123+
124+
</section>
125+
126+
<!-- /.references -->
127+
128+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
129+
130+
<section class="related">
131+
132+
</section>
133+
134+
<!-- /.related -->
135+
136+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
137+
138+
<section class="links">
139+
140+
</section>
141+
142+
<!-- /.links -->

base/at/benchmark/benchmark.js

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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 uniform = require( '@stdlib/random/array/uniform' );
25+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var pkg = require( './../package.json' ).name;
27+
var at = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg, function benchmark( b ) {
33+
var x;
34+
var v;
35+
var i;
36+
var j;
37+
38+
x = uniform( 100, 0.0, 10.0 );
39+
40+
b.tic();
41+
for ( i = 0; i < b.iterations; i++ ) {
42+
j = ( i%20 ) - 10;
43+
v = at( x, j );
44+
if ( v !== v ) {
45+
b.fail( 'should not return NaN' );
46+
}
47+
}
48+
b.toc();
49+
if ( isnan( v ) ) {
50+
b.fail( 'should not return NaN' );
51+
}
52+
b.pass( 'benchmark finished' );
53+
b.end();
54+
});

base/at/docs/repl.txt

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2+
{{alias}}( x, index )
3+
Returns 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 array-like object having an `at` method , the function defers
9+
execution to that method and assumes that the method has the following
10+
signature:
11+
12+
x.at( index )
13+
14+
If provided an array-like object without an `at` method, the function
15+
normalizes a provided index and returns a specified element.
16+
17+
If provided an out-of-bounds index, the function always returns `undefined`.
18+
19+
Parameters
20+
----------
21+
x: ArrayLikeObject
22+
Input array.
23+
24+
index: integer
25+
Element index.
26+
27+
Returns
28+
-------
29+
out: any
30+
Element value.
31+
32+
Examples
33+
--------
34+
> var x = [ 1, 2, 3, 4 ];
35+
> {{alias}}( x, 0 )
36+
1
37+
> {{alias}}( x, -2 )
38+
3
39+
40+
See Also
41+
--------
42+

base/at/docs/types/index.d.ts

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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+
/// <reference types="@stdlib/types"/>
22+
23+
import { Complex128, Complex64 } from '@stdlib/types/complex';
24+
import { Collection, AccessorArrayLike, Complex128Array, Complex64Array } from '@stdlib/types/array';
25+
26+
/**
27+
* Returns an element from an array.
28+
*
29+
* @param x - input array
30+
* @param index - element index
31+
* @returns array element
32+
*
33+
* @example
34+
* var Complex128Array = require( '@stdlib/array/complex128' );
35+
* var real = require( '@stdlib/complex/real' );
36+
* var imag = require( '@stdlib/complex/imag' );
37+
*
38+
* var x = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] );
39+
*
40+
* var v = at( x, 0 );
41+
* // returns <Complex128>
42+
*
43+
* v = at( x, 1 );
44+
* // returns <Complex128>
45+
*
46+
* v = at( x, -2 );
47+
* // returns <Complex128>
48+
*
49+
* var re = real( v );
50+
* // returns 5.0
51+
*
52+
* var im = imag( v );
53+
* // returns 6.0
54+
*/
55+
declare function at( x: Complex128Array, index: number ): Complex128 | void;
56+
57+
/**
58+
* Returns an element from an array.
59+
*
60+
* @param x - input array
61+
* @param index - element index
62+
* @returns array element
63+
*
64+
* @example
65+
* var Complex128Array = require( '@stdlib/array/complex64' );
66+
* var realf = require( '@stdlib/complex/realf' );
67+
* var imagf = require( '@stdlib/complex/imagf' );
68+
*
69+
* var x = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] );
70+
*
71+
* var v = at( x, 0 );
72+
* // returns <Complex64>
73+
*
74+
* v = at( x, 1 );
75+
* // returns <Complex64>
76+
*
77+
* v = at( x, -2 );
78+
* // returns <Complex64>
79+
*
80+
* var re = realf( v );
81+
* // returns 5.0
82+
*
83+
* var im = imagf( v );
84+
* // returns 6.0
85+
*/
86+
declare function at( x: Complex64Array, index: number ): Complex64 | void;
87+
88+
/**
89+
* Returns an element from an array.
90+
*
91+
* @param x - input array
92+
* @param index - element index
93+
* @returns array element
94+
*
95+
* @example
96+
* var x = [ 1, 2, 3, 4 ];
97+
*
98+
* var v = at( x, 0 );
99+
* // returns 1
100+
*
101+
* v = at( x, 1 );
102+
* // returns 2
103+
*
104+
* v = at( x, -2 );
105+
* // returns 3
106+
*/
107+
declare function at<T = unknown>( x: Collection<T> | AccessorArrayLike<T>, index: number ): T | void;
108+
109+
110+
// EXPORTS //
111+
112+
export = at;

0 commit comments

Comments
 (0)