Look at the code below:
val a = List(1,2,3,4,5)
a.filter(_ >= 3).map(_ * 9).drop(1).take(2)
My understanding is that it this iterates through the list once for each operation, generating code similar to this:
for(i <- a) {
// filter
}
for (i <- afiltered) {
// map
}
for (i <- afilteredandmapped) {
// drop
}
..etc
Is there anyway to combine these operations, having chaining operations that only iterates once through the list? Generating such as below:
for (i <- a) {
// filter
// map
// drop
// take
}
Stream
instead ofList
.view
is supposed to do, but it also has some other subtle effects:a.view.filter(_>=3).map(_*9).drop(1).take(2).toList