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

Improvement suggestions for shuffle #4

Closed
MarkusKramer opened this issue Nov 23, 2016 · 1 comment
Closed

Improvement suggestions for shuffle #4

MarkusKramer opened this issue Nov 23, 2016 · 1 comment
Assignees

Comments

@MarkusKramer
Copy link

MarkusKramer commented Nov 23, 2016

I was looking for code to do shuffle. I feel there are two problems with your solution:

  1. the function mutates the list given to it (and sort of pretends that it didn't by returning the result of the shuffle additionally as it's return value)
  2. it doesn't use existing code for shuffling

I wrote this code for my project:

/**
 * Returns a randomized list.
 */
fun <T> Iterable<T>.shuffle(seed: Long? = null): List<T> {
    val list = this.toMutableList()
    val random = if (seed != null) Random(seed) else Random()
    Collections.shuffle(list, random)
    return list
}

It reuses existing code to do the shuffling and can be called like other kotlin collection functions.

@gazolla
Copy link
Owner

gazolla commented Jul 2, 2017

Thanks Marcus ! Appreciate your contribution ! Project updated !

@gazolla gazolla self-assigned this Aug 22, 2017
@gazolla gazolla closed this as completed Aug 22, 2017
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

No branches or pull requests

2 participants