Skip to content

Commit c208911

Browse files
committed
Auto-generated commit
1 parent e9ff7b7 commit c208911

File tree

8 files changed

+499
-6
lines changed

8 files changed

+499
-6
lines changed

CHANGELOG.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
### Features
1212

13+
- [`1242bbf`](https://github.com/stdlib-js/stdlib/commit/1242bbf3e43a142f8d0bd4a66aece5baa33c03fe) - add `filter` method to `array/fixed-endian-factory` [(#3278)](https://github.com/stdlib-js/stdlib/pull/3278)
1314
- [`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)
1415
- [`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)
1516
- [`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)
@@ -30,9 +31,9 @@
3031

3132
### Closed Issues
3233

33-
A total of 9 issues were closed in this release:
34+
A total of 10 issues were closed in this release:
3435

35-
[#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)
36+
[#3135](https://github.com/stdlib-js/stdlib/issues/3135), [#3138](https://github.com/stdlib-js/stdlib/issues/3138), [#3140](https://github.com/stdlib-js/stdlib/issues/3140), [#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)
3637

3738
</section>
3839

@@ -44,6 +45,7 @@ A total of 9 issues were closed in this release:
4445

4546
<details>
4647

48+
- [`1242bbf`](https://github.com/stdlib-js/stdlib/commit/1242bbf3e43a142f8d0bd4a66aece5baa33c03fe) - **feat:** add `filter` method to `array/fixed-endian-factory` [(#3278)](https://github.com/stdlib-js/stdlib/pull/3278) _(by Aayush Khanna, Philipp Burckhardt)_
4749
- [`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)_
4850
- [`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)_
4951
- [`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)_

README.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,66 @@ var count = context.count;
484484
// returns 3
485485
```
486486

487+
<a name="method-filter"></a>
488+
489+
#### TypedArrayFE.prototype.filter( predicate\[, thisArg] )
490+
491+
Returns a new array containing the elements of an array which pass a test implemented by a predicate function.
492+
493+
```javascript
494+
function predicate( v ) {
495+
return ( v % 2 === 0 );
496+
}
497+
498+
var Float64ArrayFE = fixedEndianFactory( 'float64' );
499+
500+
var arr = new Float64ArrayFE( 'little-endian', [ 1.0, 2.0, 3.0, 4.0 ] );
501+
502+
var out = arr.filter( predicate );
503+
// returns <Float64ArrayFE>
504+
505+
var len = out.length;
506+
// returns 2
507+
508+
var v = out.get( 0 );
509+
// returns 2.0
510+
511+
v = out.get( 1 );
512+
// return 4.0
513+
```
514+
515+
The `predicate` function is provided three arguments:
516+
517+
- **value**: current array element.
518+
- **index**: current array element index.
519+
- **arr**: the array on which this method was called.
520+
521+
To set the function execution context, provide a `thisArg`.
522+
523+
```javascript
524+
function predicate( v, i ) {
525+
this.count += 1;
526+
return ( v % 2 === 0 );
527+
}
528+
529+
var context = {
530+
'count': 0
531+
};
532+
533+
var Float64ArrayFE = fixedEndianFactory( 'float64' );
534+
535+
var arr = new Float64ArrayFE( 'little-endian', [ 1.0, 2.0, 3.0, 4.0 ] );
536+
537+
var out = arr.filter( predicate, context );
538+
// returns <Float64ArrayFE>
539+
540+
var len = out.length;
541+
// returns 2
542+
543+
var count = context.count;
544+
// returns 4
545+
```
546+
487547
<a name="method-get"></a>
488548

489549
#### TypedArrayFE.prototype.get( i )

benchmark/benchmark.filter.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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-harness' );
24+
var pkg = require( './../package.json' ).name;
25+
var factory = require( './../lib' );
26+
27+
28+
// VARIABLES //
29+
30+
var Float64ArrayFE = factory( 'float64' );
31+
32+
33+
// MAIN //
34+
35+
bench( pkg+':filter', function benchmark( b ) {
36+
var out;
37+
var arr;
38+
var i;
39+
40+
arr = new Float64ArrayFE( 'little-endian', [ 1.0, 2.0, 2.0, 1.0 ] );
41+
42+
b.tic();
43+
for ( i = 0; i < b.iterations; i++ ) {
44+
out = arr.filter( predicate );
45+
if ( typeof out !== 'object' ) {
46+
b.fail( 'should return an object' );
47+
}
48+
}
49+
b.toc();
50+
if ( !( out instanceof Float64ArrayFE ) ) {
51+
b.fail( 'should return a typed array' );
52+
}
53+
b.pass( 'benchmark finished' );
54+
b.end();
55+
56+
function predicate( v ) {
57+
return v % 2 === 0;
58+
}
59+
});
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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-harness' );
24+
var pow = require( '@stdlib/math-base-special-pow' );
25+
var zeroTo = require( '@stdlib/array-zero-to' );
26+
var pkg = require( './../package.json' ).name;
27+
var factory = require( './../lib' );
28+
29+
30+
// VARIABLES //
31+
32+
var Float64ArrayFE = factory( 'float64' );
33+
34+
35+
// FUNCTIONS //
36+
37+
/**
38+
* Predicate function.
39+
*
40+
* @private
41+
* @param {number} value - array element
42+
* @param {NonNegativeInteger} idx - array element index
43+
* @param {TypedArray} arr - array instance
44+
* @returns {boolean} boolean indicating whether a value passes a test
45+
*/
46+
function predicate( value ) {
47+
return value % 2 === 0;
48+
}
49+
50+
/**
51+
* Creates a benchmark function.
52+
*
53+
* @private
54+
* @param {PositiveInteger} len - array length
55+
* @returns {Function} benchmark function
56+
*/
57+
function createBenchmark( len ) {
58+
var arr = new Float64ArrayFE( 'little-endian', zeroTo( len ) );
59+
return benchmark;
60+
61+
/**
62+
* Benchmark function.
63+
*
64+
* @private
65+
* @param {Benchmark} b - benchmark instance
66+
*/
67+
function benchmark( b ) {
68+
var out;
69+
var i;
70+
71+
b.tic();
72+
for ( i = 0; i < b.iterations; i++ ) {
73+
out = arr.filter( predicate );
74+
if ( typeof out !== 'object' ) {
75+
b.fail( 'should return an object' );
76+
}
77+
}
78+
b.toc();
79+
if ( !( out instanceof Float64ArrayFE ) ) {
80+
b.fail( 'should return a typed array' );
81+
}
82+
b.pass( 'benchmark finished' );
83+
b.end();
84+
}
85+
}
86+
87+
88+
// MAIN //
89+
90+
/**
91+
* Main execution sequence.
92+
*
93+
* @private
94+
*/
95+
function main() {
96+
var len;
97+
var min;
98+
var max;
99+
var f;
100+
var i;
101+
102+
min = 1; // 10^min
103+
max = 6; // 10^max
104+
105+
for ( i = min; i <= max; i++ ) {
106+
len = pow( 10, i );
107+
f = createBenchmark( len );
108+
bench( pkg+':filter:len='+len, f );
109+
}
110+
}
111+
112+
main();

dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/main.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,41 @@ function factory( dtype ) { // eslint-disable-line max-lines-per-function, stdli
609609
}
610610
});
611611

612+
/**
613+
* Returns a new array containing the elements of an array which pass a test implemented by a predicate function.
614+
*
615+
* @name filter
616+
* @memberof TypedArray.prototype
617+
* @type {Function}
618+
* @param {Function} predicate - test function
619+
* @param {*} [thisArg] - predicate function execution context
620+
* @throws {TypeError} `this` must be a typed array instance
621+
* @throws {TypeError} first argument must be a function
622+
* @returns {TypedArray} typed array
623+
*/
624+
setReadOnly( TypedArray.prototype, 'filter', function filter( predicate, thisArg ) {
625+
var buf;
626+
var out;
627+
var i;
628+
var v;
629+
630+
if ( !isTypedArray( this ) ) {
631+
throw new TypeError( format( 'invalid invocation. `this` is not %s %s.', CHAR2ARTICLE[ dtype[0] ], CTOR_NAME ) );
632+
}
633+
if ( !isFunction( predicate ) ) {
634+
throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', predicate ) );
635+
}
636+
buf = this._buffer;
637+
out = [];
638+
for ( i = 0; i < this._length; i++) {
639+
v = buf[ GETTER ]( i*BYTES_PER_ELEMENT, this._isLE );
640+
if ( predicate.call( thisArg, v, i, this ) ) {
641+
out.push( v );
642+
}
643+
}
644+
return new this.constructor( flag2byteOrder( this._isLE ), out );
645+
});
646+
612647
/**
613648
* Returns an array element.
614649
*

0 commit comments

Comments
 (0)