So in the Kotlin in Action book, it talks about using Sequences when you have a large data set so you don’t incur the larger memory in dealing with each filter map function compared to using a standard List or Iterable.
In my gist CollectionAndSequence.kt · GitHub I wrote some code to average out over 1000 iterations of running filter map from 100-1,000,000 times
I’m not sure why this is the case, but List came out on top for larger sets on average instead of smaller sets of 100.
fun squareNumbers(range: CollectionRange): List<Int> = range.rangeList.filter { it % 3 == 0 }.map { it * it }.filter { it % 4 == 0 }.map { it * 3 }.filter { it % 4 == 0 }
fun squareNumbersSequence(range: CollectionRange) = range.rangeList.asSequence().filter { it % 3 == 0 }.map { it * it }.filter { it % 4 == 0 }.map { it * 3 }.filter { it % 4 == 0 }.toList()
I did filter{}.map{}.filter{}.map{}.filter{} as the list of operations. Can someone shed some light why Sequences perform slower than lists for filter map compared to List Iterables?