I'd like to know if there a elegant way to achieve something like that:
val l = Stream.from(1)
val parts = l.some_function(3) //any number
parts.foreach( println(_) )
> 1,4,7,10...
> 2,5,8,11...
> 3,6,9,12...
Actually I need such operation on Streams for parallelization - to split the data across multiple actors without loading the whole stuff into memory.
So the simple answer is : NO, we cannot reuse the streams or traverse the streams multiple times. Any attempt to do so will result in error : Stream has already been operated on or closed.
The Stream API allows chaining multiple filters. We can leverage this to satisfy the complex filtering criteria described. Besides, we can use the not Predicate if we want to negate conditions.
A stream is a sequence of objects that supports various methods which can be pipelined to produce the desired result. The features of Java stream are – A stream is not a data structure instead it takes input from the Collections, Arrays or I/O channels.
The answer from Split a scala list into n interleaving lists fully meets the conditions, a little bit modified to suit Streams:
def round[A](seq: Iterable[A], n: Int) = {
(0 until n).map(i => seq.drop(i).sliding(1, n).flatten)
}
round(Stream.from(1),3).foreach(i => println(i.take(3).toList))
List(1, 4, 7)
List(2, 5, 8)
List(3, 6, 9)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With