View source code
Display the source code in dmd/root/array.d from which this
page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a
Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page.
Requires a signed-in GitHub account. This works well for small changes.
If you'd like to make larger changes you may want to consider using
local clone.
dmd.root.array.each
- multiple declarations
Function each
Iterates the given array and calls the given callable for each element.
Use this instead of foreach
when the array may expand during iteration.
Parameters
Name | Description |
---|---|
callable | the callable to call for each element |
array | the array to iterate |
See Also
Example
static immutable expected = [2, 3, 4, 5];
Array!int array;
foreach (e ; expected)
array .push(e);
int[] result;
array .each!((e) {
result ~= e;
});
writeln(result); // expected
Function each
Iterates the given array and calls the given callable for each element.
If callable
returns != 0
, it will stop the iteration and return that
value, otherwise it will return 0.
Use this instead of foreach
when the array may expand during iteration.
Parameters
Name | Description |
---|---|
callable | the callable to call for each element |
array | the array to iterate |
Returns
the last value returned by callable
See Also
Example
Array!int array;
foreach (e ; [2, 3, 4, 5])
array .push(e);
int[] result;
const returnValue = array .each!((e) {
result ~= e;
if (e == 3)
return 8;
return 0;
});
writeln(result); // [2, 3]
writeln(returnValue); // 8
Authors
License
Copyright © 1999-2024 by the D Language Foundation | Page generated by ddox.