first
Returns the first element of the array, or the first element matching a predicate. Throws NoSuchElementException if the array is empty or no match is found; use firstOrNull for a safe variant that returns null instead.
Signatures
fun <T, D : Dimension> MultiArray<T, D>.first(): T
inline fun <T, D : Dimension> MultiArray<T, D>.first(
predicate: (T) -> Boolean
): T
fun <T, D : Dimension> MultiArray<T, D>.firstOrNull(): T?
inline fun <T, D : Dimension> MultiArray<T, D>.firstOrNull(
predicate: (T) -> Boolean
): T?
Parameters
Parameter | Type | Description |
|---|---|---|
|
| Condition to match the first element against. |
Returns:
first(): the first element. ThrowsNoSuchElementExceptionif the array is empty.first(predicate): the first matching element. ThrowsNoSuchElementExceptionif none match.firstOrNull()/firstOrNull(predicate): same as above but returnsnullinstead of throwing.
Example
val a = mk.ndarray(mk[10, 20, 30])
a.first() // 10
a.first { it > 15 } // 20
a.firstOrNull { it > 50 } // null
28 February 2026