Skip to content
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

Closed
wants to merge 2 commits into from

Conversation

l0rinc
Copy link
Contributor

@l0rinc l0rinc commented Sep 17, 2017

Fixes: #KT-2460

}
}
}
private fun rand(upperBound: Int) = Math.floor(Math.random() * upperBound)
Copy link
Contributor Author

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.

Copy link
Member

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)
Copy link
Member

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.

Copy link
Contributor Author

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)
Copy link
Member

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

Copy link
Contributor Author

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 ->
Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, thanks!

Copy link
Contributor

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

Copy link
Contributor Author

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) {
Copy link
Member

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.

Copy link
Contributor Author

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

@ilya-g
Copy link
Member

ilya-g commented Oct 5, 2017

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
As far as I can tell, all relevant tests are passing: https://teamcity.jetbrains.com/viewLog.html?buildId=1187205

If you don't mind I'll squash the first two commits.

@l0rinc
Copy link
Contributor Author

l0rinc commented Oct 5, 2017

Sure :)

@ilya-g
Copy link
Member

ilya-g commented Oct 13, 2017

I've merged it to the master, thanks for your effort.
781ca6d~2...781ca6d

@ilya-g ilya-g closed this Oct 13, 2017
@l0rinc
Copy link
Contributor Author

l0rinc commented Oct 13, 2017

Thanks!

@l0rinc l0rinc deleted the 1.2-M2 branch October 13, 2017 14:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants