Is there a more elegant non-imperative way to do t...
# announcements
h
Is there a more elegant non-imperative way to do this (i.e. to collect
values
and
elems
)?
Copy code
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