-
Notifications
You must be signed in to change notification settings - Fork 5.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Provide Collections#shuffle as extensions for collections in JS #1300
Conversation
} | ||
} | ||
} | ||
private fun rand(upperBound: Int) = Math.floor(Math.random() * upperBound) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Couldn't find any way to make Random
customizable in JavaScript
, therefore that variant is left out.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're going to provide common Random
, see https://youtrack.jetbrains.com/issue/KT-17261, though it isn't there yet.
} | ||
} | ||
} | ||
private fun rand(upperBound: Int) = Math.floor(Math.random() * upperBound) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This requires importing kotlin.js.Math
class.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, done.
Not sure how to test this exactly, any help would be appreciated! :)
public fun <T> MutableList<T>.shuffle(): Unit { | ||
(lastIndex downTo 1).forEach { i -> | ||
rand(i + 1).let { j -> | ||
swap(i, j) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's no swap
in JS currently
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed with a private method instead, thanks!
*/ | ||
@SinceKotlin("1.2") | ||
public fun <T> MutableList<T>.shuffle(): Unit { | ||
(lastIndex downTo 1).forEach { i -> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
JS backend doesn't optimize such loops, so it would be better to rewrite it as plain old while
loop.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for (i in lastIndex downTo 1) { ... }
is already supported by optimizer
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @konsoletyper, that's exactly what I changed it to :)
@@ -112,7 +133,7 @@ private fun <T> collectionsSort(list: MutableList<T>, comparator: Comparator<in | |||
|
|||
array.asDynamic().sort(comparator.asDynamic().compare.bind(comparator)) | |||
|
|||
for (i in 0..array.size - 1) { | |||
for (i in array.indices) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
JS backend doesn't optimize such loops ATM.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, changed it to 0..array.lastIndex
Contains low risk refactorings and formattings
…n JS Fixes: #KT-2460
I've rebased it to the latest 1.2 branch and run it on build server. The branch is 1.2-Beta2...rrr/1.2-Beta2/pr1300 If you don't mind I'll squash the first two commits. |
Sure :) |
I've merged it to the master, thanks for your effort. |
Thanks! |
Fixes: #KT-2460