each

The call each!(fun)(slice1, ..., sliceN) evaluates fun for each set of elements x1, ..., xN in slice1, ..., sliceN respectively.

each allows to iterate multiple slices in the lockstep.

  1. auto each(Slices slices)
    template each(alias fun)
    @optmath
    static if(__traits(isSame, naryFun!fun, fun))
    @optmath
    each
    (
    Slices...
    )
    (
    scope Slices slices
    )
    if (
    Slices.length &&
    !is(Slices[0] : Chequer)
    )
  2. auto each(Chequer color, Slices slices)
  3. alias each = .each!(naryFun!fun)

Members

Aliases

each
alias each = .each!(naryFun!fun)
Undocumented in source.

Functions

each
auto each(Chequer color, Slices slices)

Iterates elements of selected Chequer color.

each
auto each(Slices slices)

Parameters

fun

A function. Note: $(NDSLICEREF dynamic, transposed) and $(NDSLICEREF topology, pack) can be used to specify dimensions.

Examples

Ranges and arrays

auto ar = [1, 2, 3];
ar.each!"a *= 2";
assert (ar == [2, 4, 6]);

Single slice, multiply-add

import mir.ndslice.allocation : slice;
import mir.ndslice.topology : as, iota;

//| 0 1 2 |
//| 3 4 5 |
auto sl = iota(2, 3).as!double.slice;

sl.each!((ref a) { a = a * 10 + 5; });

assert(sl ==
    [[ 5, 15, 25],
     [35, 45, 55]]);

Swap two slices

import mir.utility : swap;
import mir.ndslice.allocation : slice;
import mir.ndslice.topology : as, iota;

//| 0 1 2 |
//| 3 4 5 |
auto a = iota([2, 3], 0).as!double.slice;
//| 10 11 12 |
//| 13 14 15 |
auto b = iota([2, 3], 10).as!double.slice;

each!swap(a, b);

assert(a == iota([2, 3], 10));
assert(b == iota([2, 3], 0));

Swap two zipped slices

import mir.utility : swap;
import mir.ndslice.allocation : slice;
import mir.ndslice.topology : as, zip, iota;

//| 0 1 2 |
//| 3 4 5 |
auto a = iota([2, 3], 0).as!double.slice;
//| 10 11 12 |
//| 13 14 15 |
auto b = iota([2, 3], 10).as!double.slice;

auto z = zip(a, b);

z.each!(z => swap(z.a, z.b));

assert(a == iota([2, 3], 10));
assert(b == iota([2, 3], 0));

See Also

This is functionally similar to reduce but has not seed.

Meta