Skip to content

feat: add map method to array/bool #2292

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions lib/node_modules/@stdlib/array/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 lib/node_modules/@stdlib/array/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 lib/node_modules/@stdlib/array/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 lib/node_modules/@stdlib/array/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 lib/node_modules/@stdlib/array/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