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.

void each(alias callable, T) (
  ref Array!T array
)
if (is(ReturnType!(typeof((T t) => callable(t))) == void));

void each(alias callable, T) (
  Array!T* array
)
if (is(ReturnType!(typeof((T t) => callable(t))) == void));

Use this instead of foreach when the array may expand during iteration.

Parameters

NameDescription
callable the callable to call for each element
array the array to iterate

See Also

foreachDsymbol

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.

int each(alias callable, T) (
  ref Array!T array
)
if (is(ReturnType!(typeof((T t) => callable(t))) == int));

int each(alias callable, T) (
  Array!T* array
)
if (is(ReturnType!(typeof((T t) => callable(t))) == int));

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

NameDescription
callable the callable to call for each element
array the array to iterate

Returns

the last value returned by callable

See Also

foreachDsymbol

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

Walter Bright

License

Boost License 1.0