-
-
Notifications
You must be signed in to change notification settings - Fork 1k
feat: add array/base/for-each
#5319
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
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1e166ae
feat: add array foreach
headlessNode d54376d
docs: apply suggestions from code review
headlessNode 070cee1
docs: apply suggestions from code review
headlessNode e81c6fd
Apply suggestions from code review
kgryte b57b376
refactor: use overloads to preserve type information
kgryte 85ce93c
docs: fix import
kgryte d705c15
docs: fix comment
kgryte b6b6221
Apply suggestions from code review
kgryte File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| <!-- | ||
|
|
||
| @license Apache-2.0 | ||
|
|
||
| Copyright (c) 2025 The Stdlib Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
|
|
||
| --> | ||
|
|
||
| # forEach | ||
|
|
||
| > Invoke a callback funcion once for each array element. | ||
|
|
||
| <!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> | ||
|
|
||
| <section class="intro"> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.intro --> | ||
|
|
||
| <!-- Package usage documentation. --> | ||
|
|
||
| <section class="usage"> | ||
|
|
||
| ## Usage | ||
|
|
||
| ```javascript | ||
| var forEach = require( '@stdlib/array/base/for-each' ); | ||
| ``` | ||
|
|
||
| #### forEach( x, fcn\[, thisArg] ) | ||
|
|
||
| Invokes a callback function once for each array element. | ||
|
|
||
| ```javascript | ||
| var naryFunction = require( '@stdlib/utils/nary-function' ); | ||
| var log = require( '@stdlib/console/log' ); | ||
|
|
||
| var x = [ 1, 2, 3, 4 ]; | ||
|
|
||
| forEach( x, naryFunction( log, 1 ) ); | ||
| ``` | ||
|
|
||
| The function accepts the following arguments: | ||
|
|
||
| - **x**: input array. | ||
| - **fcn**: callback to apply. | ||
| - **thisArg**: callback execution context _(optional)_. | ||
|
|
||
| To set the callback function execution context, provide a `thisArg`. | ||
|
|
||
| <!-- eslint-disable no-invalid-this, max-len --> | ||
|
|
||
| ```javascript | ||
| function accumulate( z ) { | ||
| this.sum += z; | ||
| } | ||
|
|
||
| var x = [ 1, 2, 3, 4 ]; | ||
|
|
||
| var ctx = { | ||
| 'sum': 0 | ||
| }; | ||
|
|
||
| forEach( x, accumulate, ctx ); | ||
| var sum = ctx.sum; | ||
| // returns 10 | ||
| ``` | ||
|
|
||
| The callback function is provided the following arguments: | ||
|
|
||
| - **value**: current array element. | ||
| - **index**: current array element index. | ||
| - **arr**: the input array. | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.usage --> | ||
|
|
||
| <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||
|
|
||
| <section class="notes"> | ||
|
|
||
| ## Notes | ||
|
|
||
| - 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: | ||
|
|
||
| ```text | ||
| x.forEach( fcn, thisArg ) | ||
| ``` | ||
|
|
||
| - 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]) | ||
kgryte marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| </section> | ||
|
|
||
| <!-- /.notes --> | ||
|
|
||
| <!-- Package usage examples. --> | ||
|
|
||
| <section class="examples"> | ||
|
|
||
| ## Examples | ||
|
|
||
| <!-- eslint no-undef: "error" --> | ||
|
|
||
| ```javascript | ||
| var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); | ||
| var naryFunction = require( '@stdlib/utils/nary-function' ); | ||
| var log = require( '@stdlib/console/log' ); | ||
| var forEach = require( '@stdlib/array/base/for-each' ); | ||
|
|
||
| var x = discreteUniform( 10, 0, 10, { | ||
| 'dtype': 'float64' | ||
| }); | ||
|
|
||
| forEach( x, naryFunction( log, 1 ) ); | ||
| ``` | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.examples --> | ||
|
|
||
| <!-- 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. --> | ||
|
|
||
| <section class="references"> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.references --> | ||
|
|
||
| <!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> | ||
|
|
||
| <section class="related"> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.related --> | ||
|
|
||
| <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||
|
|
||
| <section class="links"> | ||
|
|
||
| [@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.links --> | ||
108 changes: 108 additions & 0 deletions
108
lib/node_modules/@stdlib/array/base/for-each/benchmark/benchmark.length.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| /** | ||
| * @license Apache-2.0 | ||
| * | ||
| * Copyright (c) 2025 The Stdlib Authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| 'use strict'; | ||
|
|
||
| // MODULES // | ||
|
|
||
| var bench = require( '@stdlib/bench' ); | ||
| var isnan = require( '@stdlib/math/base/assert/is-nan' ); | ||
| var pow = require( '@stdlib/math/base/special/pow' ); | ||
| var zeroTo = require( '@stdlib/array/base/zero-to' ); | ||
| var pkg = require( './../package.json' ).name; | ||
| var forEach = require( './../lib' ); | ||
|
|
||
|
|
||
| // FUNCTIONS // | ||
|
|
||
| /** | ||
| * Callback invoked for each ndarray element. | ||
| * | ||
| * @private | ||
| * @param {number} value - array element | ||
| * @throws {Error} unexpected error | ||
| */ | ||
| function clbk( value ) { | ||
| if ( isnan( value ) ) { | ||
| throw new Error( 'unexpected error' ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Creates a benchmark function. | ||
| * | ||
| * @private | ||
| * @param {PositiveInteger} len - array length | ||
| * @returns {Function} benchmark function | ||
| */ | ||
| function createBenchmark( len ) { | ||
| var x = zeroTo( len ); | ||
| return benchmark; | ||
|
|
||
| /** | ||
| * Benchmark function. | ||
| * | ||
| * @private | ||
| * @param {Benchmark} b - benchmark instance | ||
| */ | ||
| function benchmark( b ) { | ||
| var i; | ||
|
|
||
| b.tic(); | ||
| for ( i = 0; i < b.iterations; i++ ) { | ||
| forEach( x, clbk ); | ||
| if ( isnan( x[ i%len ] ) ) { | ||
| b.fail( 'should not return NaN' ); | ||
| } | ||
| } | ||
| b.toc(); | ||
| if ( isnan( x[ i%len ] ) ) { | ||
| b.fail( 'should not return NaN' ); | ||
| } | ||
| b.pass( 'benchmark finished' ); | ||
| b.end(); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| // MAIN // | ||
|
|
||
| /** | ||
| * Main execution sequence. | ||
| * | ||
| * @private | ||
| */ | ||
| function main() { | ||
| var len; | ||
| var min; | ||
| var max; | ||
| var f; | ||
| var i; | ||
|
|
||
| min = 1; // 10^min | ||
| max = 6; // 10^max | ||
|
|
||
| for ( i = min; i <= max; i++ ) { | ||
| len = pow( 10, i ); | ||
|
|
||
| f = createBenchmark( len ); | ||
| bench( pkg+':dtype=generic,len='+len, f ); | ||
| } | ||
| } | ||
|
|
||
| main(); |
36 changes: 36 additions & 0 deletions
36
lib/node_modules/@stdlib/array/base/for-each/docs/repl.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
|
|
||
| {{alias}}( x, fcn[, thisArg] ) | ||
| Invoke a callback function once for each array element. | ||
|
|
||
| The callback function is provided the following arguments: | ||
|
|
||
| - value: current array element. | ||
| - index: current array element index. | ||
| - arr: the input array. | ||
|
|
||
| If provided an array-like object having a `forEach` method , the function | ||
| defers execution to that method and assumes that the method has the | ||
| following signature: | ||
|
|
||
| x.forEach( clbk, thisArg ) | ||
|
|
||
| Parameters | ||
| ---------- | ||
| x: Array|TypedArray|Object | ||
| Input array. | ||
|
|
||
| fcn: Function | ||
| Callback function. | ||
|
|
||
| thisArg: any (optional) | ||
| Callback function execution context. | ||
|
|
||
| Examples | ||
| -------- | ||
| > var x = [ 1, 2, 3, 4 ]; | ||
| > function f( v ) { if ( v !== v ) { throw new Error( '...' ); } }; | ||
| > {{alias}}( x, f ); | ||
|
|
||
| See Also | ||
| -------- | ||
|
|
84 changes: 84 additions & 0 deletions
84
lib/node_modules/@stdlib/array/base/for-each/docs/types/index.d.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| /* | ||
| * @license Apache-2.0 | ||
| * | ||
| * Copyright (c) 2025 The Stdlib Authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| // TypeScript Version: 4.1 | ||
|
|
||
| /// <reference types="@stdlib/types"/> | ||
|
|
||
| import { Collection, AccessorArrayLike } from '@stdlib/types/array'; | ||
|
|
||
| /** | ||
| * Callback invoked for each array element. | ||
| */ | ||
| type Nullary<V> = ( this: V ) => void; | ||
|
|
||
| /** | ||
| * Callback invoked for each array element. | ||
| * | ||
| * @param value - current array element | ||
| */ | ||
| type Unary<T, V> = ( this: V, value: T ) => void; | ||
|
|
||
| /** | ||
| * Callback invoked for each array element. | ||
| * | ||
| * @param value - current array element | ||
| * @param index - current array element index | ||
| */ | ||
| type Binary<T, V> = ( this: V, value: T, indices: Array<number> ) => void; | ||
|
|
||
| /** | ||
| * Callback invoked for each array element. | ||
| * | ||
| * @param value - current array element | ||
| * @param index - current array element index | ||
| * @param arr - input array | ||
| */ | ||
| type Ternary<T, U, V> = ( this: V, value: T, indices: Array<number>, arr: U ) => void; | ||
|
|
||
| /** | ||
| * Callback invoked for each array element. | ||
| * | ||
| * @param value - current array element | ||
| * @param index - current array element index | ||
| * @param arr - input array | ||
| */ | ||
| type Callback<T, U, V> = Nullary<V> | Unary<T, V> | Binary<T, V> | Ternary<T, U, V>; | ||
|
|
||
| /** | ||
| * Invokes a callback function once for each array element. | ||
| * | ||
| * @param x - input array | ||
| * @param fcn - callback function | ||
| * @param thisArg - callback function execution context | ||
| * | ||
| * @example | ||
| * var naryFunction = require( '@stdlib/utils/nary-function' ); | ||
| * var log = require( '@stdlib/console/log' ); | ||
| * | ||
| * var x = [ 1, 2, 3, 4]; | ||
| * | ||
| * // Apply the callback function: | ||
| * forEach( x, naryFunction( log, 1 ) ); | ||
| */ | ||
| declare function forEach<T = unknown, U = unknown, V = unknown>( x: Collection<T> | AccessorArrayLike<U>, fcn: Callback<number, Collection<T>, V>, thisArg?: ThisParameterType<Callback<number, Collection<T>, V>> ): void; | ||
|
|
||
|
|
||
| // EXPORTS // | ||
|
|
||
| export = forEach; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.