Introduce array-like list instantiation functions.

#KT-14935 Fixed
This commit is contained in:
Ilya Gorbunov
2016-12-26 22:20:53 +03:00
parent d7885e699c
commit 5db75c993b
2 changed files with 26 additions and 0 deletions
@@ -104,6 +104,26 @@ public fun <T : Any> listOfNotNull(element: T?): List<T> = if (element != null)
/** Returns a new read-only list only of those given elements, that are not null. The returned list is serializable (JVM). */
public fun <T : Any> listOfNotNull(vararg elements: T?): List<T> = elements.filterNotNull()
/**
* Creates a new read-only list with the specified [size], where each element is calculated by calling the specified
* [init] function. The [init] function returns a list element given its index.
*/
@SinceKotlin("1.1")
@kotlin.internal.InlineOnly
public inline fun <T> List(size: Int, init: (index: Int) -> T): List<T> = MutableList(size, init)
/**
* Creates a new mutable list with the specified [size], where each element is calculated by calling the specified
* [init] function. The [init] function returns a list element given its index.
*/
@SinceKotlin("1.1")
@kotlin.internal.InlineOnly
public inline fun <T> MutableList(size: Int, init: (index: Int) -> T): MutableList<T> {
val list = ArrayList<T>(size)
repeat(size) { index -> list.add(init(index)) }
return list
}
/**
* Returns an [IntRange] of the valid indices for this collection.
*/