New course! Every coder should learn Generative AI!
Try a free lesson+ 7
This code will extend and provide working methods.
// rand function for shuffled.
import java.util.Random
val random = Random()
fun rand(from: Int, to: Int) = random.nextInt(
to-from)+from
// Extend Iterable<Int> with a shuffled function.
fun Iterable<Int>.shuffled(): List<Int> {
var count = this.count()
var list = MutableList(count, {-1})
for (loop in 0 until count) {
var index = rand(0, count-1)
while (list[index] != -1)
index = (index+1)%count
list[index] = loop
} // for
for (loop in 0 until count)
list[loop] = this.elementAt(list[loop])
return list.toList()
} // shuffled
// Extend Interable<String> with chunked.
fun Iterable<String>.chunked(size: Int): List<List<String>> {
var count = this.count()
val outer = (count+size-1)/size
return List(outer, {index -> List(
if (count > size) {
count -= size
size
} else
count, {this.elementAt(index*size+it)})})
} // chunked
+ 3
Just FYI, your suggestion/request has been logged. Thanks for providing feedback.