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 27, 2024
1 parent f330b15 commit c4b3301
Show file tree
Hide file tree
Showing 13 changed files with 1,529 additions and 5 deletions.
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions dist/index.js.map

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,15 @@ setReadOnly( ns, 'arrayShape', require( './../shape' ) );
*/
setReadOnly( ns, 'SharedArrayBuffer', require( './../shared-buffer' ) );

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

/**
* @name circarray2iterator
* @memberof ns
Expand Down
158 changes: 158 additions & 0 deletions slice/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
<!--
@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.
-->

# slice

> Return a shallow copy of a portion of an 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 slice = require( '@stdlib/array/slice' );
```

#### slice( x\[, start\[, end]] )

Returns a shallow copy of a portion of an array.

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

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

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

The function accepts the following arguments:

- **x**: input array.
- **start**: starting index (inclusive). Default: `0`.
- **end**: ending index (exclusive). Default: `x.length`.

By default, the function returns a shallow copy beginning from the first element of the provided input array. To begin copying from an alternative element, provide a starting index (inclusive).

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

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

By default, the function copies through the end of the provided input array. To copy up until a specific element, provide an ending index (exclusive).

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

var out = slice( x, 1, 4 );
// returns [ 2, 3, 4 ]
```

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

```text
x.slice( start, end )
```
- If provided an array-like object without a `slice` method, the function copies input array elements to a new generic 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/zero-to' );
var slice = require( '@stdlib/array/slice' );
var x = zeroTo( 6, 'float64' );
// returns <Float64Array>[ 0.0, 1.0, 2.0, 3.0, 4.0, 5.0 ]
var s = slice( x );
// returns <Float64Array>[ 0.0, 1.0, 2.0, 3.0, 4.0, 5.0 ]
s = slice( x, 0, 4 );
// returns <Float64Array>[ 0.0, 1.0, 2.0, 3.0 ]
s = slice( x, 2 );
// returns <Float64Array>[ 2.0, 3.0, 4.0, 5.0 ]
s = slice( x, 2, 4 );
// returns <Float64Array>[ 2.0, 3.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 slice/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 ones = require( './../../ones' );
var pkg = require( './../package.json' ).name;
var slice = require( './../lib' );


// FUNCTIONS //

/**
* Creates a benchmark function.
*
* @private
* @param {PositiveInteger} len - array length
* @returns {Function} benchmark function
*/
function createBenchmark( len ) {
var x = ones( len, 'generic' );
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 = slice( 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();
41 changes: 41 additions & 0 deletions slice/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

{{alias}}( x[, start[, end]] )
Returns a shallow copy of a portion of an array.

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

x.slice( start, end )

If provided an array-like object without a `slice` method, the function
copies input array elements to a new generic array.

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

start: integer (optional)
Starting index (inclusive). Default: 0.

end: integer (optional)
Ending index (exclusive). Default: x.length.

Returns
-------
out: Array|TypedArray
Output array.

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

See Also
--------

Loading

0 comments on commit c4b3301

Please sign in to comment.