stdlib: Add List and MutableList functions

This commit is contained in:
Ilya Matveev
2017-04-12 18:17:10 +07:00
committed by ilmat192
parent 569fb7e013
commit 6fc7c3495a
@@ -97,6 +97,26 @@ public fun <T : Any> listOfNotNull(element: T?): List<T> =
/** Returns a new read-only list only of those given elements, that are not null. */
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.
*/