forEach
Performs the given action on each element of the array. Indexed variants provide the element's flat index (forEachIndexed, 1D only) or multi-dimensional index (forEachMultiIndexed).
Signatures
inline fun <T, D : Dimension> MultiArray<T, D>.forEach(
action: (T) -> Unit
)
inline fun <T> MultiArray<T, D1>.forEachIndexed(
action: (index: Int, T) -> Unit
)
inline fun <T, D : Dimension> MultiArray<T, D>.forEachMultiIndexed(
action: (index: IntArray, T) -> Unit
)
Parameters
Parameter | Type | Description |
|---|---|---|
|
| Action to perform on each element. |
|
| Action with flat index (1D arrays only). |
|
| Action with multi-dimensional index. |
Example
val a = mk.ndarray(mk[mk[1, 2], mk[3, 4]])
a.forEach { print("$it ") }
// 1 2 3 4
a.forEachMultiIndexed { idx, v ->
println("${idx.toList()} -> $v")
}
// [0, 0] -> 1
// [0, 1] -> 2
// [1, 0] -> 3
// [1, 1] -> 4
28 February 2026