Skip to content

Commit b376e7e

Browse files
committed
Auto-generated commit
1 parent 6afc64f commit b376e7e

File tree

6 files changed

+508
-5
lines changed

6 files changed

+508
-5
lines changed

CHANGELOG.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,7 @@ This release closes the following issue:
481481

482482
##### Features
483483

484+
- [`41af546`](https://github.com/stdlib-js/stdlib/commit/41af5467a9067f22ec8facdb535389bac10093bd) - add `reduceRight` method to `array/fixed-endian-factory` [(#3300)](https://github.com/stdlib-js/stdlib/pull/3300)
484485
- [`b679536`](https://github.com/stdlib-js/stdlib/commit/b679536e73bb91b1f9194bb6897232a3aa5cf2f2) - add `reduce` method to `array/fixed-endian-factory` [(#3296)](https://github.com/stdlib-js/stdlib/pull/3296)
485486
- [`6ddda90`](https://github.com/stdlib-js/stdlib/commit/6ddda90477b17bb06353e539d6d6dbf9207e6b8f) - add `join` method to `array/fixed-endian-factory` [(#3287)](https://github.com/stdlib-js/stdlib/pull/3287)
486487
- [`c8143c4`](https://github.com/stdlib-js/stdlib/commit/c8143c4f1f5254c67916bb1548aa5443d282e263) - add `lastIndexOf` method to `array/fixed-endian-factory` [(#3281)](https://github.com/stdlib-js/stdlib/pull/3281)
@@ -499,9 +500,9 @@ This release closes the following issue:
499500

500501
##### Closed Issues
501502

502-
A total of 8 issues were closed in this release:
503+
A total of 9 issues were closed in this release:
503504

504-
[#3135](https://github.com/stdlib-js/stdlib/issues/3135), [#3138](https://github.com/stdlib-js/stdlib/issues/3138), [#3146](https://github.com/stdlib-js/stdlib/issues/3146), [#3147](https://github.com/stdlib-js/stdlib/issues/3147), [#3149](https://github.com/stdlib-js/stdlib/issues/3149), [#3150](https://github.com/stdlib-js/stdlib/issues/3150), [#3151](https://github.com/stdlib-js/stdlib/issues/3151), [#3155](https://github.com/stdlib-js/stdlib/issues/3155)
505+
[#3135](https://github.com/stdlib-js/stdlib/issues/3135), [#3138](https://github.com/stdlib-js/stdlib/issues/3138), [#3146](https://github.com/stdlib-js/stdlib/issues/3146), [#3147](https://github.com/stdlib-js/stdlib/issues/3147), [#3149](https://github.com/stdlib-js/stdlib/issues/3149), [#3150](https://github.com/stdlib-js/stdlib/issues/3150), [#3151](https://github.com/stdlib-js/stdlib/issues/3151), [#3152](https://github.com/stdlib-js/stdlib/issues/3152), [#3155](https://github.com/stdlib-js/stdlib/issues/3155)
505506

506507
</section>
507508

@@ -555,9 +556,9 @@ A total of 8 issues were closed in this release:
555556

556557
### Closed Issues
557558

558-
A total of 15 issues were closed in this release:
559+
A total of 16 issues were closed in this release:
559560

560-
[#3135](https://github.com/stdlib-js/stdlib/issues/3135), [#3138](https://github.com/stdlib-js/stdlib/issues/3138), [#3146](https://github.com/stdlib-js/stdlib/issues/3146), [#3147](https://github.com/stdlib-js/stdlib/issues/3147), [#3149](https://github.com/stdlib-js/stdlib/issues/3149), [#3150](https://github.com/stdlib-js/stdlib/issues/3150), [#3151](https://github.com/stdlib-js/stdlib/issues/3151), [#3155](https://github.com/stdlib-js/stdlib/issues/3155), [#3164](https://github.com/stdlib-js/stdlib/issues/3164), [#3165](https://github.com/stdlib-js/stdlib/issues/3165), [#3176](https://github.com/stdlib-js/stdlib/issues/3176), [#3177](https://github.com/stdlib-js/stdlib/issues/3177), [#3178](https://github.com/stdlib-js/stdlib/issues/3178), [#3179](https://github.com/stdlib-js/stdlib/issues/3179), [#3180](https://github.com/stdlib-js/stdlib/issues/3180)
561+
[#3135](https://github.com/stdlib-js/stdlib/issues/3135), [#3138](https://github.com/stdlib-js/stdlib/issues/3138), [#3146](https://github.com/stdlib-js/stdlib/issues/3146), [#3147](https://github.com/stdlib-js/stdlib/issues/3147), [#3149](https://github.com/stdlib-js/stdlib/issues/3149), [#3150](https://github.com/stdlib-js/stdlib/issues/3150), [#3151](https://github.com/stdlib-js/stdlib/issues/3151), [#3152](https://github.com/stdlib-js/stdlib/issues/3152), [#3155](https://github.com/stdlib-js/stdlib/issues/3155), [#3164](https://github.com/stdlib-js/stdlib/issues/3164), [#3165](https://github.com/stdlib-js/stdlib/issues/3165), [#3176](https://github.com/stdlib-js/stdlib/issues/3176), [#3177](https://github.com/stdlib-js/stdlib/issues/3177), [#3178](https://github.com/stdlib-js/stdlib/issues/3178), [#3179](https://github.com/stdlib-js/stdlib/issues/3179), [#3180](https://github.com/stdlib-js/stdlib/issues/3180)
561562

562563
</section>
563564

@@ -588,6 +589,7 @@ A total of 8 people contributed to this release. Thank you to the following cont
588589

589590
<details>
590591

592+
- [`41af546`](https://github.com/stdlib-js/stdlib/commit/41af5467a9067f22ec8facdb535389bac10093bd) - **feat:** add `reduceRight` method to `array/fixed-endian-factory` [(#3300)](https://github.com/stdlib-js/stdlib/pull/3300) _(by Aayush Khanna, Philipp Burckhardt)_
591593
- [`b679536`](https://github.com/stdlib-js/stdlib/commit/b679536e73bb91b1f9194bb6897232a3aa5cf2f2) - **feat:** add `reduce` method to `array/fixed-endian-factory` [(#3296)](https://github.com/stdlib-js/stdlib/pull/3296) _(by Aayush Khanna)_
592594
- [`6ddda90`](https://github.com/stdlib-js/stdlib/commit/6ddda90477b17bb06353e539d6d6dbf9207e6b8f) - **feat:** add `join` method to `array/fixed-endian-factory` [(#3287)](https://github.com/stdlib-js/stdlib/pull/3287) _(by Aditya Sapra, Philipp Burckhardt)_
593595
- [`c8143c4`](https://github.com/stdlib-js/stdlib/commit/c8143c4f1f5254c67916bb1548aa5443d282e263) - **feat:** add `lastIndexOf` method to `array/fixed-endian-factory` [(#3281)](https://github.com/stdlib-js/stdlib/pull/3281) _(by Aditya Sapra, Philipp Burckhardt)_

fixed-endian-factory/README.md

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,7 @@ var out = arr.map( fcn, context );
608608
var count = context.count;
609609
// returns 3;
610610
```
611-
611+
612612
<a name="method-reduce"></a>
613613

614614
#### TypedArray.prototype.reduce( reducerFn\[, initialValue] )
@@ -655,6 +655,52 @@ var out = arr.reduce( reducer, 0 );
655655
// returns 2
656656
```
657657

658+
<a name="method-reduce-right"></a>
659+
660+
#### TypedArray.prototype.reduceRight( reducerFn\[, initialValue] )
661+
662+
Applies a provided callback function to each element of the array, in reverse order, passing in the return value from the calculation on the following element and returning the accumulated result upon completion.
663+
664+
```javascript
665+
function reducer( acc, v ) {
666+
return ( acc && v );
667+
}
668+
669+
var Float64ArrayFE = fixedEndianFactory( 'float64' );
670+
671+
var arr = new Float64ArrayFE( 'little-endian', [ 1.0, 0.0, 1.0 ] );
672+
// returns <Float64ArrayFE>
673+
674+
var out = arr.reduceRight( reducer );
675+
// returns 0.0
676+
```
677+
678+
The reducer function is provided four arguments:
679+
680+
- **acc**: accumulated result.
681+
- **value**: current array element.
682+
- **index**: current array element index.
683+
- **arr**: the array on which this method was called.
684+
685+
By default, the function initializes the accumulated result to the first element in the array and passes the second array element as `value` during the first invocation of the provided callback. To begin accumulation from a different starting value and pass in the first array element as `value` during the first invocation of the provided callback, provide an `initialValue` argument.
686+
687+
```javascript
688+
function reducer( acc, v ) {
689+
if ( v ) {
690+
return acc + 1;
691+
}
692+
return acc;
693+
}
694+
695+
var Float64ArrayFE = fixedEndianFactory( 'float64' );
696+
697+
var arr = new Float64ArrayFE( 'little-endian', [ 1.0, 0.0, 1.0 ] );
698+
// returns <Float64ArrayFE>
699+
700+
var out = arr.reduceRight( reducer, 0 );
701+
// returns 2
702+
```
703+
658704
<a name="method-set"></a>
659705

660706
#### TypedArrayFE.prototype.set( arr\[, offset] )
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive;
25+
var pkg = require( './../package.json' ).name;
26+
var factory = require( './../lib' );
27+
28+
29+
// VARIABLES //
30+
31+
var Float64ArrayFE = factory( 'float64' );
32+
33+
34+
// FUNCTIONS //
35+
36+
/**
37+
* Reducer function.
38+
*
39+
* @private
40+
* @param {integer} acc - accumulated value
41+
* @param {number} value - current array element
42+
* @param {integer} index - current array index
43+
* @returns {integer} accumulated value
44+
*/
45+
function reducer( acc, value ) {
46+
if ( value ) {
47+
return acc + 1;
48+
}
49+
return acc;
50+
}
51+
52+
53+
// MAIN //
54+
55+
bench( pkg+':reduceRight', function benchmark( b ) {
56+
var out;
57+
var arr;
58+
var i;
59+
60+
arr = new Float64ArrayFE( 'little-endian', [ 1.0, 0.0, 0.0, 1.0 ] );
61+
62+
b.tic();
63+
for ( i = 0; i < b.iterations; i++ ) {
64+
out = arr.reduceRight( reducer, 0 );
65+
if ( typeof out !== 'number' ) {
66+
b.fail( 'should return a number' );
67+
}
68+
}
69+
b.toc();
70+
if ( !isInteger( out ) ) {
71+
b.fail( 'should return an integer' );
72+
}
73+
b.pass( 'benchmark finished' );
74+
b.end();
75+
});
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var pow = require( '@stdlib/math/base/special/pow' );
25+
var zeroTo = require( './../../zero-to' );
26+
var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive;
27+
var pkg = require( './../package.json' ).name;
28+
var factory = require( './../lib' );
29+
30+
31+
// VARIABLES //
32+
33+
var Float64ArrayFE = factory( 'float64' );
34+
35+
36+
// FUNCTIONS //
37+
38+
/**
39+
* Reducer function.
40+
*
41+
* @private
42+
* @param {integer} acc - accumulated value
43+
* @param {number} value - current array element
44+
* @param {integer} index - current array index
45+
* @returns {integer} accumulated value
46+
*/
47+
function reducer( acc, value ) {
48+
if ( value ) {
49+
return acc + 1;
50+
}
51+
return acc;
52+
}
53+
54+
/**
55+
* Creates a benchmark function.
56+
*
57+
* @private
58+
* @param {PositiveInteger} len - array length
59+
* @returns {Function} benchmark function
60+
*/
61+
function createBenchmark( len ) {
62+
var arr = new Float64ArrayFE( 'little-endian', zeroTo( len ) );
63+
return benchmark;
64+
65+
/**
66+
* Benchmark function.
67+
*
68+
* @private
69+
* @param {Benchmark} b - benchmark instance
70+
*/
71+
function benchmark( b ) {
72+
var out;
73+
var i;
74+
75+
b.tic();
76+
for ( i = 0; i < b.iterations; i++ ) {
77+
out = arr.reduceRight( reducer );
78+
if ( typeof out !== 'number' ) {
79+
b.fail( 'should return a number' );
80+
}
81+
}
82+
b.toc();
83+
if ( !isInteger( out ) ) {
84+
b.fail( 'should return an integer' );
85+
}
86+
b.pass( 'benchmark finished' );
87+
b.end();
88+
}
89+
}
90+
91+
92+
// MAIN //
93+
94+
/**
95+
* Main execution sequence.
96+
*
97+
* @private
98+
*/
99+
function main() {
100+
var len;
101+
var min;
102+
var max;
103+
var f;
104+
var i;
105+
106+
min = 1; // 10^min
107+
max = 6; // 10^max
108+
109+
for ( i = min; i <= max; i++ ) {
110+
len = pow( 10, i );
111+
f = createBenchmark( len );
112+
bench( pkg+':reduceRight:len='+len, f );
113+
}
114+
}
115+
116+
main();

fixed-endian-factory/lib/main.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -811,6 +811,48 @@ function factory( dtype ) { // eslint-disable-line max-lines-per-function, stdli
811811
return acc;
812812
});
813813

814+
/**
815+
* Applies a provided callback function to each element of the array, in reverse order, passing in the return value from the calculation on the following element and returning the accumulated result upon completion.
816+
*
817+
* @name reduceRight
818+
* @memberof TypedArray.prototype
819+
* @type {Function}
820+
* @param {Function} reducer - callback function
821+
* @param {*} [initialValue] - initial value
822+
* @throws {TypeError} `this` must be a typed array
823+
* @throws {Error} if not provided an initial value, the array must have at least one element
824+
* @returns {*} accumulated result
825+
*/
826+
setReadOnly( TypedArray.prototype, 'reduceRight', function reduceRight( reducer, initialValue ) {
827+
var buf;
828+
var len;
829+
var acc;
830+
var i;
831+
832+
if ( !isTypedArray( this ) ) {
833+
throw new TypeError( format( 'invalid invocation. `this` is not %s %s.', CHAR2ARTICLE[ dtype[0] ], CTOR_NAME ) );
834+
}
835+
if ( !isFunction( reducer ) ) {
836+
throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', reducer ) );
837+
}
838+
buf = this._buffer;
839+
len = this._length;
840+
if ( arguments.length > 1 ) {
841+
acc = initialValue;
842+
i = len - 1;
843+
} else {
844+
if ( len === 0 ) {
845+
throw new Error( 'invalid operation. If not provided an initial value, an array must contain at least one element.' );
846+
}
847+
acc = buf[ GETTER ]( ( len - 1 ) * BYTES_PER_ELEMENT, this._isLE );
848+
i = len - 2;
849+
}
850+
for ( ; i >= 0; i-- ) {
851+
acc = reducer( acc, buf[ GETTER ]( i * BYTES_PER_ELEMENT, this._isLE ), i, this );
852+
}
853+
return acc;
854+
});
855+
814856
/**
815857
* Sets an array element.
816858
*

0 commit comments

Comments
 (0)