Stream is essentially a fancy Iterator. It implements custom API, which is similar to Iterators's, but is faster and fully specialized for primitives
Once Stream is enabled, Scala collections receive a new implicit method .all, which returns Stream of all collection elements:
import scalqa.Stream.Enable._
val list = (1 to 1000).toList
list .filter(_ % 2 == 0).flatMap(_ => list).map(_ / 2L).sum
list.all.filter(_ % 2 == 0).flatMap(_ => list).map(_ / 2L).sum
The above two statements calculate same value, but first is done with native List methods, the second uses Stream, which in this case is over 500% faster
If one types .all before doing heavy data transformations, the program will run faster, while using less memory
Calling <collection>.all is similar to creating lazy view with <collection>.view, except Stream is faster and specialized
Generally Scalqa/Stream framework can be useful in 3 different scenarios:
Stream can be an extension of Scala collections to get performance advantage
Stream is essentially a fancy Iterator. It implements custom API, which is similar to Iterators's, but is faster and fully specialized for primitives
Once Stream is enabled, Scala collections receive a new implicit method
.all
, which returns Stream of all collection elements:The above two statements calculate same value, but first is done with native List methods, the second uses Stream, which in this case is over 500% faster
If one types
.all
before doing heavy data transformations, the program will run faster, while using less memoryCalling <collection>.all is similar to creating lazy view with <collection>.view, except Stream is faster and specialized
Generally Scalqa/Stream framework can be useful in 3 different scenarios: