Skip to content

Provide Collections#shuffle as extensions for collections in JS#1300

Closed
l0rinc wants to merge 2 commits intoJetBrains:1.2-M2from
l0rinc:1.2-M2
Closed

Provide Collections#shuffle as extensions for collections in JS#1300
l0rinc wants to merge 2 commits intoJetBrains:1.2-M2from
l0rinc:1.2-M2

Conversation

@l0rinc
Copy link
Copy Markdown
Contributor

@l0rinc l0rinc commented Sep 17, 2017

Fixes: #KT-2460

swap(i, j)
}
}
}
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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 :)


for (i in 0..array.size - 1) {
for (i in array.indices) {
list[i] = array[i]
Copy link
Copy Markdown
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
Copy Markdown
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

Pap Lőrinc added 2 commits September 26, 2017 00:50
@ilya-g
Copy link
Copy Markdown
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
Copy Markdown
Contributor Author

l0rinc commented Oct 5, 2017

Sure :)

@ilya-g
Copy link
Copy Markdown
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
Copy Markdown
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