Iterates elements of selected Chequer color.
A function. Note: $(NDSLICEREF dynamic, transposed) and $(NDSLICEREF topology, pack) can be used to specify dimensions.
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));
This is functionally similar to reduce but has not seed.
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.