stdlib: Add List and MutableList functions
This commit is contained in:
@@ -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. */
|
/** 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()
|
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.
|
* Returns an [IntRange] of the valid indices for this collection.
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user