hastebrot
08/06/2016, 10:27 AMvalues
and elems
)?
fun main(args: Array<String>) {
// input: An infinite sequence of integers.
// output: A sequence of N values of the input sequence, with the first element copied to the
// end of the sequence. The first element of the output sequence wrapped into a
// Head object, the rest wrapped into Next objects.
val sequence = generateSequence(/* startValue */ 5) { it + 1 }
val values = sequence.take(/* numOfValues */ 3).toList().let { it + it.first() }
val elems = values.mapIndexed { index, value ->
if (index == 0) Head(value) else Next(value)
}
println(elems) // [Head(value=5), Next(value=6), Next(value=7), Next(value=5)]
}
interface Element
data class Head(val value: Int) : Element
data class Next(val value: Int) : Element