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 Jun 5, 2024
1 parent 943e1fc commit a3e6a84
Show file tree
Hide file tree
Showing 7 changed files with 517 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,7 @@ This release closes the following issue:

##### Features

- [`40da309`](https://github.com/stdlib-js/stdlib/commit/40da3097c6ffaed4cd9284d6cdeff8bf11786553) - add `map` method to `array/bool` [(#2292)](https://github.com/stdlib-js/stdlib/pull/2292)
- [`5d53c2d`](https://github.com/stdlib-js/stdlib/commit/5d53c2de22e6899dc3970b4714c0c4e228562f92) - add initial TypeScript declarations
- [`e221398`](https://github.com/stdlib-js/stdlib/commit/e2213984af7a95af1b439aae8e2122968892c55d) - add `array/bool`

Expand Down Expand Up @@ -1634,6 +1635,7 @@ A total of 13 people contributed to this release. Thank you to the following con

<details>

- [`40da309`](https://github.com/stdlib-js/stdlib/commit/40da3097c6ffaed4cd9284d6cdeff8bf11786553) - **feat:** add `map` method to `array/bool` [(#2292)](https://github.com/stdlib-js/stdlib/pull/2292) _(by Jaysukh Makvana, Athan Reines)_
- [`3edcfe5`](https://github.com/stdlib-js/stdlib/commit/3edcfe5d814fd12a56dbe492ddc78663721f5acd) - **feat:** update namespace TypeScript declarations [(#2303)](https://github.com/stdlib-js/stdlib/pull/2303) _(by stdlib-bot, Athan Reines)_
- [`96e896a`](https://github.com/stdlib-js/stdlib/commit/96e896a39be08912b2e06dfb6b671ec13d042412) - **feat:** add support for boolean array indices _(by Athan Reines)_
- [`cc06b5c`](https://github.com/stdlib-js/stdlib/commit/cc06b5c8c67a1c8d1022920273535b29e5abf550) - **fix:** add missing generic parameter _(by Athan Reines)_
Expand Down
61 changes: 61 additions & 0 deletions bool/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,67 @@ var v = arr.get( 100 );
// returns undefined
```

<a name="method-map"></a>

#### BooleanArray.prototype.map( callbackFn\[, thisArg] )

Returns a new array with each element being the result of a provided callback function.

```javascript
function invert( v ) {
return !v;
}

var arr = new BooleanArray( 3 );

arr.set( true, 0 );
arr.set( false, 1 );
arr.set( true, 2 );

var out = arr.map( invert );
// returns <BooleanArray>

var z = out.get( 0 );
// returns false

z = out.get( 1 );
// returns true

z = out.get( 2 );
// returns false
```

The callback function is provided three arguments:

- **value**: current array element.
- **index**: current array element index.
- **arr**: the array on which this method was called.

To set the function execution context, provide a `thisArg`.

```javascript
function invert( v, i ) {
this.count += i;
return !v;
}

var arr = new BooleanArray( 3 );

var context = {
'count': 0
};

arr.set( true, 0 );
arr.set( false, 1 );
arr.set( true, 2 );

var out = arr.map( invert, context );
// returns <BooleanArray>

var count = context.count;
// returns 3;
```

<a name="method-set"></a>

#### BooleanArray.prototype.set( v\[, i] )
Expand Down
55 changes: 55 additions & 0 deletions bool/benchmark/benchmark.map.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* @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 isBooleanArray = require( '@stdlib/assert/is-booleanarray' );
var pkg = require( './../package.json' ).name;
var BooleanArray = require( './../lib' );


// MAIN //

bench( pkg+':map', function benchmark( b ) {
var out;
var arr;
var i;

arr = new BooleanArray( [ true, false, false, true, true, false ] );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = arr.map( invert );
if ( typeof out !== 'object' ) {
b.fail( 'should return an object' );
}
}
b.toc();
if ( !isBooleanArray( out ) ) {
b.fail( 'should return a BooleanArray' );
}
b.pass( 'benchmark finished' );
b.end();

function invert( v ) {
return !v;
}
});
104 changes: 104 additions & 0 deletions bool/benchmark/benchmark.map.length.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/**
* @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 Boolean = require( '@stdlib/boolean/ctor' );
var identity = require( '@stdlib/utils/identity-function' );
var isBooleanArray = require( '@stdlib/assert/is-booleanarray' );
var pkg = require( './../package.json' ).name;
var BooleanArray = require( './../lib' );


// FUNCTIONS //

/**
* Creates a benchmark function.
*
* @private
* @param {PositiveInteger} len - array length
* @returns {Function} benchmark function
*/
function createBenchmark( len ) {
var arr;
var i;

arr = [];
for ( i = 0; i < len; i++ ) {
arr.push( Boolean( i%2 ) );
}
arr = new BooleanArray( arr );

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 = arr.map( identity );
if ( typeof out !== 'object' ) {
b.fail( 'should return an object' );
}
}
b.toc();
if ( !isBooleanArray( out ) ) {
b.fail( 'should return a BooleanArray' );
}
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+':map:len='+len, f );
}
}

main();
76 changes: 76 additions & 0 deletions bool/docs/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,50 @@ type FromBinary<U> = ( this: U, value: boolean, index: number ) => boolean;
*/
type FromCallback<U> = FromNullary<U> | FromUnary<U> | FromBinary<U>;

/**
* Callback invoked for each element in an array.
*
* @returns returned value
*/
type NullaryMapFcn<U> = ( this: U ) => any;

/**
* Callback invoked for each element in an array.
*
* @param value - current array element
* @returns returned value
*/
type UnaryMapFcn<U> = ( this: U, value: boolean ) => any;

/**
* Callback invoked for each element in an array.
*
* @param value - current array element
* @param index - current array element index
* @returns returned value
*/
type BinaryMapFcn<U> = ( this: U, value: boolean, index: number ) => any;

/**
* Callback invoked for each element in an array.
*
* @param value - current array element
* @param index - current array element index
* @param arr - array on which the method was called
* @returns returned value
*/
type TernaryMapFcn<U> = ( this: U, value: boolean, index: number, arr: BooleanArray ) => any;

/**
* Callback invoked for each element in an array.
*
* @param value - current array element
* @param index - current array element index
* @param arr - array on which the method was called
* @returns returned value
*/
type MapFcn<U> = NullaryMapFcn<U> | UnaryMapFcn<U> | BinaryMapFcn<U> | TernaryMapFcn<U>;

/**
* Class for creating a Boolean array.
*/
Expand Down Expand Up @@ -194,6 +238,38 @@ declare class BooleanArray implements BooleanArrayInterface {
*/
get( i: number ): boolean | void;

/**
* Returns a new array with each element being the result of a provided callback function.
*
* @param fcn - callback function
* @param thisArg - callback function execution context
* @returns new boolean array
*
* @example
* function invert( v ) {
* return !v;
* }
*
* var arr = new BooleanArray( 3 );
*
* arr.set( true, 0 );
* arr.set( false, 1 );
* arr.set( true, 2 );
*
* var out = arr.map( invert );
* // returns <BooleanArray>
*
* var v = out.get( 0 );
* // returns false
*
* v = out.get( 1 );
* // returns true
*
* v = out.get( 2 );
* // returns false
*/
map<U = unknown>( fcn: MapFcn<U>, thisArg?: ThisParameterType<MapFcn<U>> ): BooleanArray;

/**
* Sets an array element.
*
Expand Down
55 changes: 55 additions & 0 deletions bool/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,61 @@ setReadOnlyAccessor( BooleanArray.prototype, 'length', function get() {
return this._length;
});

/**
* Returns a new array with each element being the result of a provided callback function.
*
* @name map
* @memberof BooleanArray.prototype
* @type {Function}
* @param {Function} fcn - callback function
* @param {*} [thisArg] - callback function execution context
* @throws {TypeError} `this` must be a boolean array
* @throws {TypeError} first argument must be a function
* @returns {BooleanArray} new boolean array
*
* @example
* function invert( v ) {
* return !v;
* }
*
* var arr = new BooleanArray( 3 );
*
* arr.set( true, 0 );
* arr.set( false, 1 );
* arr.set( true, 2 );
*
* var out = arr.map( invert );
* // returns <BooleanArray>
*
* var z = out.get( 0 );
* // returns false
*
* z = out.get( 1 );
* // returns true
*
* z = out.get( 2 );
* // returns false
*/
setReadOnly( BooleanArray.prototype, 'map', function map( fcn, thisArg ) {
var outbuf;
var out;
var buf;
var i;
if ( !isBooleanArray( this ) ) {
throw new TypeError( 'invalid invocation. `this` is not a boolean array.' );
}
if ( !isFunction( fcn ) ) {
throw new TypeError( 'invalid argument. First argument must be a function. Value: `%s`.', fcn );
}
buf = this._buffer;
out = new this.constructor( this._length );
outbuf = out._buffer; // eslint-disable-line no-underscore-dangle
for ( i = 0; i < this._length; i++ ) {
outbuf[ i ] = Boolean( fcn.call( thisArg, Boolean( buf[ i ] ), i, this ) );
}
return out;
});

/**
* Sets an array element.
*
Expand Down
Loading

0 comments on commit a3e6a84

Please sign in to comment.