package kotlin // // NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt // See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib // import kotlin.platform.* import java.util.* import java.util.Collections // TODO: it's temporary while we have java.util.Collections in js /** * Returns 1st *element* from the collection. */ public inline fun Array.component1(): T { return get(0) } /** * Returns 1st *element* from the collection. */ public inline fun BooleanArray.component1(): Boolean { return get(0) } /** * Returns 1st *element* from the collection. */ public inline fun ByteArray.component1(): Byte { return get(0) } /** * Returns 1st *element* from the collection. */ public inline fun CharArray.component1(): Char { return get(0) } /** * Returns 1st *element* from the collection. */ public inline fun DoubleArray.component1(): Double { return get(0) } /** * Returns 1st *element* from the collection. */ public inline fun FloatArray.component1(): Float { return get(0) } /** * Returns 1st *element* from the collection. */ public inline fun IntArray.component1(): Int { return get(0) } /** * Returns 1st *element* from the collection. */ public inline fun LongArray.component1(): Long { return get(0) } /** * Returns 1st *element* from the collection. */ public inline fun ShortArray.component1(): Short { return get(0) } /** * Returns 1st *element* from the collection. */ public inline fun List.component1(): T { return get(0) } /** * Returns 2nd *element* from the collection. */ public inline fun Array.component2(): T { return get(1) } /** * Returns 2nd *element* from the collection. */ public inline fun BooleanArray.component2(): Boolean { return get(1) } /** * Returns 2nd *element* from the collection. */ public inline fun ByteArray.component2(): Byte { return get(1) } /** * Returns 2nd *element* from the collection. */ public inline fun CharArray.component2(): Char { return get(1) } /** * Returns 2nd *element* from the collection. */ public inline fun DoubleArray.component2(): Double { return get(1) } /** * Returns 2nd *element* from the collection. */ public inline fun FloatArray.component2(): Float { return get(1) } /** * Returns 2nd *element* from the collection. */ public inline fun IntArray.component2(): Int { return get(1) } /** * Returns 2nd *element* from the collection. */ public inline fun LongArray.component2(): Long { return get(1) } /** * Returns 2nd *element* from the collection. */ public inline fun ShortArray.component2(): Short { return get(1) } /** * Returns 2nd *element* from the collection. */ public inline fun List.component2(): T { return get(1) } /** * Returns 3rd *element* from the collection. */ public inline fun Array.component3(): T { return get(2) } /** * Returns 3rd *element* from the collection. */ public inline fun BooleanArray.component3(): Boolean { return get(2) } /** * Returns 3rd *element* from the collection. */ public inline fun ByteArray.component3(): Byte { return get(2) } /** * Returns 3rd *element* from the collection. */ public inline fun CharArray.component3(): Char { return get(2) } /** * Returns 3rd *element* from the collection. */ public inline fun DoubleArray.component3(): Double { return get(2) } /** * Returns 3rd *element* from the collection. */ public inline fun FloatArray.component3(): Float { return get(2) } /** * Returns 3rd *element* from the collection. */ public inline fun IntArray.component3(): Int { return get(2) } /** * Returns 3rd *element* from the collection. */ public inline fun LongArray.component3(): Long { return get(2) } /** * Returns 3rd *element* from the collection. */ public inline fun ShortArray.component3(): Short { return get(2) } /** * Returns 3rd *element* from the collection. */ public inline fun List.component3(): T { return get(2) } /** * Returns 4th *element* from the collection. */ public inline fun Array.component4(): T { return get(3) } /** * Returns 4th *element* from the collection. */ public inline fun BooleanArray.component4(): Boolean { return get(3) } /** * Returns 4th *element* from the collection. */ public inline fun ByteArray.component4(): Byte { return get(3) } /** * Returns 4th *element* from the collection. */ public inline fun CharArray.component4(): Char { return get(3) } /** * Returns 4th *element* from the collection. */ public inline fun DoubleArray.component4(): Double { return get(3) } /** * Returns 4th *element* from the collection. */ public inline fun FloatArray.component4(): Float { return get(3) } /** * Returns 4th *element* from the collection. */ public inline fun IntArray.component4(): Int { return get(3) } /** * Returns 4th *element* from the collection. */ public inline fun LongArray.component4(): Long { return get(3) } /** * Returns 4th *element* from the collection. */ public inline fun ShortArray.component4(): Short { return get(3) } /** * Returns 4th *element* from the collection. */ public inline fun List.component4(): T { return get(3) } /** * Returns 5th *element* from the collection. */ public inline fun Array.component5(): T { return get(4) } /** * Returns 5th *element* from the collection. */ public inline fun BooleanArray.component5(): Boolean { return get(4) } /** * Returns 5th *element* from the collection. */ public inline fun ByteArray.component5(): Byte { return get(4) } /** * Returns 5th *element* from the collection. */ public inline fun CharArray.component5(): Char { return get(4) } /** * Returns 5th *element* from the collection. */ public inline fun DoubleArray.component5(): Double { return get(4) } /** * Returns 5th *element* from the collection. */ public inline fun FloatArray.component5(): Float { return get(4) } /** * Returns 5th *element* from the collection. */ public inline fun IntArray.component5(): Int { return get(4) } /** * Returns 5th *element* from the collection. */ public inline fun LongArray.component5(): Long { return get(4) } /** * Returns 5th *element* from the collection. */ public inline fun ShortArray.component5(): Short { return get(4) } /** * Returns 5th *element* from the collection. */ public inline fun List.component5(): T { return get(4) } /** * Returns true if *element* is found in the collection */ public fun Array.contains(element: T): Boolean { return indexOf(element) >= 0 } /** * Returns true if *element* is found in the collection */ public fun BooleanArray.contains(element: Boolean): Boolean { return indexOf(element) >= 0 } /** * Returns true if *element* is found in the collection */ public fun ByteArray.contains(element: Byte): Boolean { return indexOf(element) >= 0 } /** * Returns true if *element* is found in the collection */ public fun CharArray.contains(element: Char): Boolean { return indexOf(element) >= 0 } /** * Returns true if *element* is found in the collection */ public fun DoubleArray.contains(element: Double): Boolean { return indexOf(element) >= 0 } /** * Returns true if *element* is found in the collection */ public fun FloatArray.contains(element: Float): Boolean { return indexOf(element) >= 0 } /** * Returns true if *element* is found in the collection */ public fun IntArray.contains(element: Int): Boolean { return indexOf(element) >= 0 } /** * Returns true if *element* is found in the collection */ public fun LongArray.contains(element: Long): Boolean { return indexOf(element) >= 0 } /** * Returns true if *element* is found in the collection */ public fun ShortArray.contains(element: Short): Boolean { return indexOf(element) >= 0 } /** * Returns true if *element* is found in the collection */ public fun Iterable.contains(element: T): Boolean { if (this is Collection<*>) return contains(element) return indexOf(element) >= 0 } /** * Returns true if *element* is found in the collection */ public fun Sequence.contains(element: T): Boolean { if (this is Collection<*>) return contains(element) return indexOf(element) >= 0 } deprecated("Migrate to using Sequence and respective functions") /** * Returns true if *element* is found in the collection */ public fun Stream.contains(element: T): Boolean { if (this is Collection<*>) return contains(element) return indexOf(element) >= 0 } /** * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this collection. */ public fun Array.elementAt(index: Int): T { return get(index) } /** * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this collection. */ public fun BooleanArray.elementAt(index: Int): Boolean { return get(index) } /** * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this collection. */ public fun ByteArray.elementAt(index: Int): Byte { return get(index) } /** * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this collection. */ public fun CharArray.elementAt(index: Int): Char { return get(index) } /** * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this collection. */ public fun DoubleArray.elementAt(index: Int): Double { return get(index) } /** * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this collection. */ public fun FloatArray.elementAt(index: Int): Float { return get(index) } /** * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this collection. */ public fun IntArray.elementAt(index: Int): Int { return get(index) } /** * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this collection. */ public fun LongArray.elementAt(index: Int): Long { return get(index) } /** * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this collection. */ public fun ShortArray.elementAt(index: Int): Short { return get(index) } /** * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this collection. */ public fun Iterable.elementAt(index: Int): T { if (this is List) return get(index) return elementAtOrElse(index) { throw IndexOutOfBoundsException("Collection doesn't contain element at index $index") } } /** * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this collection. */ public fun List.elementAt(index: Int): T { return get(index) } /** * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this collection. */ public fun Sequence.elementAt(index: Int): T { return elementAtOrElse(index) { throw IndexOutOfBoundsException("Sequence doesn't contain element at index $index") } } deprecated("Migrate to using Sequence and respective functions") /** * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this collection. */ public fun Stream.elementAt(index: Int): T { return elementAtOrElse(index) { throw IndexOutOfBoundsException("Stream doesn't contain element at index $index") } } /** * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this collection. */ public fun String.elementAt(index: Int): Char { return get(index) } /** * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this collection. */ public inline fun Array.elementAtOrElse(index: Int, defaultValue: (Int) -> T): T { return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) } /** * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this collection. */ public inline fun BooleanArray.elementAtOrElse(index: Int, defaultValue: (Int) -> Boolean): Boolean { return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) } /** * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this collection. */ public inline fun ByteArray.elementAtOrElse(index: Int, defaultValue: (Int) -> Byte): Byte { return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) } /** * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this collection. */ public inline fun CharArray.elementAtOrElse(index: Int, defaultValue: (Int) -> Char): Char { return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) } /** * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this collection. */ public inline fun DoubleArray.elementAtOrElse(index: Int, defaultValue: (Int) -> Double): Double { return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) } /** * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this collection. */ public inline fun FloatArray.elementAtOrElse(index: Int, defaultValue: (Int) -> Float): Float { return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) } /** * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this collection. */ public inline fun IntArray.elementAtOrElse(index: Int, defaultValue: (Int) -> Int): Int { return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) } /** * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this collection. */ public inline fun LongArray.elementAtOrElse(index: Int, defaultValue: (Int) -> Long): Long { return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) } /** * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this collection. */ public inline fun ShortArray.elementAtOrElse(index: Int, defaultValue: (Int) -> Short): Short { return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) } /** * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this collection. */ public inline fun Iterable.elementAtOrElse(index: Int, defaultValue: (Int) -> T): T { if (this is List) return this.elementAtOrElse(index, defaultValue) if (index < 0) return defaultValue(index) val iterator = iterator() var count = 0 while (iterator.hasNext()) { val element = iterator.next() if (index == count++) return element } return defaultValue(index) } /** * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this collection. */ public inline fun List.elementAtOrElse(index: Int, defaultValue: (Int) -> T): T { return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) } /** * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this collection. */ public inline fun Sequence.elementAtOrElse(index: Int, defaultValue: (Int) -> T): T { if (index < 0) return defaultValue(index) val iterator = iterator() var count = 0 while (iterator.hasNext()) { val element = iterator.next() if (index == count++) return element } return defaultValue(index) } deprecated("Migrate to using Sequence and respective functions") /** * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this collection. */ public inline fun Stream.elementAtOrElse(index: Int, defaultValue: (Int) -> T): T { if (index < 0) return defaultValue(index) val iterator = iterator() var count = 0 while (iterator.hasNext()) { val element = iterator.next() if (index == count++) return element } return defaultValue(index) } /** * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this collection. */ public inline fun String.elementAtOrElse(index: Int, defaultValue: (Int) -> Char): Char { return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) } /** * Returns an element at the given [index] or `null` if the [index] is out of bounds of this collection. */ public fun Array.elementAtOrNull(index: Int): T? { return if (index >= 0 && index <= lastIndex) get(index) else null } /** * Returns an element at the given [index] or `null` if the [index] is out of bounds of this collection. */ public fun BooleanArray.elementAtOrNull(index: Int): Boolean? { return if (index >= 0 && index <= lastIndex) get(index) else null } /** * Returns an element at the given [index] or `null` if the [index] is out of bounds of this collection. */ public fun ByteArray.elementAtOrNull(index: Int): Byte? { return if (index >= 0 && index <= lastIndex) get(index) else null } /** * Returns an element at the given [index] or `null` if the [index] is out of bounds of this collection. */ public fun CharArray.elementAtOrNull(index: Int): Char? { return if (index >= 0 && index <= lastIndex) get(index) else null } /** * Returns an element at the given [index] or `null` if the [index] is out of bounds of this collection. */ public fun DoubleArray.elementAtOrNull(index: Int): Double? { return if (index >= 0 && index <= lastIndex) get(index) else null } /** * Returns an element at the given [index] or `null` if the [index] is out of bounds of this collection. */ public fun FloatArray.elementAtOrNull(index: Int): Float? { return if (index >= 0 && index <= lastIndex) get(index) else null } /** * Returns an element at the given [index] or `null` if the [index] is out of bounds of this collection. */ public fun IntArray.elementAtOrNull(index: Int): Int? { return if (index >= 0 && index <= lastIndex) get(index) else null } /** * Returns an element at the given [index] or `null` if the [index] is out of bounds of this collection. */ public fun LongArray.elementAtOrNull(index: Int): Long? { return if (index >= 0 && index <= lastIndex) get(index) else null } /** * Returns an element at the given [index] or `null` if the [index] is out of bounds of this collection. */ public fun ShortArray.elementAtOrNull(index: Int): Short? { return if (index >= 0 && index <= lastIndex) get(index) else null } /** * Returns an element at the given [index] or `null` if the [index] is out of bounds of this collection. */ public fun Iterable.elementAtOrNull(index: Int): T? { if (this is List) return this.elementAtOrNull(index) if (index < 0) return null val iterator = iterator() var count = 0 while (iterator.hasNext()) { val element = iterator.next() if (index == count++) return element } return null } /** * Returns an element at the given [index] or `null` if the [index] is out of bounds of this collection. */ public fun List.elementAtOrNull(index: Int): T? { return if (index >= 0 && index <= lastIndex) get(index) else null } /** * Returns an element at the given [index] or `null` if the [index] is out of bounds of this collection. */ public fun Sequence.elementAtOrNull(index: Int): T? { if (index < 0) return null val iterator = iterator() var count = 0 while (iterator.hasNext()) { val element = iterator.next() if (index == count++) return element } return null } deprecated("Migrate to using Sequence and respective functions") /** * Returns an element at the given [index] or `null` if the [index] is out of bounds of this collection. */ public fun Stream.elementAtOrNull(index: Int): T? { if (index < 0) return null val iterator = iterator() var count = 0 while (iterator.hasNext()) { val element = iterator.next() if (index == count++) return element } return null } /** * Returns an element at the given [index] or `null` if the [index] is out of bounds of this collection. */ public fun String.elementAtOrNull(index: Int): Char? { return if (index >= 0 && index <= lastIndex) get(index) else null } /** * Returns first element. * @throws NoSuchElementException if the collection is empty. */ public fun Array.first(): T { if (isEmpty()) throw NoSuchElementException("Collection is empty") return this[0] } /** * Returns first element. * @throws NoSuchElementException if the collection is empty. */ public fun BooleanArray.first(): Boolean { if (isEmpty()) throw NoSuchElementException("Collection is empty") return this[0] } /** * Returns first element. * @throws NoSuchElementException if the collection is empty. */ public fun ByteArray.first(): Byte { if (isEmpty()) throw NoSuchElementException("Collection is empty") return this[0] } /** * Returns first element. * @throws NoSuchElementException if the collection is empty. */ public fun CharArray.first(): Char { if (isEmpty()) throw NoSuchElementException("Collection is empty") return this[0] } /** * Returns first element. * @throws NoSuchElementException if the collection is empty. */ public fun DoubleArray.first(): Double { if (isEmpty()) throw NoSuchElementException("Collection is empty") return this[0] } /** * Returns first element. * @throws NoSuchElementException if the collection is empty. */ public fun FloatArray.first(): Float { if (isEmpty()) throw NoSuchElementException("Collection is empty") return this[0] } /** * Returns first element. * @throws NoSuchElementException if the collection is empty. */ public fun IntArray.first(): Int { if (isEmpty()) throw NoSuchElementException("Collection is empty") return this[0] } /** * Returns first element. * @throws NoSuchElementException if the collection is empty. */ public fun LongArray.first(): Long { if (isEmpty()) throw NoSuchElementException("Collection is empty") return this[0] } /** * Returns first element. * @throws NoSuchElementException if the collection is empty. */ public fun ShortArray.first(): Short { if (isEmpty()) throw NoSuchElementException("Collection is empty") return this[0] } /** * Returns first element. * @throws NoSuchElementException if the collection is empty. */ public fun Iterable.first(): T { when (this) { is List<*> -> { if (isEmpty()) throw NoSuchElementException("Collection is empty") else return this[0] as T } else -> { val iterator = iterator() if (!iterator.hasNext()) throw NoSuchElementException("Collection is empty") return iterator.next() } } } /** * Returns first element. * @throws NoSuchElementException if the collection is empty. */ public fun List.first(): T { if (isEmpty()) throw NoSuchElementException("Collection is empty") return this[0] } /** * Returns first element. * @throws NoSuchElementException if the collection is empty. */ public fun Sequence.first(): T { when (this) { is List<*> -> { if (isEmpty()) throw NoSuchElementException("Collection is empty") else return this[0] as T } else -> { val iterator = iterator() if (!iterator.hasNext()) throw NoSuchElementException("Collection is empty") return iterator.next() } } } deprecated("Migrate to using Sequence and respective functions") /** * Returns first element. * @throws NoSuchElementException if the collection is empty. */ public fun Stream.first(): T { when (this) { is List<*> -> { if (isEmpty()) throw NoSuchElementException("Collection is empty") else return this[0] as T } else -> { val iterator = iterator() if (!iterator.hasNext()) throw NoSuchElementException("Collection is empty") return iterator.next() } } } /** * Returns first character. * @throws NoSuchElementException if the string is empty. */ public fun String.first(): Char { if (isEmpty()) throw NoSuchElementException("Collection is empty") return this[0] } /** * Returns the first element matching the given [predicate]. * @throws NoSuchElementException if no such element is found. */ public inline fun Array.first(predicate: (T) -> Boolean): T { for (element in this) if (predicate(element)) return element throw NoSuchElementException("No element matching predicate was found") } /** * Returns the first element matching the given [predicate]. * @throws NoSuchElementException if no such element is found. */ public inline fun BooleanArray.first(predicate: (Boolean) -> Boolean): Boolean { for (element in this) if (predicate(element)) return element throw NoSuchElementException("No element matching predicate was found") } /** * Returns the first element matching the given [predicate]. * @throws NoSuchElementException if no such element is found. */ public inline fun ByteArray.first(predicate: (Byte) -> Boolean): Byte { for (element in this) if (predicate(element)) return element throw NoSuchElementException("No element matching predicate was found") } /** * Returns the first element matching the given [predicate]. * @throws NoSuchElementException if no such element is found. */ public inline fun CharArray.first(predicate: (Char) -> Boolean): Char { for (element in this) if (predicate(element)) return element throw NoSuchElementException("No element matching predicate was found") } /** * Returns the first element matching the given [predicate]. * @throws NoSuchElementException if no such element is found. */ public inline fun DoubleArray.first(predicate: (Double) -> Boolean): Double { for (element in this) if (predicate(element)) return element throw NoSuchElementException("No element matching predicate was found") } /** * Returns the first element matching the given [predicate]. * @throws NoSuchElementException if no such element is found. */ public inline fun FloatArray.first(predicate: (Float) -> Boolean): Float { for (element in this) if (predicate(element)) return element throw NoSuchElementException("No element matching predicate was found") } /** * Returns the first element matching the given [predicate]. * @throws NoSuchElementException if no such element is found. */ public inline fun IntArray.first(predicate: (Int) -> Boolean): Int { for (element in this) if (predicate(element)) return element throw NoSuchElementException("No element matching predicate was found") } /** * Returns the first element matching the given [predicate]. * @throws NoSuchElementException if no such element is found. */ public inline fun LongArray.first(predicate: (Long) -> Boolean): Long { for (element in this) if (predicate(element)) return element throw NoSuchElementException("No element matching predicate was found") } /** * Returns the first element matching the given [predicate]. * @throws NoSuchElementException if no such element is found. */ public inline fun ShortArray.first(predicate: (Short) -> Boolean): Short { for (element in this) if (predicate(element)) return element throw NoSuchElementException("No element matching predicate was found") } /** * Returns the first element matching the given [predicate]. * @throws NoSuchElementException if no such element is found. */ public inline fun Iterable.first(predicate: (T) -> Boolean): T { for (element in this) if (predicate(element)) return element throw NoSuchElementException("No element matching predicate was found") } /** * Returns the first element matching the given [predicate]. * @throws NoSuchElementException if no such element is found. */ public inline fun Sequence.first(predicate: (T) -> Boolean): T { for (element in this) if (predicate(element)) return element throw NoSuchElementException("No element matching predicate was found") } deprecated("Migrate to using Sequence and respective functions") /** * Returns the first element matching the given [predicate]. * @throws NoSuchElementException if no such element is found. */ public inline fun Stream.first(predicate: (T) -> Boolean): T { for (element in this) if (predicate(element)) return element throw NoSuchElementException("No element matching predicate was found") } /** * Returns the first character matching the given [predicate]. * @throws NoSuchElementException if no such character is found. */ public inline fun String.first(predicate: (Char) -> Boolean): Char { for (element in this) if (predicate(element)) return element throw NoSuchElementException("No element matching predicate was found") } /** * Returns the first element, or null if the collection is empty. */ public fun Array.firstOrNull(): T? { return if (isEmpty()) null else this[0] } /** * Returns the first element, or null if the collection is empty. */ public fun BooleanArray.firstOrNull(): Boolean? { return if (isEmpty()) null else this[0] } /** * Returns the first element, or null if the collection is empty. */ public fun ByteArray.firstOrNull(): Byte? { return if (isEmpty()) null else this[0] } /** * Returns the first element, or null if the collection is empty. */ public fun CharArray.firstOrNull(): Char? { return if (isEmpty()) null else this[0] } /** * Returns the first element, or null if the collection is empty. */ public fun DoubleArray.firstOrNull(): Double? { return if (isEmpty()) null else this[0] } /** * Returns the first element, or null if the collection is empty. */ public fun FloatArray.firstOrNull(): Float? { return if (isEmpty()) null else this[0] } /** * Returns the first element, or null if the collection is empty. */ public fun IntArray.firstOrNull(): Int? { return if (isEmpty()) null else this[0] } /** * Returns the first element, or null if the collection is empty. */ public fun LongArray.firstOrNull(): Long? { return if (isEmpty()) null else this[0] } /** * Returns the first element, or null if the collection is empty. */ public fun ShortArray.firstOrNull(): Short? { return if (isEmpty()) null else this[0] } /** * Returns the first element, or null if the collection is empty. */ public fun Iterable.firstOrNull(): T? { when (this) { is List<*> -> { if (isEmpty()) return null else return this[0] as T } else -> { val iterator = iterator() if (!iterator.hasNext()) return null return iterator.next() } } } /** * Returns the first element, or null if the collection is empty. */ public fun List.firstOrNull(): T? { return if (isEmpty()) null else this[0] } /** * Returns the first element, or null if the collection is empty. */ public fun Sequence.firstOrNull(): T? { when (this) { is List<*> -> { if (isEmpty()) return null else return this[0] as T } else -> { val iterator = iterator() if (!iterator.hasNext()) return null return iterator.next() } } } deprecated("Migrate to using Sequence and respective functions") /** * Returns the first element, or null if the collection is empty. */ public fun Stream.firstOrNull(): T? { when (this) { is List<*> -> { if (isEmpty()) return null else return this[0] as T } else -> { val iterator = iterator() if (!iterator.hasNext()) return null return iterator.next() } } } /** * Returns the first character, or null if string is empty. */ public fun String.firstOrNull(): Char? { return if (isEmpty()) null else this[0] } /** * Returns the first element matching the given [predicate], or `null` if element was not found */ public inline fun Array.firstOrNull(predicate: (T) -> Boolean): T? { for (element in this) if (predicate(element)) return element return null } /** * Returns the first element matching the given [predicate], or `null` if element was not found */ public inline fun BooleanArray.firstOrNull(predicate: (Boolean) -> Boolean): Boolean? { for (element in this) if (predicate(element)) return element return null } /** * Returns the first element matching the given [predicate], or `null` if element was not found */ public inline fun ByteArray.firstOrNull(predicate: (Byte) -> Boolean): Byte? { for (element in this) if (predicate(element)) return element return null } /** * Returns the first element matching the given [predicate], or `null` if element was not found */ public inline fun CharArray.firstOrNull(predicate: (Char) -> Boolean): Char? { for (element in this) if (predicate(element)) return element return null } /** * Returns the first element matching the given [predicate], or `null` if element was not found */ public inline fun DoubleArray.firstOrNull(predicate: (Double) -> Boolean): Double? { for (element in this) if (predicate(element)) return element return null } /** * Returns the first element matching the given [predicate], or `null` if element was not found */ public inline fun FloatArray.firstOrNull(predicate: (Float) -> Boolean): Float? { for (element in this) if (predicate(element)) return element return null } /** * Returns the first element matching the given [predicate], or `null` if element was not found */ public inline fun IntArray.firstOrNull(predicate: (Int) -> Boolean): Int? { for (element in this) if (predicate(element)) return element return null } /** * Returns the first element matching the given [predicate], or `null` if element was not found */ public inline fun LongArray.firstOrNull(predicate: (Long) -> Boolean): Long? { for (element in this) if (predicate(element)) return element return null } /** * Returns the first element matching the given [predicate], or `null` if element was not found */ public inline fun ShortArray.firstOrNull(predicate: (Short) -> Boolean): Short? { for (element in this) if (predicate(element)) return element return null } /** * Returns the first element matching the given [predicate], or `null` if element was not found */ public inline fun Iterable.firstOrNull(predicate: (T) -> Boolean): T? { for (element in this) if (predicate(element)) return element return null } /** * Returns the first element matching the given [predicate], or `null` if element was not found */ public inline fun Sequence.firstOrNull(predicate: (T) -> Boolean): T? { for (element in this) if (predicate(element)) return element return null } deprecated("Migrate to using Sequence and respective functions") /** * Returns the first element matching the given [predicate], or `null` if element was not found */ public inline fun Stream.firstOrNull(predicate: (T) -> Boolean): T? { for (element in this) if (predicate(element)) return element return null } /** * Returns the first character matching the given [predicate], or `null` if character was not found */ public inline fun String.firstOrNull(predicate: (Char) -> Boolean): Char? { for (element in this) if (predicate(element)) return element return null } /** * Returns first index of [element], or -1 if the collection does not contain element */ public fun Array.indexOf(element: T): Int { if (element == null) { for (index in indices) { if (this[index] == null) { return index } } } else { for (index in indices) { if (element == this[index]) { return index } } } return -1 } /** * Returns first index of [element], or -1 if the collection does not contain element */ public fun BooleanArray.indexOf(element: Boolean): Int { for (index in indices) { if (element == this[index]) { return index } } return -1 } /** * Returns first index of [element], or -1 if the collection does not contain element */ public fun ByteArray.indexOf(element: Byte): Int { for (index in indices) { if (element == this[index]) { return index } } return -1 } /** * Returns first index of [element], or -1 if the collection does not contain element */ public fun CharArray.indexOf(element: Char): Int { for (index in indices) { if (element == this[index]) { return index } } return -1 } /** * Returns first index of [element], or -1 if the collection does not contain element */ public fun DoubleArray.indexOf(element: Double): Int { for (index in indices) { if (element == this[index]) { return index } } return -1 } /** * Returns first index of [element], or -1 if the collection does not contain element */ public fun FloatArray.indexOf(element: Float): Int { for (index in indices) { if (element == this[index]) { return index } } return -1 } /** * Returns first index of [element], or -1 if the collection does not contain element */ public fun IntArray.indexOf(element: Int): Int { for (index in indices) { if (element == this[index]) { return index } } return -1 } /** * Returns first index of [element], or -1 if the collection does not contain element */ public fun LongArray.indexOf(element: Long): Int { for (index in indices) { if (element == this[index]) { return index } } return -1 } /** * Returns first index of [element], or -1 if the collection does not contain element */ public fun ShortArray.indexOf(element: Short): Int { for (index in indices) { if (element == this[index]) { return index } } return -1 } /** * Returns first index of [element], or -1 if the collection does not contain element */ public fun Iterable.indexOf(element: T): Int { var index = 0 for (item in this) { if (element == item) return index index++ } return -1 } /** * Returns first index of [element], or -1 if the collection does not contain element */ public fun Sequence.indexOf(element: T): Int { var index = 0 for (item in this) { if (element == item) return index index++ } return -1 } deprecated("Migrate to using Sequence and respective functions") /** * Returns first index of [element], or -1 if the collection does not contain element */ public fun Stream.indexOf(element: T): Int { var index = 0 for (item in this) { if (element == item) return index index++ } return -1 } /** * Returns index of the first element matching the given [predicate], or -1 if the collection does not contain such element */ public inline fun Array.indexOfFirst(predicate: (T) -> Boolean): Int { for (index in indices) { if (predicate(this[index])) { return index } } return -1 } /** * Returns index of the first element matching the given [predicate], or -1 if the collection does not contain such element */ public inline fun BooleanArray.indexOfFirst(predicate: (Boolean) -> Boolean): Int { for (index in indices) { if (predicate(this[index])) { return index } } return -1 } /** * Returns index of the first element matching the given [predicate], or -1 if the collection does not contain such element */ public inline fun ByteArray.indexOfFirst(predicate: (Byte) -> Boolean): Int { for (index in indices) { if (predicate(this[index])) { return index } } return -1 } /** * Returns index of the first element matching the given [predicate], or -1 if the collection does not contain such element */ public inline fun CharArray.indexOfFirst(predicate: (Char) -> Boolean): Int { for (index in indices) { if (predicate(this[index])) { return index } } return -1 } /** * Returns index of the first element matching the given [predicate], or -1 if the collection does not contain such element */ public inline fun DoubleArray.indexOfFirst(predicate: (Double) -> Boolean): Int { for (index in indices) { if (predicate(this[index])) { return index } } return -1 } /** * Returns index of the first element matching the given [predicate], or -1 if the collection does not contain such element */ public inline fun FloatArray.indexOfFirst(predicate: (Float) -> Boolean): Int { for (index in indices) { if (predicate(this[index])) { return index } } return -1 } /** * Returns index of the first element matching the given [predicate], or -1 if the collection does not contain such element */ public inline fun IntArray.indexOfFirst(predicate: (Int) -> Boolean): Int { for (index in indices) { if (predicate(this[index])) { return index } } return -1 } /** * Returns index of the first element matching the given [predicate], or -1 if the collection does not contain such element */ public inline fun LongArray.indexOfFirst(predicate: (Long) -> Boolean): Int { for (index in indices) { if (predicate(this[index])) { return index } } return -1 } /** * Returns index of the first element matching the given [predicate], or -1 if the collection does not contain such element */ public inline fun ShortArray.indexOfFirst(predicate: (Short) -> Boolean): Int { for (index in indices) { if (predicate(this[index])) { return index } } return -1 } /** * Returns index of the first element matching the given [predicate], or -1 if the collection does not contain such element */ public inline fun Iterable.indexOfFirst(predicate: (T) -> Boolean): Int { var index = 0 for (item in this) { if (predicate(item)) return index index++ } return -1 } /** * Returns index of the first element matching the given [predicate], or -1 if the collection does not contain such element */ public inline fun List.indexOfFirst(predicate: (T) -> Boolean): Int { for (index in indices) { if (predicate(this[index])) { return index } } return -1 } /** * Returns index of the first element matching the given [predicate], or -1 if the collection does not contain such element */ public inline fun Sequence.indexOfFirst(predicate: (T) -> Boolean): Int { var index = 0 for (item in this) { if (predicate(item)) return index index++ } return -1 } deprecated("Migrate to using Sequence and respective functions") /** * Returns index of the first element matching the given [predicate], or -1 if the collection does not contain such element */ public inline fun Stream.indexOfFirst(predicate: (T) -> Boolean): Int { var index = 0 for (item in this) { if (predicate(item)) return index index++ } return -1 } /** * Returns index of the first element matching the given [predicate], or -1 if the collection does not contain such element */ public inline fun String.indexOfFirst(predicate: (Char) -> Boolean): Int { for (index in indices) { if (predicate(this[index])) { return index } } return -1 } /** * Returns index of the last element matching the given [predicate], or -1 if the collection does not contain such element */ public inline fun Array.indexOfLast(predicate: (T) -> Boolean): Int { for (index in indices.reversed()) { if (predicate(this[index])) { return index } } return -1 } /** * Returns index of the last element matching the given [predicate], or -1 if the collection does not contain such element */ public inline fun BooleanArray.indexOfLast(predicate: (Boolean) -> Boolean): Int { for (index in indices.reversed()) { if (predicate(this[index])) { return index } } return -1 } /** * Returns index of the last element matching the given [predicate], or -1 if the collection does not contain such element */ public inline fun ByteArray.indexOfLast(predicate: (Byte) -> Boolean): Int { for (index in indices.reversed()) { if (predicate(this[index])) { return index } } return -1 } /** * Returns index of the last element matching the given [predicate], or -1 if the collection does not contain such element */ public inline fun CharArray.indexOfLast(predicate: (Char) -> Boolean): Int { for (index in indices.reversed()) { if (predicate(this[index])) { return index } } return -1 } /** * Returns index of the last element matching the given [predicate], or -1 if the collection does not contain such element */ public inline fun DoubleArray.indexOfLast(predicate: (Double) -> Boolean): Int { for (index in indices.reversed()) { if (predicate(this[index])) { return index } } return -1 } /** * Returns index of the last element matching the given [predicate], or -1 if the collection does not contain such element */ public inline fun FloatArray.indexOfLast(predicate: (Float) -> Boolean): Int { for (index in indices.reversed()) { if (predicate(this[index])) { return index } } return -1 } /** * Returns index of the last element matching the given [predicate], or -1 if the collection does not contain such element */ public inline fun IntArray.indexOfLast(predicate: (Int) -> Boolean): Int { for (index in indices.reversed()) { if (predicate(this[index])) { return index } } return -1 } /** * Returns index of the last element matching the given [predicate], or -1 if the collection does not contain such element */ public inline fun LongArray.indexOfLast(predicate: (Long) -> Boolean): Int { for (index in indices.reversed()) { if (predicate(this[index])) { return index } } return -1 } /** * Returns index of the last element matching the given [predicate], or -1 if the collection does not contain such element */ public inline fun ShortArray.indexOfLast(predicate: (Short) -> Boolean): Int { for (index in indices.reversed()) { if (predicate(this[index])) { return index } } return -1 } /** * Returns index of the last element matching the given [predicate], or -1 if the collection does not contain such element */ public inline fun Iterable.indexOfLast(predicate: (T) -> Boolean): Int { var lastIndex = -1 var index = 0 for (item in this) { if (predicate(item)) lastIndex = index index++ } return lastIndex } /** * Returns index of the last element matching the given [predicate], or -1 if the collection does not contain such element */ public inline fun List.indexOfLast(predicate: (T) -> Boolean): Int { for (index in indices.reversed()) { if (predicate(this[index])) { return index } } return -1 } /** * Returns index of the last element matching the given [predicate], or -1 if the collection does not contain such element */ public inline fun Sequence.indexOfLast(predicate: (T) -> Boolean): Int { var lastIndex = -1 var index = 0 for (item in this) { if (predicate(item)) lastIndex = index index++ } return lastIndex } deprecated("Migrate to using Sequence and respective functions") /** * Returns index of the last element matching the given [predicate], or -1 if the collection does not contain such element */ public inline fun Stream.indexOfLast(predicate: (T) -> Boolean): Int { var lastIndex = -1 var index = 0 for (item in this) { if (predicate(item)) lastIndex = index index++ } return lastIndex } /** * Returns index of the last element matching the given [predicate], or -1 if the collection does not contain such element */ public inline fun String.indexOfLast(predicate: (Char) -> Boolean): Int { for (index in indices.reversed()) { if (predicate(this[index])) { return index } } return -1 } /** * Returns the last element. * @throws NoSuchElementException if the collection is empty. */ public fun Array.last(): T { if (isEmpty()) throw NoSuchElementException("Collection is empty") return this[lastIndex] } /** * Returns the last element. * @throws NoSuchElementException if the collection is empty. */ public fun BooleanArray.last(): Boolean { if (isEmpty()) throw NoSuchElementException("Collection is empty") return this[lastIndex] } /** * Returns the last element. * @throws NoSuchElementException if the collection is empty. */ public fun ByteArray.last(): Byte { if (isEmpty()) throw NoSuchElementException("Collection is empty") return this[lastIndex] } /** * Returns the last element. * @throws NoSuchElementException if the collection is empty. */ public fun CharArray.last(): Char { if (isEmpty()) throw NoSuchElementException("Collection is empty") return this[lastIndex] } /** * Returns the last element. * @throws NoSuchElementException if the collection is empty. */ public fun DoubleArray.last(): Double { if (isEmpty()) throw NoSuchElementException("Collection is empty") return this[lastIndex] } /** * Returns the last element. * @throws NoSuchElementException if the collection is empty. */ public fun FloatArray.last(): Float { if (isEmpty()) throw NoSuchElementException("Collection is empty") return this[lastIndex] } /** * Returns the last element. * @throws NoSuchElementException if the collection is empty. */ public fun IntArray.last(): Int { if (isEmpty()) throw NoSuchElementException("Collection is empty") return this[lastIndex] } /** * Returns the last element. * @throws NoSuchElementException if the collection is empty. */ public fun LongArray.last(): Long { if (isEmpty()) throw NoSuchElementException("Collection is empty") return this[lastIndex] } /** * Returns the last element. * @throws NoSuchElementException if the collection is empty. */ public fun ShortArray.last(): Short { if (isEmpty()) throw NoSuchElementException("Collection is empty") return this[lastIndex] } /** * Returns the last element. * @throws NoSuchElementException if the collection is empty. */ public fun Iterable.last(): T { when (this) { is List<*> -> { if (isEmpty()) throw NoSuchElementException("Collection is empty") else return this[this.lastIndex] as T } else -> { val iterator = iterator() if (!iterator.hasNext()) throw NoSuchElementException("Collection is empty") var last = iterator.next() while (iterator.hasNext()) last = iterator.next() return last } } } /** * Returns the last element. * @throws NoSuchElementException if the collection is empty. */ public fun List.last(): T { if (isEmpty()) throw NoSuchElementException("Collection is empty") return this[lastIndex] } /** * Returns the last element. * @throws NoSuchElementException if the collection is empty. */ public fun Sequence.last(): T { val iterator = iterator() if (!iterator.hasNext()) throw NoSuchElementException("Collection is empty") var last = iterator.next() while (iterator.hasNext()) last = iterator.next() return last } deprecated("Migrate to using Sequence and respective functions") /** * Returns the last element. * @throws NoSuchElementException if the collection is empty. */ public fun Stream.last(): T { val iterator = iterator() if (!iterator.hasNext()) throw NoSuchElementException("Collection is empty") var last = iterator.next() while (iterator.hasNext()) last = iterator.next() return last } /** * "Returns the last character. * @throws NoSuchElementException if the string is empty. */ public fun String.last(): Char { if (isEmpty()) throw NoSuchElementException("Collection is empty") return this[lastIndex] } /** * Returns the last element matching the given [predicate]. * @throws NoSuchElementException if no such element is found. */ public inline fun Array.last(predicate: (T) -> Boolean): T { for (index in this.indices.reversed()) { val element = this[index] if (predicate(element)) return element } throw NoSuchElementException("Collection doesn't contain any element matching the predicate.") } /** * Returns the last element matching the given [predicate]. * @throws NoSuchElementException if no such element is found. */ public inline fun BooleanArray.last(predicate: (Boolean) -> Boolean): Boolean { for (index in this.indices.reversed()) { val element = this[index] if (predicate(element)) return element } throw NoSuchElementException("Collection doesn't contain any element matching the predicate.") } /** * Returns the last element matching the given [predicate]. * @throws NoSuchElementException if no such element is found. */ public inline fun ByteArray.last(predicate: (Byte) -> Boolean): Byte { for (index in this.indices.reversed()) { val element = this[index] if (predicate(element)) return element } throw NoSuchElementException("Collection doesn't contain any element matching the predicate.") } /** * Returns the last element matching the given [predicate]. * @throws NoSuchElementException if no such element is found. */ public inline fun CharArray.last(predicate: (Char) -> Boolean): Char { for (index in this.indices.reversed()) { val element = this[index] if (predicate(element)) return element } throw NoSuchElementException("Collection doesn't contain any element matching the predicate.") } /** * Returns the last element matching the given [predicate]. * @throws NoSuchElementException if no such element is found. */ public inline fun DoubleArray.last(predicate: (Double) -> Boolean): Double { for (index in this.indices.reversed()) { val element = this[index] if (predicate(element)) return element } throw NoSuchElementException("Collection doesn't contain any element matching the predicate.") } /** * Returns the last element matching the given [predicate]. * @throws NoSuchElementException if no such element is found. */ public inline fun FloatArray.last(predicate: (Float) -> Boolean): Float { for (index in this.indices.reversed()) { val element = this[index] if (predicate(element)) return element } throw NoSuchElementException("Collection doesn't contain any element matching the predicate.") } /** * Returns the last element matching the given [predicate]. * @throws NoSuchElementException if no such element is found. */ public inline fun IntArray.last(predicate: (Int) -> Boolean): Int { for (index in this.indices.reversed()) { val element = this[index] if (predicate(element)) return element } throw NoSuchElementException("Collection doesn't contain any element matching the predicate.") } /** * Returns the last element matching the given [predicate]. * @throws NoSuchElementException if no such element is found. */ public inline fun LongArray.last(predicate: (Long) -> Boolean): Long { for (index in this.indices.reversed()) { val element = this[index] if (predicate(element)) return element } throw NoSuchElementException("Collection doesn't contain any element matching the predicate.") } /** * Returns the last element matching the given [predicate]. * @throws NoSuchElementException if no such element is found. */ public inline fun ShortArray.last(predicate: (Short) -> Boolean): Short { for (index in this.indices.reversed()) { val element = this[index] if (predicate(element)) return element } throw NoSuchElementException("Collection doesn't contain any element matching the predicate.") } /** * Returns the last element matching the given [predicate]. * @throws NoSuchElementException if no such element is found. */ public inline fun Iterable.last(predicate: (T) -> Boolean): T { if (this is List) return this.last(predicate) var last: T? = null var found = false for (element in this) { if (predicate(element)) { last = element found = true } } if (!found) throw NoSuchElementException("Collection doesn't contain any element matching the predicate.") return last as T } /** * Returns the last element matching the given [predicate]. * @throws NoSuchElementException if no such element is found. */ public inline fun List.last(predicate: (T) -> Boolean): T { for (index in this.indices.reversed()) { val element = this[index] if (predicate(element)) return element } throw NoSuchElementException("Collection doesn't contain any element matching the predicate.") } /** * Returns the last element matching the given [predicate]. * @throws NoSuchElementException if no such element is found. */ public inline fun Sequence.last(predicate: (T) -> Boolean): T { var last: T? = null var found = false for (element in this) { if (predicate(element)) { last = element found = true } } if (!found) throw NoSuchElementException("Collection doesn't contain any element matching the predicate.") return last as T } deprecated("Migrate to using Sequence and respective functions") /** * Returns the last element matching the given [predicate]. * @throws NoSuchElementException if no such element is found. */ public inline fun Stream.last(predicate: (T) -> Boolean): T { var last: T? = null var found = false for (element in this) { if (predicate(element)) { last = element found = true } } if (!found) throw NoSuchElementException("Collection doesn't contain any element matching the predicate.") return last as T } /** * "Returns the last character matching the given [predicate]. * @throws NoSuchElementException if no such character is found. */ public inline fun String.last(predicate: (Char) -> Boolean): Char { var last: Char? = null var found = false for (element in this) { if (predicate(element)) { last = element found = true } } if (!found) throw NoSuchElementException("Collection doesn't contain any element matching the predicate.") return last as Char } /** * Returns last index of *element*, or -1 if the collection does not contain element */ public fun Array.lastIndexOf(element: T): Int { if (element == null) { for (index in indices.reverse()) { if (this[index] == null) { return index } } } else { for (index in indices.reverse()) { if (element == this[index]) { return index } } } return -1 } /** * Returns last index of *element*, or -1 if the collection does not contain element */ public fun BooleanArray.lastIndexOf(element: Boolean): Int { for (index in indices.reverse()) { if (element == this[index]) { return index } } return -1 } /** * Returns last index of *element*, or -1 if the collection does not contain element */ public fun ByteArray.lastIndexOf(element: Byte): Int { for (index in indices.reverse()) { if (element == this[index]) { return index } } return -1 } /** * Returns last index of *element*, or -1 if the collection does not contain element */ public fun CharArray.lastIndexOf(element: Char): Int { for (index in indices.reverse()) { if (element == this[index]) { return index } } return -1 } /** * Returns last index of *element*, or -1 if the collection does not contain element */ public fun DoubleArray.lastIndexOf(element: Double): Int { for (index in indices.reverse()) { if (element == this[index]) { return index } } return -1 } /** * Returns last index of *element*, or -1 if the collection does not contain element */ public fun FloatArray.lastIndexOf(element: Float): Int { for (index in indices.reverse()) { if (element == this[index]) { return index } } return -1 } /** * Returns last index of *element*, or -1 if the collection does not contain element */ public fun IntArray.lastIndexOf(element: Int): Int { for (index in indices.reverse()) { if (element == this[index]) { return index } } return -1 } /** * Returns last index of *element*, or -1 if the collection does not contain element */ public fun LongArray.lastIndexOf(element: Long): Int { for (index in indices.reverse()) { if (element == this[index]) { return index } } return -1 } /** * Returns last index of *element*, or -1 if the collection does not contain element */ public fun ShortArray.lastIndexOf(element: Short): Int { for (index in indices.reverse()) { if (element == this[index]) { return index } } return -1 } /** * Returns last index of *element*, or -1 if the collection does not contain element */ public fun Iterable.lastIndexOf(element: T): Int { var lastIndex = -1 var index = 0 for (item in this) { if (element == item) lastIndex = index index++ } return lastIndex } /** * Returns last index of *element*, or -1 if the collection does not contain element */ public fun Sequence.lastIndexOf(element: T): Int { var lastIndex = -1 var index = 0 for (item in this) { if (element == item) lastIndex = index index++ } return lastIndex } deprecated("Migrate to using Sequence and respective functions") /** * Returns last index of *element*, or -1 if the collection does not contain element */ public fun Stream.lastIndexOf(element: T): Int { var lastIndex = -1 var index = 0 for (item in this) { if (element == item) lastIndex = index index++ } return lastIndex } /** * Returns the last element, or `null` if the collection is empty */ public fun Array.lastOrNull(): T? { return if (isEmpty()) null else this[size() - 1] } /** * Returns the last element, or `null` if the collection is empty */ public fun BooleanArray.lastOrNull(): Boolean? { return if (isEmpty()) null else this[size() - 1] } /** * Returns the last element, or `null` if the collection is empty */ public fun ByteArray.lastOrNull(): Byte? { return if (isEmpty()) null else this[size() - 1] } /** * Returns the last element, or `null` if the collection is empty */ public fun CharArray.lastOrNull(): Char? { return if (isEmpty()) null else this[size() - 1] } /** * Returns the last element, or `null` if the collection is empty */ public fun DoubleArray.lastOrNull(): Double? { return if (isEmpty()) null else this[size() - 1] } /** * Returns the last element, or `null` if the collection is empty */ public fun FloatArray.lastOrNull(): Float? { return if (isEmpty()) null else this[size() - 1] } /** * Returns the last element, or `null` if the collection is empty */ public fun IntArray.lastOrNull(): Int? { return if (isEmpty()) null else this[size() - 1] } /** * Returns the last element, or `null` if the collection is empty */ public fun LongArray.lastOrNull(): Long? { return if (isEmpty()) null else this[size() - 1] } /** * Returns the last element, or `null` if the collection is empty */ public fun ShortArray.lastOrNull(): Short? { return if (isEmpty()) null else this[size() - 1] } /** * Returns the last element, or `null` if the collection is empty */ public fun Iterable.lastOrNull(): T? { when (this) { is List<*> -> return if (isEmpty()) null else this[size() - 1] as T else -> { val iterator = iterator() if (!iterator.hasNext()) return null var last = iterator.next() while (iterator.hasNext()) last = iterator.next() return last } } } /** * Returns the last element, or `null` if the collection is empty */ public fun List.lastOrNull(): T? { return if (isEmpty()) null else this[size() - 1] } /** * Returns the last element, or `null` if the collection is empty */ public fun Sequence.lastOrNull(): T? { val iterator = iterator() if (!iterator.hasNext()) return null var last = iterator.next() while (iterator.hasNext()) last = iterator.next() return last } deprecated("Migrate to using Sequence and respective functions") /** * Returns the last element, or `null` if the collection is empty */ public fun Stream.lastOrNull(): T? { val iterator = iterator() if (!iterator.hasNext()) return null var last = iterator.next() while (iterator.hasNext()) last = iterator.next() return last } /** * Returns the last character, or `null` if the string is empty */ public fun String.lastOrNull(): Char? { return if (isEmpty()) null else this[length() - 1] } /** * Returns the last element matching the given [predicate], or `null` if no such element was found. */ public inline fun Array.lastOrNull(predicate: (T) -> Boolean): T? { for (index in this.indices.reversed()) { val element = this[index] if (predicate(element)) return element } return null } /** * Returns the last element matching the given [predicate], or `null` if no such element was found. */ public inline fun BooleanArray.lastOrNull(predicate: (Boolean) -> Boolean): Boolean? { for (index in this.indices.reversed()) { val element = this[index] if (predicate(element)) return element } return null } /** * Returns the last element matching the given [predicate], or `null` if no such element was found. */ public inline fun ByteArray.lastOrNull(predicate: (Byte) -> Boolean): Byte? { for (index in this.indices.reversed()) { val element = this[index] if (predicate(element)) return element } return null } /** * Returns the last element matching the given [predicate], or `null` if no such element was found. */ public inline fun CharArray.lastOrNull(predicate: (Char) -> Boolean): Char? { for (index in this.indices.reversed()) { val element = this[index] if (predicate(element)) return element } return null } /** * Returns the last element matching the given [predicate], or `null` if no such element was found. */ public inline fun DoubleArray.lastOrNull(predicate: (Double) -> Boolean): Double? { for (index in this.indices.reversed()) { val element = this[index] if (predicate(element)) return element } return null } /** * Returns the last element matching the given [predicate], or `null` if no such element was found. */ public inline fun FloatArray.lastOrNull(predicate: (Float) -> Boolean): Float? { for (index in this.indices.reversed()) { val element = this[index] if (predicate(element)) return element } return null } /** * Returns the last element matching the given [predicate], or `null` if no such element was found. */ public inline fun IntArray.lastOrNull(predicate: (Int) -> Boolean): Int? { for (index in this.indices.reversed()) { val element = this[index] if (predicate(element)) return element } return null } /** * Returns the last element matching the given [predicate], or `null` if no such element was found. */ public inline fun LongArray.lastOrNull(predicate: (Long) -> Boolean): Long? { for (index in this.indices.reversed()) { val element = this[index] if (predicate(element)) return element } return null } /** * Returns the last element matching the given [predicate], or `null` if no such element was found. */ public inline fun ShortArray.lastOrNull(predicate: (Short) -> Boolean): Short? { for (index in this.indices.reversed()) { val element = this[index] if (predicate(element)) return element } return null } /** * Returns the last element matching the given [predicate], or `null` if no such element was found. */ public inline fun Iterable.lastOrNull(predicate: (T) -> Boolean): T? { if (this is List) return this.lastOrNull(predicate) var last: T? = null for (element in this) { if (predicate(element)) { last = element } } return last } /** * Returns the last element matching the given [predicate], or `null` if no such element was found. */ public inline fun List.lastOrNull(predicate: (T) -> Boolean): T? { for (index in this.indices.reversed()) { val element = this[index] if (predicate(element)) return element } return null } /** * Returns the last element matching the given [predicate], or `null` if no such element was found. */ public inline fun Sequence.lastOrNull(predicate: (T) -> Boolean): T? { var last: T? = null for (element in this) { if (predicate(element)) { last = element } } return last } deprecated("Migrate to using Sequence and respective functions") /** * Returns the last element matching the given [predicate], or `null` if no such element was found. */ public inline fun Stream.lastOrNull(predicate: (T) -> Boolean): T? { var last: T? = null for (element in this) { if (predicate(element)) { last = element } } return last } /** * Returns the last character matching the given [predicate], or `null` if no such character was found. */ public inline fun String.lastOrNull(predicate: (Char) -> Boolean): Char? { var last: Char? = null for (element in this) { if (predicate(element)) { last = element } } return last } /** * Returns the single element, or throws an exception if the collection is empty or has more than one element. */ public fun Array.single(): T { return when (size()) { 0 -> throw NoSuchElementException("Collection is empty") 1 -> this[0] else -> throw IllegalArgumentException("Collection has more than one element") } } /** * Returns the single element, or throws an exception if the collection is empty or has more than one element. */ public fun BooleanArray.single(): Boolean { return when (size()) { 0 -> throw NoSuchElementException("Collection is empty") 1 -> this[0] else -> throw IllegalArgumentException("Collection has more than one element") } } /** * Returns the single element, or throws an exception if the collection is empty or has more than one element. */ public fun ByteArray.single(): Byte { return when (size()) { 0 -> throw NoSuchElementException("Collection is empty") 1 -> this[0] else -> throw IllegalArgumentException("Collection has more than one element") } } /** * Returns the single element, or throws an exception if the collection is empty or has more than one element. */ public fun CharArray.single(): Char { return when (size()) { 0 -> throw NoSuchElementException("Collection is empty") 1 -> this[0] else -> throw IllegalArgumentException("Collection has more than one element") } } /** * Returns the single element, or throws an exception if the collection is empty or has more than one element. */ public fun DoubleArray.single(): Double { return when (size()) { 0 -> throw NoSuchElementException("Collection is empty") 1 -> this[0] else -> throw IllegalArgumentException("Collection has more than one element") } } /** * Returns the single element, or throws an exception if the collection is empty or has more than one element. */ public fun FloatArray.single(): Float { return when (size()) { 0 -> throw NoSuchElementException("Collection is empty") 1 -> this[0] else -> throw IllegalArgumentException("Collection has more than one element") } } /** * Returns the single element, or throws an exception if the collection is empty or has more than one element. */ public fun IntArray.single(): Int { return when (size()) { 0 -> throw NoSuchElementException("Collection is empty") 1 -> this[0] else -> throw IllegalArgumentException("Collection has more than one element") } } /** * Returns the single element, or throws an exception if the collection is empty or has more than one element. */ public fun LongArray.single(): Long { return when (size()) { 0 -> throw NoSuchElementException("Collection is empty") 1 -> this[0] else -> throw IllegalArgumentException("Collection has more than one element") } } /** * Returns the single element, or throws an exception if the collection is empty or has more than one element. */ public fun ShortArray.single(): Short { return when (size()) { 0 -> throw NoSuchElementException("Collection is empty") 1 -> this[0] else -> throw IllegalArgumentException("Collection has more than one element") } } /** * Returns the single element, or throws an exception if the collection is empty or has more than one element. */ public fun Iterable.single(): T { when (this) { is List<*> -> return when (size()) { 0 -> throw NoSuchElementException("Collection is empty") 1 -> this[0] as T else -> throw IllegalArgumentException("Collection has more than one element") } else -> { val iterator = iterator() if (!iterator.hasNext()) throw NoSuchElementException("Collection is empty") var single = iterator.next() if (iterator.hasNext()) throw IllegalArgumentException("Collection has more than one element") return single } } } /** * Returns the single element, or throws an exception if the collection is empty or has more than one element. */ public fun List.single(): T { return when (size()) { 0 -> throw NoSuchElementException("Collection is empty") 1 -> this[0] else -> throw IllegalArgumentException("Collection has more than one element") } } /** * Returns the single element, or throws an exception if the collection is empty or has more than one element. */ public fun Sequence.single(): T { when (this) { is List<*> -> return when (size()) { 0 -> throw NoSuchElementException("Collection is empty") 1 -> this[0] as T else -> throw IllegalArgumentException("Collection has more than one element") } else -> { val iterator = iterator() if (!iterator.hasNext()) throw NoSuchElementException("Collection is empty") var single = iterator.next() if (iterator.hasNext()) throw IllegalArgumentException("Collection has more than one element") return single } } } deprecated("Migrate to using Sequence and respective functions") /** * Returns the single element, or throws an exception if the collection is empty or has more than one element. */ public fun Stream.single(): T { when (this) { is List<*> -> return when (size()) { 0 -> throw NoSuchElementException("Collection is empty") 1 -> this[0] as T else -> throw IllegalArgumentException("Collection has more than one element") } else -> { val iterator = iterator() if (!iterator.hasNext()) throw NoSuchElementException("Collection is empty") var single = iterator.next() if (iterator.hasNext()) throw IllegalArgumentException("Collection has more than one element") return single } } } /** * Returns the single character, or throws an exception if the string is empty or has more than one character. */ public fun String.single(): Char { return when (length()) { 0 -> throw NoSuchElementException("Collection is empty") 1 -> this[0] else -> throw IllegalArgumentException("Collection has more than one element") } } /** * Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element */ public inline fun Array.single(predicate: (T) -> Boolean): T { var single: T? = null var found = false for (element in this) { if (predicate(element)) { if (found) throw IllegalArgumentException("Collection contains more than one matching element") single = element found = true } } if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate") return single as T } /** * Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element */ public inline fun BooleanArray.single(predicate: (Boolean) -> Boolean): Boolean { var single: Boolean? = null var found = false for (element in this) { if (predicate(element)) { if (found) throw IllegalArgumentException("Collection contains more than one matching element") single = element found = true } } if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate") return single as Boolean } /** * Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element */ public inline fun ByteArray.single(predicate: (Byte) -> Boolean): Byte { var single: Byte? = null var found = false for (element in this) { if (predicate(element)) { if (found) throw IllegalArgumentException("Collection contains more than one matching element") single = element found = true } } if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate") return single as Byte } /** * Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element */ public inline fun CharArray.single(predicate: (Char) -> Boolean): Char { var single: Char? = null var found = false for (element in this) { if (predicate(element)) { if (found) throw IllegalArgumentException("Collection contains more than one matching element") single = element found = true } } if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate") return single as Char } /** * Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element */ public inline fun DoubleArray.single(predicate: (Double) -> Boolean): Double { var single: Double? = null var found = false for (element in this) { if (predicate(element)) { if (found) throw IllegalArgumentException("Collection contains more than one matching element") single = element found = true } } if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate") return single as Double } /** * Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element */ public inline fun FloatArray.single(predicate: (Float) -> Boolean): Float { var single: Float? = null var found = false for (element in this) { if (predicate(element)) { if (found) throw IllegalArgumentException("Collection contains more than one matching element") single = element found = true } } if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate") return single as Float } /** * Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element */ public inline fun IntArray.single(predicate: (Int) -> Boolean): Int { var single: Int? = null var found = false for (element in this) { if (predicate(element)) { if (found) throw IllegalArgumentException("Collection contains more than one matching element") single = element found = true } } if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate") return single as Int } /** * Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element */ public inline fun LongArray.single(predicate: (Long) -> Boolean): Long { var single: Long? = null var found = false for (element in this) { if (predicate(element)) { if (found) throw IllegalArgumentException("Collection contains more than one matching element") single = element found = true } } if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate") return single as Long } /** * Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element */ public inline fun ShortArray.single(predicate: (Short) -> Boolean): Short { var single: Short? = null var found = false for (element in this) { if (predicate(element)) { if (found) throw IllegalArgumentException("Collection contains more than one matching element") single = element found = true } } if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate") return single as Short } /** * Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element */ public inline fun Iterable.single(predicate: (T) -> Boolean): T { var single: T? = null var found = false for (element in this) { if (predicate(element)) { if (found) throw IllegalArgumentException("Collection contains more than one matching element") single = element found = true } } if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate") return single as T } /** * Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element */ public inline fun Sequence.single(predicate: (T) -> Boolean): T { var single: T? = null var found = false for (element in this) { if (predicate(element)) { if (found) throw IllegalArgumentException("Collection contains more than one matching element") single = element found = true } } if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate") return single as T } deprecated("Migrate to using Sequence and respective functions") /** * Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element */ public inline fun Stream.single(predicate: (T) -> Boolean): T { var single: T? = null var found = false for (element in this) { if (predicate(element)) { if (found) throw IllegalArgumentException("Collection contains more than one matching element") single = element found = true } } if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate") return single as T } /** * Returns the single character matching the given [predicate], or throws exception if there is no or more than one matching character */ public inline fun String.single(predicate: (Char) -> Boolean): Char { var single: Char? = null var found = false for (element in this) { if (predicate(element)) { if (found) throw IllegalArgumentException("Collection contains more than one matching element") single = element found = true } } if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate") return single as Char } /** * Returns single element, or `null` if the collection is empty or has more than one element. */ public fun Array.singleOrNull(): T? { return if (size() == 1) this[0] else null } /** * Returns single element, or `null` if the collection is empty or has more than one element. */ public fun BooleanArray.singleOrNull(): Boolean? { return if (size() == 1) this[0] else null } /** * Returns single element, or `null` if the collection is empty or has more than one element. */ public fun ByteArray.singleOrNull(): Byte? { return if (size() == 1) this[0] else null } /** * Returns single element, or `null` if the collection is empty or has more than one element. */ public fun CharArray.singleOrNull(): Char? { return if (size() == 1) this[0] else null } /** * Returns single element, or `null` if the collection is empty or has more than one element. */ public fun DoubleArray.singleOrNull(): Double? { return if (size() == 1) this[0] else null } /** * Returns single element, or `null` if the collection is empty or has more than one element. */ public fun FloatArray.singleOrNull(): Float? { return if (size() == 1) this[0] else null } /** * Returns single element, or `null` if the collection is empty or has more than one element. */ public fun IntArray.singleOrNull(): Int? { return if (size() == 1) this[0] else null } /** * Returns single element, or `null` if the collection is empty or has more than one element. */ public fun LongArray.singleOrNull(): Long? { return if (size() == 1) this[0] else null } /** * Returns single element, or `null` if the collection is empty or has more than one element. */ public fun ShortArray.singleOrNull(): Short? { return if (size() == 1) this[0] else null } /** * Returns single element, or `null` if the collection is empty or has more than one element. */ public fun Iterable.singleOrNull(): T? { when (this) { is List<*> -> return if (size() == 1) this[0] as T else null else -> { val iterator = iterator() if (!iterator.hasNext()) return null var single = iterator.next() if (iterator.hasNext()) return null return single } } } /** * Returns single element, or `null` if the collection is empty or has more than one element. */ public fun List.singleOrNull(): T? { return if (size() == 1) this[0] else null } /** * Returns single element, or `null` if the collection is empty or has more than one element. */ public fun Sequence.singleOrNull(): T? { when (this) { is List<*> -> return if (size() == 1) this[0] as T else null else -> { val iterator = iterator() if (!iterator.hasNext()) return null var single = iterator.next() if (iterator.hasNext()) return null return single } } } deprecated("Migrate to using Sequence and respective functions") /** * Returns single element, or `null` if the collection is empty or has more than one element. */ public fun Stream.singleOrNull(): T? { when (this) { is List<*> -> return if (size() == 1) this[0] as T else null else -> { val iterator = iterator() if (!iterator.hasNext()) return null var single = iterator.next() if (iterator.hasNext()) return null return single } } } /** * Returns the single character, or `null` if the string is empty or has more than one character. */ public fun String.singleOrNull(): Char? { return if (length() == 1) this[0] else null } /** * Returns the single element matching the given [predicate], or `null` if element was not found or more than one element was found */ public inline fun Array.singleOrNull(predicate: (T) -> Boolean): T? { var single: T? = null var found = false for (element in this) { if (predicate(element)) { if (found) return null single = element found = true } } if (!found) return null return single } /** * Returns the single element matching the given [predicate], or `null` if element was not found or more than one element was found */ public inline fun BooleanArray.singleOrNull(predicate: (Boolean) -> Boolean): Boolean? { var single: Boolean? = null var found = false for (element in this) { if (predicate(element)) { if (found) return null single = element found = true } } if (!found) return null return single } /** * Returns the single element matching the given [predicate], or `null` if element was not found or more than one element was found */ public inline fun ByteArray.singleOrNull(predicate: (Byte) -> Boolean): Byte? { var single: Byte? = null var found = false for (element in this) { if (predicate(element)) { if (found) return null single = element found = true } } if (!found) return null return single } /** * Returns the single element matching the given [predicate], or `null` if element was not found or more than one element was found */ public inline fun CharArray.singleOrNull(predicate: (Char) -> Boolean): Char? { var single: Char? = null var found = false for (element in this) { if (predicate(element)) { if (found) return null single = element found = true } } if (!found) return null return single } /** * Returns the single element matching the given [predicate], or `null` if element was not found or more than one element was found */ public inline fun DoubleArray.singleOrNull(predicate: (Double) -> Boolean): Double? { var single: Double? = null var found = false for (element in this) { if (predicate(element)) { if (found) return null single = element found = true } } if (!found) return null return single } /** * Returns the single element matching the given [predicate], or `null` if element was not found or more than one element was found */ public inline fun FloatArray.singleOrNull(predicate: (Float) -> Boolean): Float? { var single: Float? = null var found = false for (element in this) { if (predicate(element)) { if (found) return null single = element found = true } } if (!found) return null return single } /** * Returns the single element matching the given [predicate], or `null` if element was not found or more than one element was found */ public inline fun IntArray.singleOrNull(predicate: (Int) -> Boolean): Int? { var single: Int? = null var found = false for (element in this) { if (predicate(element)) { if (found) return null single = element found = true } } if (!found) return null return single } /** * Returns the single element matching the given [predicate], or `null` if element was not found or more than one element was found */ public inline fun LongArray.singleOrNull(predicate: (Long) -> Boolean): Long? { var single: Long? = null var found = false for (element in this) { if (predicate(element)) { if (found) return null single = element found = true } } if (!found) return null return single } /** * Returns the single element matching the given [predicate], or `null` if element was not found or more than one element was found */ public inline fun ShortArray.singleOrNull(predicate: (Short) -> Boolean): Short? { var single: Short? = null var found = false for (element in this) { if (predicate(element)) { if (found) return null single = element found = true } } if (!found) return null return single } /** * Returns the single element matching the given [predicate], or `null` if element was not found or more than one element was found */ public inline fun Iterable.singleOrNull(predicate: (T) -> Boolean): T? { var single: T? = null var found = false for (element in this) { if (predicate(element)) { if (found) return null single = element found = true } } if (!found) return null return single } /** * Returns the single element matching the given [predicate], or `null` if element was not found or more than one element was found */ public inline fun Sequence.singleOrNull(predicate: (T) -> Boolean): T? { var single: T? = null var found = false for (element in this) { if (predicate(element)) { if (found) return null single = element found = true } } if (!found) return null return single } deprecated("Migrate to using Sequence and respective functions") /** * Returns the single element matching the given [predicate], or `null` if element was not found or more than one element was found */ public inline fun Stream.singleOrNull(predicate: (T) -> Boolean): T? { var single: T? = null var found = false for (element in this) { if (predicate(element)) { if (found) return null single = element found = true } } if (!found) return null return single } /** * Returns the single character matching the given [predicate], or `null` if character was not found or more than one character was found */ public inline fun String.singleOrNull(predicate: (Char) -> Boolean): Char? { var single: Char? = null var found = false for (element in this) { if (predicate(element)) { if (found) return null single = element found = true } } if (!found) return null return single }