diff --git a/js/js.libraries/src/core/collections.kt b/js/js.libraries/src/core/collections.kt index 52f54646867..d7b25d8c587 100644 --- a/js/js.libraries/src/core/collections.kt +++ b/js/js.libraries/src/core/collections.kt @@ -17,7 +17,7 @@ package kotlin.collections import kotlin.comparisons.naturalOrder -import kotlin.js.Math +import kotlin.math.floor /** Returns the array if it's not `null`, or an empty array otherwise. */ @kotlin.internal.InlineOnly @@ -100,17 +100,13 @@ public fun MutableList.fill(value: T): Unit { @SinceKotlin("1.2") public fun MutableList.shuffle(): Unit { for (i in lastIndex downTo 1) { - rand(i + 1).let { j -> - swap(this, i, j) - } + val j = rand(i + 1) + val copy = this[i] + this[i] = this[j] + this[j] = copy + } } -private fun rand(upperBound: Int) = Math.floor(Math.random() * upperBound) -private fun swap(list: MutableList, i: Int, j: Int): Unit { - val copy = list[i] - list[i] = list[j] - list[j] = copy -} - +private fun rand(upperBound: Int) = floor(kotlin.js.Math.random() * upperBound).toInt() /** * Returns a new list with the elements of this list randomly shuffled. @@ -139,7 +135,7 @@ private fun collectionsSort(list: MutableList, comparator: Comparator Array?.orEmpty(): Array expect inline fun Collection.toTypedArray(): Array -@SinceKotlin("1.2") header fun MutableList.fill(value: T): Unit -@SinceKotlin("1.2") header fun MutableList.shuffle(): Unit -@SinceKotlin("1.2") header fun Iterable.shuffled(): List +@SinceKotlin("1.2") +expect fun MutableList.fill(value: T): Unit +@SinceKotlin("1.2") +expect fun MutableList.shuffle(): Unit +@SinceKotlin("1.2") +expect fun Iterable.shuffled(): List -header fun > MutableList.sort(): Unit -header fun MutableList.sortWith(comparator: Comparator): Unit +expect fun > MutableList.sort(): Unit +expect fun MutableList.sortWith(comparator: Comparator): Unit // from Grouping.kt