From 6fc7c3495ae15d258f929624f00c5803fcdceddc Mon Sep 17 00:00:00 2001 From: Ilya Matveev Date: Wed, 12 Apr 2017 18:17:10 +0700 Subject: [PATCH] stdlib: Add List and MutableList functions --- .../kotlin/kotlin/collections/Collections.kt | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/runtime/src/main/kotlin/kotlin/collections/Collections.kt b/runtime/src/main/kotlin/kotlin/collections/Collections.kt index 8369e854f9e..d2e3835e7f6 100644 --- a/runtime/src/main/kotlin/kotlin/collections/Collections.kt +++ b/runtime/src/main/kotlin/kotlin/collections/Collections.kt @@ -97,6 +97,26 @@ public fun listOfNotNull(element: T?): List = /** Returns a new read-only list only of those given elements, that are not null. */ public fun listOfNotNull(vararg elements: T?): List = 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 List(size: Int, init: (index: Int) -> T): List = 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 MutableList(size: Int, init: (index: Int) -> T): MutableList { + val list = ArrayList(size) + repeat(size) { index -> list.add(init(index)) } + return list +} + /** * Returns an [IntRange] of the valid indices for this collection. */