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.

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

Members

Functions

each
int each(Array!T array)
Undocumented in source. Be warned that the author may not have intended to support it.
each
int each(Array!T* array)
Undocumented in source. Be warned that the author may not have intended to support it.

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

Meta