Hi zvg, thanks for the question!
The solution to the challenge using sequences here required just a bit of a stretch.
Check out the following code - which applies the distinct
function to the sequence, just before the take
function:
val one = listOf("larry", "moe", "curly")
val two = listOf("boggs", "scaggs", "simpson")
fun main(args: Array<String>) {
val patrons = generateSequence {
one.shuffled().first() + two.shuffled().first()
}.distinct()
.take(9)
.toSet()
println(patrons.size)
}
Notice that the number 9, the max unique value for combining these two lists, is always printed.
This may behave a bit differently than what you’d expect since you might think distinct would apply to only a single element, but if you dig into the implementation of distinct you will see that it actually applies to all elements (take a look at the DistinctIterator
class’s computeNext
function to see how it works).
The result is that when distinct
and take
are called on the sequence, all values from the sequence will be unique ones.
Hope that helps! Thanks again for the question.