Don’t know if this is possible, but I have some code like this:
val list = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
val evens = list.filter { e => e % 2 == 0 }
if(someCondition) {
val result = evens.filter { e => e % 3 == 0 }
} else {
val result = evens.filter { e => e % 5 == 0 }
}
But I don’t want to iterate over all elements twice, so is there a way that I can create a “generic pick-all-the-evens numbers on this collection” and apply some other function, so that it would only iterate once?
If you turn
listinto a lazy collection, such as anIterator, then you can apply all the filter operations (or other things likemapetc) in one pass:When you convert the collection to an Iterator with
.iterator, Scala will keep track of the operations to be performed (here, twofilters), but will wait to perform them until the result is actually accessed (here, via the call to.toList).So I might rewrite your code like this:
Depending on exactly what you want to do, you might want an
Iterator, aStream, or aView. They are all lazily computed (so the one-pass aspect will apply), but they differ on things like whether they can be iterated over multiple times (StreamandView) or whether they keep the computed value around for later access (Stream).To really see these different lazy behaviors, try running this bit of code and set
<OPERATION>to eithertoList,iterator,view, ortoStream:Here’s the behavior you will see:
List(or any other non-lazy collection): Eachfilteris requires a separate iteration through the collection. The resulting filtered collection is stored in memory so that eachforeachcan just display it.Iterator: Bothfilters and the firstforeachare done in a single iteration. The secondforeachdoes nothing since theIteratorhas been consumed. Results are not stored in memory.View: Bothforeachcalls result in their own single-pass iteration over the collection to perform thefilters. Results are not stored in memory.Stream: Bothfilters and the firstforeachare done in a single iteration. The resulting filtered collection is stored in memory so that eachforeachcan just display it.