Skip to content

feat: add reduce method to array/complex64 #1271

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 34 commits into from
Feb 24, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
a34dd86
feat: add implementation and ts declaration to reduce method array/co…
Jaysukh-409 Jan 15, 2024
5c88777
feat: updated typescript declaration
Jaysukh-409 Jan 15, 2024
85c749c
feat: apply suggestion in implementation
Jaysukh-409 Jan 27, 2024
f9c9c5c
Apply suggestions from code review
kgryte Jan 28, 2024
6d2ee68
Apply suggestions from code review
kgryte Jan 28, 2024
58fa1c4
Apply suggestions from code review
kgryte Jan 28, 2024
53b299a
Apply suggestions from code review
kgryte Jan 28, 2024
199ab19
Apply suggestions from code review
kgryte Jan 28, 2024
f83df65
Apply suggestions from code review
kgryte Jan 28, 2024
e99521d
Apply suggestions from code review
kgryte Jan 28, 2024
8a0ae80
Apply suggestions from code review
kgryte Jan 28, 2024
1672d68
Merge branch 'develop' of https://github.com/stdlib-js/stdlib into re…
Jaysukh-409 Feb 20, 2024
2f33439
Merge branch 'reduce' of https://github.com/Jaysukh-409/stdlib into r…
Jaysukh-409 Feb 20, 2024
d2982f1
feat: added benchmark and update README
Jaysukh-409 Feb 20, 2024
bf1a24b
Apply suggestions from code review
kgryte Feb 21, 2024
db3bd3b
Apply suggestions from code review
kgryte Feb 21, 2024
5b08331
Apply suggestions from code review
kgryte Feb 21, 2024
39cabdb
Apply suggestions from code review
kgryte Feb 21, 2024
c82d5f8
Apply suggestions from code review
kgryte Feb 21, 2024
67f4dae
Apply suggestions from code review
kgryte Feb 21, 2024
26abe08
Apply suggestions from code review
kgryte Feb 21, 2024
ad19a46
Apply suggestions from code review
kgryte Feb 21, 2024
c6c12b6
Apply suggestions from code review
kgryte Feb 21, 2024
fdf608c
feat: added tests
Jaysukh-409 Feb 23, 2024
1934ab4
fix: lint error in benchmark
Jaysukh-409 Feb 23, 2024
856fa92
Apply suggestions from code review
kgryte Feb 23, 2024
8f01c36
feat: updated tests
Jaysukh-409 Feb 24, 2024
f35837f
Apply suggestions from code review
kgryte Feb 24, 2024
2a48fe9
Apply suggestions from code review
kgryte Feb 24, 2024
d087f6b
Apply suggestions from code review
kgryte Feb 24, 2024
f5ff8a7
Apply suggestions from code review
kgryte Feb 24, 2024
be872b3
Apply suggestions from code review
kgryte Feb 24, 2024
9529b8e
Apply suggestions from code review
kgryte Feb 24, 2024
d94a052
Apply suggestions from code review
kgryte Feb 24, 2024
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
84 changes: 84 additions & 0 deletions lib/node_modules/@stdlib/array/complex64/docs/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,60 @@ type TernaryMapFcn<U> = ( this: U, value: Complex64, index: number, arr: Complex
*/
type MapFcn<U> = NullaryMapFcn<U> | UnaryMapFcn<U> | BinaryMapFcn<U> | TernaryMapFcn<U>;

/**
* Reducer function invoked for each element in an array.
*
* @returns accumulated result
*/
type NullaryReducer<U, V> = ( this: U ) => V;

/**
* Reducer function invoked for each element in an array.
*
* @param acc - accumulated result
* @returns accumulated result
*/
type UnaryReducer<U, V> = ( this: U, acc: V ) => V;

/**
* Reducer function invoked for each element in an array.
*
* @param acc - accumulated result
* @param value - current array element
* @returns accumulated result
*/
type BinaryReducer<U, V> = ( this: U, acc: V, value: Complex64 ) => V;

/**
* Reducer function invoked for each element in an array.
*
* @param acc - accumulated result
* @param value - current array element
* @param index - current array element index
* @returns accumulated result
*/
type TernaryReducer<U, V> = ( this: U, acc: V, value: Complex64, index: number ) => V;

/**
* @param acc - accumulated result
* @param value - current array element
* @param index - current array element index
* @param arr - array on which the method was called
* @returns accumulated result
*/
type QuaternaryReducer<U, V> = ( this: U, acc: V, value: Complex64, index: number, arr: Complex64Array ) => V;

/**
* Reducer function invoked for each element in an array.
*
* @param acc - accumulated result
* @param value - current array element
* @param index - current array element index
* @param arr - array on which the method was called
* @returns accumulated result
*/
type Reducer = NullaryReducer<U, V> | UnaryReducer<U, V> | BinaryReducer<U, V> | TernaryReducer<U, V> | QuaternaryReducer<U, V>;

/**
* Class for creating a 64-bit complex number array.
*/
Expand Down Expand Up @@ -813,6 +867,36 @@ declare class Complex64Array implements Complex64ArrayInterface {
*/
map<U = unknown>( fcn: MapFcn<U>, thisArg?: ThisParameterType<MapFcn<U>> ): Complex64Array;

/**
* Applies a provided callback function on each element of the array, in order, passing in the return value from the calculation on the preceding element and returning the accumulated result upon completion.
*
* @param reducer - callback function
* @param initialValue - initial value
* @returns accumulated result
*
* @example
* var realf = require( '@stdlib/complex/realf' );
* var imagf = require( '@stdlib/complex/imagf' );
* var caddf = require( '@stdlib/math/base/ops/caddf' );
* var Complex64 = require( '@stdlib/complex/float32' );
*
* 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 );
*
* var z = arr.reduce( caddf );
* // returns <Complex64>
*
* var re = realf( z );
* // returns 6.0
*
* var im = imagf( z );
* // returns 6.0
*/
reduce<U = unknown, V = unknown>( reducer: Reducer<U, V>, initialValue?: V ): V;

/**
* Reverses an array in-place.
*
Expand Down
65 changes: 65 additions & 0 deletions lib/node_modules/@stdlib/array/complex64/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -1699,6 +1699,71 @@ setReadOnly( Complex64Array.prototype, 'map', function map( fcn, thisArg ) {
return out;
});

/**
* Returns value that results from running the reducer callback function to completion over the entire typed array.
*
* @name reduce
* @memberof Complex64Array.prototype
* @type {Function}
* @param {Function} reducer - callback function
* @param {*} [initialValue] - initial value
* @throws {TypeError} `this` must be a complex number array
* @throws {TypeError} first argument must be a function
* @returns {*} accumulated result
*
* @example
* var realf = require( '@stdlib/complex/realf' );
* var imagf = require( '@stdlib/complex/imagf' );
* var caddf = require( '@stdlib/math/base/ops/caddf' );
* var Complex64 = require( '@stdlib/complex/float32' );
*
* 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 );
*
* var z = arr.reduce( caddf );
* // returns <Complex64>
*
* var re = realf( z );
* // returns 6.0
*
* var im = imagf( z );
* // returns 6.0
*/
setReadOnly( Complex64Array.prototype, 'reduce', function reduce( reducer, initialValue ) {
var buf;
var acc;
var len;
var v;
var i;

if ( !isComplexArray( this ) ) {
throw new TypeError( 'invalid invocation. `this` is not a complex number array.' );
}
if ( !isFunction( reducer ) ) {
throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', reducer ) );
}
buf = this._buffer;
len = this._length;
if ( arguments.length > 1 ) {
i = 0;
acc = initialValue;
} else {
if ( len === 0 ) {
throw new TypeError( 'invalid argument. If not provided an initial value, input array must contain at least one element.' );
}
acc = getComplex64( buf, 0 );
i = 1;
}
for ( ; i < len; i++ ) {
v = getComplex64( buf, i );
acc = reducer( acc, v, i, this );
}
return acc;
});

/**
* Reverses an array in-place.
*
Expand Down