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.

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

Parameters

callable

the callable to call for each element

array

the array to iterate

Return Value

the last value returned by callable

Examples

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;
});

assert(result == [2, 3]);
assert(returnValue == 8);

See Also

Suggestion Box / Bug Report