Update. Not perfect, but better. I wonder why it ...
# announcements
h
Update. Not perfect, but better. I wonder why it always resets the sequence when I use
it
.
Copy code
val sequence = generateSequence(/* startValue */ 3) { it + 1 }
val values = sequence.take(/* numOfValues */ 3).let {
    it + it.take(1)
}
val elements = values.let {
    it.take(1).map { Head(it) } + it.drop(1).map { Next(it) }
}
println(values.toList()) // [3, 4, 5, 3]
println(elements.toList()) // [Head(3), Next(4), Next(5), Next(3)]
Update 2: Oh, I have to use
sequence.constrainOnce()
. Update 3: It is possible to consume the first element of a sequence and continue by using a tempory iterator:
Copy code
val seq = listOf(1, 2, 3).asSequence().constrainOnce()
val iter = seq.iterator()
println(iter.next())
println(iter.asSequence().take(1).first())