Solution: Understanding sequences

fun m(i: Int): Int { print("m$i ") return i * i } fun f(i: Int): Boolean { print("f$i ") return i % 2 == 0 } fun main() { val list = listOf(1, 2, 3, 4) list.map(::m).filter(::f) // m1 m2 m3 m4 f1 f4 f9 f16 list.filter(::f).map(::m) // f1 f2 f3 f4 m2 m4 // (notice that using filter first is more efficient) val sequence = sequenceOf(1, 2, 3, 4) sequence.map(::m).filter(::f).toList() // m1 f1 m2 f4 m3 f9 m4 f16 sequence.map(::m).filter(::f) // (nothing) // (a sequence does nothing until a terminal operation) sequence.map(::m).filter(::f).first() // m1 f1 m2 f4 sequence.filter(::f).map(::m).toList() // f1 f2 m2 f3 f4 m4 val sequence2 = list.asSequence().map(::m) // (nothing) // (a sequence does nothing until a terminal operation) sequence2.toList() // m1 m2 m3 m4 sequence2.filter(::f).toList() // m1 f1 m2 f4 m3 f9 m4 f16 }