Skip to content

Commit

Permalink
Auto-generated commit
Browse files Browse the repository at this point in the history
  • Loading branch information
stdlib-bot committed Jan 16, 2024
1 parent d119271 commit 4f26fba
Show file tree
Hide file tree
Showing 13 changed files with 1,189 additions and 5 deletions.
9 changes: 9 additions & 0 deletions base/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1035,6 +1035,15 @@ setReadOnly( ns, 'quinary5d', require( './../../base/quinary5d' ) );
*/
setReadOnly( ns, 'resolveGetter', require( './../../base/resolve-getter' ) );

/**
* @name reverse
* @memberof ns
* @readonly
* @type {Function}
* @see {@link module:@stdlib/array/base/reverse}
*/
setReadOnly( ns, 'reverse', require( './../../base/reverse' ) );

/**
* @name setter
* @memberof ns
Expand Down
128 changes: 128 additions & 0 deletions base/reverse/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<!--
@license Apache-2.0
Copyright (c) 2024 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.
-->

# reverse

> Reverse an array in-place.
<!-- 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 reverse = require( '@stdlib/array/base/reverse' );
```

#### reverse( x )

Reverses an array in-place.

```javascript
var x = [ 1, 2, 3, 4, 5, 6 ];

var out = reverse( x );
// returns [ 6, 5, 4, 3, 2, 1 ]

var bool = ( out === x );
// returns true
```

</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 `reverse` method, the function defers execution to that method and assumes that the method API has the following signature:

```text
x.reverse()
```
- If provided an array-like object without a `reverse` method, the function manually reverses elements and mutates the input array.
</section>
<!-- /.notes -->
<!-- Package usage examples. -->
<section class="examples">
## Examples
<!-- eslint no-undef: "error" -->
```javascript
var Float64Array = require( '@stdlib/array/float64' );
var zeroTo = require( '@stdlib/array/base/zero-to' );
var reverse = require( '@stdlib/array/base/reverse' );
var x = new Float64Array( zeroTo( 6 ) );
// returns <Float64Array>[ 0.0, 1.0, 2.0, 3.0, 4.0, 5.0 ]
var y = reverse( x );
// returns <Float64Array>[ 5.0, 4.0, 3.0, 2.0, 1.0, 0.0 ]
var z = reverse( y );
// returns <Float64Array>[ 0.0, 1.0, 2.0, 3.0, 4.0, 5.0 ]
```

</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">

</section>

<!-- /.links -->
96 changes: 96 additions & 0 deletions base/reverse/benchmark/benchmark.length.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2024 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 pow = require( '@stdlib/math/base/special/pow' );
var isArray = require( '@stdlib/assert/is-array' );
var zeroTo = require( './../../../base/zero-to' );
var pkg = require( './../package.json' ).name;
var reverse = require( './../lib' );


// FUNCTIONS //

/**
* Creates a benchmark function.
*
* @private
* @param {PositiveInteger} len - array length
* @returns {Function} benchmark function
*/
function createBenchmark( len ) {
var x = zeroTo( 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 = reverse( x );
if ( out.length !== len ) {
b.fail( 'unexpected length' );
}
}
b.toc();
if ( !isArray( out ) ) {
b.fail( 'should return an array' );
}
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();
31 changes: 31 additions & 0 deletions base/reverse/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

{{alias}}( x )
Reverses an array in-place.

If provided an array-like object having a `reverse` method, the function
defers execution to that method and assumes that the method has the
following signature:

x.reverse()

If provided an array-like object without a `reverse` method, the function
manually reverses elements and mutates the input array in-place.

Parameters
----------
x: ArrayLikeObject
Input array.

Returns
-------
out: ArrayLikeObject
Input array.

Examples
--------
> var out = {{alias}}( [ 1, 2, 3, 4 ] )
[ 4, 3, 2, 1 ]

See Also
--------

Loading

0 comments on commit 4f26fba

Please sign in to comment.