Skip to content

Commit 072052b

Browse files
headlessNodekgryte
andauthored
feat: add array/base/for-each
PR-URL: #5319 Closes: stdlib-js/metr-issue-tracker#17 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent 54e5f3f commit 072052b

File tree

10 files changed

+912
-0
lines changed

10 files changed

+912
-0
lines changed
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# forEach
22+
23+
> Invoke a callback funcion once for each array element.
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<!-- Package usage documentation. -->
34+
35+
<section class="usage">
36+
37+
## Usage
38+
39+
```javascript
40+
var forEach = require( '@stdlib/array/base/for-each' );
41+
```
42+
43+
#### forEach( x, fcn\[, thisArg] )
44+
45+
Invokes a callback function once for each array element.
46+
47+
```javascript
48+
var naryFunction = require( '@stdlib/utils/nary-function' );
49+
var log = require( '@stdlib/console/log' );
50+
51+
var x = [ 1, 2, 3, 4 ];
52+
53+
forEach( x, naryFunction( log, 1 ) );
54+
```
55+
56+
The function accepts the following arguments:
57+
58+
- **x**: input array.
59+
- **fcn**: callback to apply.
60+
- **thisArg**: callback execution context _(optional)_.
61+
62+
To set the callback function execution context, provide a `thisArg`.
63+
64+
<!-- eslint-disable no-invalid-this -->
65+
66+
```javascript
67+
function accumulate( z ) {
68+
this.sum += z;
69+
}
70+
71+
var x = [ 1, 2, 3, 4 ];
72+
73+
var ctx = {
74+
'sum': 0
75+
};
76+
77+
forEach( x, accumulate, ctx );
78+
var sum = ctx.sum;
79+
// returns 10
80+
```
81+
82+
The callback function is provided the following arguments:
83+
84+
- **value**: current array element.
85+
- **index**: current array element index.
86+
- **arr**: the input array.
87+
88+
</section>
89+
90+
<!-- /.usage -->
91+
92+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
93+
94+
<section class="notes">
95+
96+
## Notes
97+
98+
- If provided an array-like object having a `forEach` method, the function defers execution to that method and assumes that the method API has the following signature:
99+
100+
```text
101+
x.forEach( fcn, thisArg )
102+
```
103+
104+
- The function support array-like objects having getter and setter accessors for array element access (e.g., [`@stdlib/array/base/accessor`][@stdlib/array/base/accessor]).
105+
106+
</section>
107+
108+
<!-- /.notes -->
109+
110+
<!-- Package usage examples. -->
111+
112+
<section class="examples">
113+
114+
## Examples
115+
116+
<!-- eslint no-undef: "error" -->
117+
118+
```javascript
119+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
120+
var naryFunction = require( '@stdlib/utils/nary-function' );
121+
var log = require( '@stdlib/console/log' );
122+
var forEach = require( '@stdlib/array/base/for-each' );
123+
124+
var x = discreteUniform( 10, 0, 10, {
125+
'dtype': 'float64'
126+
});
127+
128+
forEach( x, naryFunction( log, 1 ) );
129+
```
130+
131+
</section>
132+
133+
<!-- /.examples -->
134+
135+
<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
136+
137+
<section class="references">
138+
139+
</section>
140+
141+
<!-- /.references -->
142+
143+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
144+
145+
<section class="related">
146+
147+
</section>
148+
149+
<!-- /.related -->
150+
151+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
152+
153+
<section class="links">
154+
155+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
156+
157+
</section>
158+
159+
<!-- /.links -->
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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 pow = require( '@stdlib/math/base/special/pow' );
26+
var zeroTo = require( '@stdlib/array/base/zero-to' );
27+
var pkg = require( './../package.json' ).name;
28+
var forEach = require( './../lib' );
29+
30+
31+
// FUNCTIONS //
32+
33+
/**
34+
* Callback invoked for each ndarray element.
35+
*
36+
* @private
37+
* @param {number} value - array element
38+
* @throws {Error} unexpected error
39+
*/
40+
function clbk( value ) {
41+
if ( isnan( value ) ) {
42+
throw new Error( 'unexpected error' );
43+
}
44+
}
45+
46+
/**
47+
* Creates a benchmark function.
48+
*
49+
* @private
50+
* @param {PositiveInteger} len - array length
51+
* @returns {Function} benchmark function
52+
*/
53+
function createBenchmark( len ) {
54+
var x = zeroTo( len );
55+
return benchmark;
56+
57+
/**
58+
* Benchmark function.
59+
*
60+
* @private
61+
* @param {Benchmark} b - benchmark instance
62+
*/
63+
function benchmark( b ) {
64+
var i;
65+
66+
b.tic();
67+
for ( i = 0; i < b.iterations; i++ ) {
68+
forEach( x, clbk );
69+
if ( isnan( x[ i%len ] ) ) {
70+
b.fail( 'should not return NaN' );
71+
}
72+
}
73+
b.toc();
74+
if ( isnan( x[ i%len ] ) ) {
75+
b.fail( 'should not return NaN' );
76+
}
77+
b.pass( 'benchmark finished' );
78+
b.end();
79+
}
80+
}
81+
82+
83+
// MAIN //
84+
85+
/**
86+
* Main execution sequence.
87+
*
88+
* @private
89+
*/
90+
function main() {
91+
var len;
92+
var min;
93+
var max;
94+
var f;
95+
var i;
96+
97+
min = 1; // 10^min
98+
max = 6; // 10^max
99+
100+
for ( i = min; i <= max; i++ ) {
101+
len = pow( 10, i );
102+
103+
f = createBenchmark( len );
104+
bench( pkg+':dtype=generic,len='+len, f );
105+
}
106+
}
107+
108+
main();
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
{{alias}}( x, fcn[, thisArg] )
3+
Invoke a callback function once for each array element.
4+
5+
The callback function is provided the following arguments:
6+
7+
- value: current array element.
8+
- index: current array element index.
9+
- arr: the input array.
10+
11+
If provided an array-like object having a `forEach` method , the function
12+
defers execution to that method and assumes that the method has the
13+
following signature:
14+
15+
x.forEach( clbk, thisArg )
16+
17+
Parameters
18+
----------
19+
x: Array|TypedArray|Object
20+
Input array.
21+
22+
fcn: Function
23+
Callback function.
24+
25+
thisArg: any (optional)
26+
Callback function execution context.
27+
28+
Examples
29+
--------
30+
> var x = [ 1, 2, 3, 4 ];
31+
> function f( v ) { if ( v !== v ) { throw new Error( '...' ); } };
32+
> {{alias}}( x, f );
33+
34+
See Also
35+
--------
36+

0 commit comments

Comments
 (0)