Multik 0.3.0 Help

flatMap

Transforms each element into an Iterable of results and flattens all results into a single 1D array. Useful when each element maps to a variable number of output values.

Signatures

inline fun <T, D : Dimension, reified R> MultiArray<T, D>.flatMap( transform: (T) -> Iterable<R> ): D1Array<R> inline fun <T, reified R> MultiArray<T, D1>.flatMapIndexed( transform: (index: Int, T) -> Iterable<R> ): D1Array<R> inline fun <T, reified R> MultiArray<T, D1>.flatMapMultiIndexed( transform: (index: IntArray, T) -> Iterable<R> ): D1Array<R>

Parameters

Parameter

Type

Description

transform

(T) -> Iterable<R>

Maps each element to an iterable of results.

transform

(index: Int, T) -> Iterable<R>

With flat index (1D arrays only).

transform

(index: IntArray, T) -> Iterable<R>

With multi-dimensional index.

Returns: D1Array<R> — all results flattened into a single 1D array.

Example

val a = mk.ndarray(mk[1, 2, 3]) val result = a.flatMap { listOf(it, it * 10) } // [1, 10, 2, 20, 3, 30]
28 February 2026