Skip to content

Commit 956a462

Browse files
committed
feat: add forEach method
1 parent de1ef8b commit 956a462

File tree

5 files changed

+459
-0
lines changed

5 files changed

+459
-0
lines changed

Diff for: lib/node_modules/@stdlib/array/fixed-endian-factory/README.md

+59
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,65 @@ var v = arr.get( 0 );
306306
// returns 1.0
307307
```
308308

309+
<a name="method-for-each"></a>
310+
311+
#### TypedArrayFE.prototype.forEach( callbackFn\[, thisArg] )
312+
313+
Invokes a function once for each array element.
314+
315+
```javascript
316+
function log( v, i ) {
317+
console.log( '%s: %s', i.toString(), v.toString() );
318+
}
319+
320+
var Float64ArrayFE = fixedEndianFactory( 'float64' );
321+
322+
var arr = new Float64ArrayFE( 'little-endian', 3 );
323+
324+
arr.set( 1.5, 0 );
325+
arr.set( 2.5, 1 );
326+
arr.set( 3.5, 2 );
327+
328+
arr.forEach( log );
329+
/* =>
330+
0: 1.5
331+
1: 2.5
332+
2: 3.5
333+
*/
334+
```
335+
336+
The invoked function is provided three arguments:
337+
338+
- **value**: current array element.
339+
- **index**: current array element index.
340+
- **arr**: the array on which this method was called.
341+
342+
To set the function execution context, provide a `thisArg`.
343+
344+
```javascript
345+
function fcn( v, i ) {
346+
this.count += 1;
347+
console.log( '%s: %s', i.toString(), v.toString() );
348+
}
349+
350+
var Float64ArrayFE = fixedEndianFactory( 'float64' );
351+
352+
var arr = new Float64ArrayFE( 'little-endian', 3 );
353+
354+
var context = {
355+
'count': 0
356+
};
357+
358+
arr.set( 1.0, 0 );
359+
arr.set( 2.0, 1 );
360+
arr.set( 3.0, 2 );
361+
362+
arr.forEach( fcn, context );
363+
364+
var count = context.count;
365+
// returns 3
366+
```
367+
309368
<a name="method-get"></a>
310369

311370
#### TypedArrayFE.prototype.get( i )
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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 isnan = require( '@stdlib/math/base/assert/is-nan' );
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+
// MAIN //
35+
36+
bench( pkg+':forEach', function benchmark( b ) {
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+
arr.forEach( check );
45+
if ( arr.length !== 4 ) {
46+
b.fail( 'should not change an array length' );
47+
}
48+
}
49+
b.toc();
50+
if ( arr.length !== 4 ) {
51+
b.fail( 'should not change an array length' );
52+
}
53+
b.pass( 'benchmark finished' );
54+
b.end();
55+
56+
function check( v ) {
57+
if ( isnan( v ) ) {
58+
b.fail( 'should not return NaN' );
59+
}
60+
}
61+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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( '@stdlib/array/zero-to' );
26+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
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+
* Creates a benchmark function.
40+
*
41+
* @private
42+
* @param {PositiveInteger} len - array length
43+
* @returns {Function} benchmark function
44+
*/
45+
function createBenchmark( len ) {
46+
var arr = new Float64ArrayFE( 'little-endian', zeroTo( len ) );
47+
return benchmark;
48+
49+
/**
50+
* Benchmark function.
51+
*
52+
* @private
53+
* @param {Benchmark} b - benchmark instance
54+
*/
55+
function benchmark( b ) {
56+
var i;
57+
58+
b.tic();
59+
for ( i = 0; i < b.iterations; i++ ) {
60+
arr.forEach( callback );
61+
if ( arr.length !== len ) {
62+
b.fail( 'should not change an array length' );
63+
}
64+
}
65+
b.toc();
66+
if ( arr.length !== len ) {
67+
b.fail( 'should not change an array length' );
68+
}
69+
b.pass( 'benchmark finished' );
70+
b.end();
71+
72+
function callback( value ) {
73+
if ( isnan( value ) ) {
74+
throw new Error( 'something went wrong' );
75+
}
76+
}
77+
}
78+
}
79+
80+
81+
// MAIN //
82+
83+
/**
84+
* Main execution sequence.
85+
*
86+
* @private
87+
*/
88+
function main() {
89+
var len;
90+
var min;
91+
var max;
92+
var f;
93+
var i;
94+
95+
min = 1; // 10^min
96+
max = 6; // 10^max
97+
98+
for ( i = min; i <= max; i++ ) {
99+
len = pow( 10, i );
100+
f = createBenchmark( len );
101+
bench( pkg+':forEach:len='+len, f );
102+
}
103+
}
104+
105+
main();

Diff for: lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js

+27
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,33 @@ function factory( dtype ) { // eslint-disable-line max-lines-per-function, stdli
507507
*/
508508
setReadOnly( TypedArray.prototype, 'BYTES_PER_ELEMENT', TypedArray.BYTES_PER_ELEMENT );
509509

510+
/**
511+
* Invokes a function once for each array element.
512+
*
513+
* @name forEach
514+
* @memberof TypedArray.prototype
515+
* @type {Function}
516+
* @param {Function} fcn - function to invoke
517+
* @param {*} [thisArg] - function invocation context
518+
* @throws {TypeError} `this` must be a typed array instance
519+
* @throws {TypeError} first argument must be a function
520+
*/
521+
setReadOnly( TypedArray.prototype, 'forEach', function forEach( fcn, thisArg ) {
522+
var buf;
523+
var i;
524+
525+
if ( !isTypedArray( this ) ) {
526+
throw new TypeError( format( 'invalid invocation. `this` is not %s %s.', CHAR2ARTICLE[ dtype[0] ], CTOR_NAME ) );
527+
}
528+
if ( !isFunction( fcn ) ) {
529+
throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', fcn ) );
530+
}
531+
buf = this._buffer;
532+
for ( i = 0; i < this._length; i++ ) {
533+
fcn.call( thisArg, buf[ GETTER ]( i*BYTES_PER_ELEMENT, this._isLE ), i, this );
534+
}
535+
});
536+
510537
/**
511538
* Returns an array element.
512539
*

0 commit comments

Comments
 (0)