Skip to content
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

feat: add forEach method to array/complex64 #1211

Merged
merged 12 commits into from
Dec 24, 2023
66 changes: 66 additions & 0 deletions lib/node_modules/@stdlib/array/complex64/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1197,6 +1197,72 @@ var count = context.count;
// returns 3
```

<a name="method-for-each"></a>

#### Complex64Array.prototype.forEach( predicate\[, thisArg] )

Invokes a function once for each array element.

```javascript
var Complex64 = require( '@stdlib/complex/float32' );

function log( v, i ) {
console.log( '%s: %s', i, v.toString() );
}

var arr = new Complex64Array( 3 );

// Set the first three elements:
arr.set( [ 1.0, 1.0 ], 0 );
arr.set( [ 2.0, 2.0 ], 1 );
arr.set( [ 3.0, 3.0 ], 2 );

arr.forEach( log );
/* =>
0: 1 + 1i
1: 2 + 2i
2: 3 + 3i
*/
```

The invoked 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
var Complex64 = require( '@stdlib/complex/float32' );

function fcn( v, i ) {
this.count += 1;
console.log( '%s: %s', i, v.toString() );
}

var arr = new Complex64Array( 3 );

var context = {
'count': 0
};

// Set the first three elements:
arr.set( [ 1.0, -1.0 ], 0 );
arr.set( [ 2.0, -2.0 ], 1 );
arr.set( [ 3.0, -3.0 ], 2 );

arr.forEach( fcn, context );
/* =>
0: 1 + 1i
1: 2 + 2i
2: 3 + 3i
*/

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

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

#### Complex64Array.prototype.get( i )
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2023 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 isComplex64 = require( '@stdlib/assert/is-complex64' );
var pkg = require( './../package.json' ).name;
var Complex64Array = require( './../lib' );


// MAIN //

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

arr = new Complex64Array( [ 1, 2, 3, 4 ] );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
arr.forEach( check );
if ( arr.length !== 2 ) {
b.fail( 'should not change an array length' );
}
}
b.toc();
if ( arr.length !== 2 ) {
b.fail( 'should not change an array length' );
}
b.pass( 'benchmark finished' );
b.end();

function check( v ) {
if ( !isComplex64( v ) ) {
b.fail( 'should be a complex number' );
}
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2023 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 Complex64 = require( '@stdlib/complex/float32' );
var realf = require( '@stdlib/complex/realf' );
var imagf = require( '@stdlib/complex/imagf' );
var pkg = require( './../package.json' ).name;
var Complex64Array = 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( new Complex64( i, i ) );
}
arr = new Complex64Array( arr );

return benchmark;

/**
* Benchmark function.
*
* @private
* @param {Benchmark} b - benchmark instance
*/
function benchmark( b ) {
var i;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
arr.forEach( callback );
if ( arr.length !== len ) {
b.fail( 'should not change an array length' );
}
}
b.toc();
if ( arr.length !== len ) {
b.fail( 'should not change an array length' );
}
b.pass( 'benchmark finished' );
b.end();

function callback( value ) {
if ( realf( value ) !== imagf( value ) ) {
throw new Error( 'something went wrong' );
}
}
}
}


// 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+':forEach:len='+len, f );
}
}

main();
73 changes: 73 additions & 0 deletions lib/node_modules/@stdlib/array/complex64/docs/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable max-lines */

/*
* @license Apache-2.0
*
Expand Down Expand Up @@ -105,6 +107,50 @@ type TernaryPredicate<U> = ( this: U, value: Complex64, index: number, arr: Comp
*/
type Predicate<U> = NullaryPredicate<U> | UnaryPredicate<U> | BinaryPredicate<U> | TernaryPredicate<U>;

/**
* Callback invoked for each element in an array.
*
* @returns undefined
*/
type NullaryCallback<U> = ( this: U ) => void;

/**
* Callback invoked for each element in an array.
*
* @param value - current array element
* @returns undefined
*/
type UnaryCallback<U> = ( this: U, value: Complex64 ) => void;

/**
* Callback invoked for each element in an array.
*
* @param value - current array element
* @param index - current array element index
* @returns undefined
*/
type BinaryCallback<U> = ( this: U, value: Complex64, index: number ) => void;

/**
* 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 undefined
*/
type TernaryCallback<U> = ( this: U, value: Complex64, index: number, arr: Complex64Array ) => void;

/**
* 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 undefined
*/
type Callback<U> = NullaryCallback<U> | UnaryCallback<U> | BinaryCallback<U> | TernaryCallback<U>;

/**
* Class for creating a 64-bit complex number array.
*/
Expand Down Expand Up @@ -501,6 +547,33 @@ declare class Complex64Array implements Complex64ArrayInterface {
*/
findLastIndex<U = unknown>( predicate: Predicate, thisArg?: ThisParameterType<Predicate<U>> ): number;

/**
* Invokes a function once for each array element.
*
* @param fcn - function to invoke
* @param thisArg - execution context
* @returns undefined
*
* @example
* var Complex64 = require( '@stdlib/complex/float32' );
*
* function log( v, i ) {
* console.log( '%s: %s', i, v.toString() );
* }
*
* var arr = new Complex64Array( 3 );
*
* arr.set( [ 1.0, 1.0 ], 0 );
* arr.set( [ 2.0, 2.0 ], 1 );
* arr.set( [ 3.0, 3.0 ], 2 );
*
* arr.forEach( log );
* // => 0: 1 + 1i
* // => 1: 2 + 2i
* // => 2: 3 + 3i
*/
forEach<U = unknown>( fcn: Callback, thisArg?: ThisParameterType<Callback<U>> ): void;

/**
* Returns an array element.
*
Expand Down
43 changes: 43 additions & 0 deletions lib/node_modules/@stdlib/array/complex64/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
* @param {*} value - value to test
* @returns {boolean} boolean indicating if a value is a `Complex64Array`
*/
function isComplex64Array( value ) { // TODO: move to array/base/assert/is-complex64-array

Check warning on line 109 in lib/node_modules/@stdlib/array/complex64/lib/main.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected 'todo' comment: 'TODO: move to...'
return (
typeof value === 'object' &&
value !== null &&
Expand All @@ -122,7 +122,7 @@
* @param {*} value - value to test
* @returns {boolean} boolean indicating if a value is a `Complex128Array`
*/
function isComplex128Array( value ) { // TODO: move to array/base/assert/is-complex128-array

Check warning on line 125 in lib/node_modules/@stdlib/array/complex64/lib/main.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected 'todo' comment: 'TODO: move to...'
return (
typeof value === 'object' &&
value !== null &&
Expand Down Expand Up @@ -281,7 +281,7 @@
}
buf = buf[ ITERATOR_SYMBOL ]();
if ( !isFunction( buf.next ) ) {
throw new TypeError( format( 'invalid argument. Must provide a length, ArrayBuffer, typed array, array-like object, or an iterable. Value: `%s`.', buf ) ); // FIXME: `buf` is what is returned from above, NOT the original value

Check warning on line 284 in lib/node_modules/@stdlib/array/complex64/lib/main.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected 'fixme' comment: 'FIXME: `buf` is what is returned from...'
}
buf = fromIterator( buf );
if ( buf instanceof Error ) {
Expand Down Expand Up @@ -746,7 +746,7 @@
if ( !isComplexArray( this ) ) {
throw new TypeError( 'invalid invocation. `this` is not a complex number array.' );
}
// FIXME: prefer a functional `copyWithin` implementation which addresses lack of universal browser support (e.g., IE11 and Safari) or ensure that typed arrays are polyfilled

Check warning on line 749 in lib/node_modules/@stdlib/array/complex64/lib/main.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected 'fixme' comment: 'FIXME: prefer a functional `copyWithin`...'
if ( arguments.length === 2 ) {
this._buffer.copyWithin( target*2, start*2 );
} else {
Expand Down Expand Up @@ -1192,6 +1192,49 @@
return -1;
});

/**
* Invokes a function once for each array element.
*
* @name forEach
* @memberof Complex64Array.prototype
* @type {Function}
* @param {Function} fcn - function to invoke
* @param {*} [thisArg] - function invocation context
* @throws {TypeError} `this` must be a complex number array
* @throws {TypeError} first argument must be a function
*
* @example
* var Complex64 = require( '@stdlib/complex/float32' );
*
* function log( v, i ) {
* console.log( '%s: %s', i, v.toString() );
* }
*
* var arr = new Complex64Array( 3 );
*
* arr.set( [ 1.0, 1.0 ], 0 );
* arr.set( [ 2.0, 2.0 ], 1 );
* arr.set( [ 3.0, 3.0 ], 2 );
*
* arr.forEach( log );
*/
setReadOnly( Complex64Array.prototype, 'forEach', function forEach( fcn, thisArg ) {
var buf;
var i;
var z;
if ( !isComplexArray( this ) ) {
throw new TypeError( 'invalid invocation. `this` is not a complex number array.' );
}
if ( !isFunction( fcn ) ) {
throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', fcn ) );
}
buf = this._buffer;
for ( i = 0; i < this._length; i++ ) {
z = getComplex64( buf, i );
fcn.call( thisArg, z, i, this );
}
});

/**
* Returns an array element.
*
Expand Down Expand Up @@ -1632,7 +1675,7 @@
// We need to copy source values...
tmp = new Float32Array( N );
for ( i = 0; i < N; i++ ) {
tmp[ i ] = sbuf[ i ]; // TODO: handle accessor arrays

Check warning on line 1678 in lib/node_modules/@stdlib/array/complex64/lib/main.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected 'todo' comment: 'TODO: handle accessor arrays'
}
sbuf = tmp;
}
Expand Down
Loading
Loading