View source code
Display the source code in std/algorithm/iteration.d from which this page was generated on
github.
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.
Page wiki
View or edit the community-maintained wiki page associated with this page.
Template std.algorithm.iteration.each
Eagerly iterates over r and calls pred over each element.
Arguments
template each(alias pred);
Functions
| Function name | Description |
|---|---|
| each |
Parameters
| Name | Description |
|---|---|
| pred | predicate to apply to each element of the range |
| r | range or iterable over which each iterates |
Example
void deleteOldBackups()
{
import std.algorithm, std.datetime, std.file;
auto cutoff = Clock.currTime() - 7.days;
dirEntries("", "*~", SpanMode.depth)
.filter!(de => de.timeLastModified < cutoff)
.each!remove();
}
If the range supports it, the value can be mutated in place. Examples:
arr.each!((ref a) => a++); arr.each!"a++";
If no predicate is specified, will default to doing nothing
but consuming the entire range. each.front will be evaluated, but this
can be avoided by explicitly specifying a predicate lambda with a
lazy parameter.
also supports eachopApply-based iterators, so it will work
with e.g. std.parallelism.parallel.
See Also
std.range.tee