Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

split a stream in many

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.

like image 341
Mikhail Golubtsov Avatar asked Jun 14 '13 18:06

Mikhail Golubtsov


People also ask

Can one stream be reused and visited many times?

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.

Can we use two filter in stream?

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.

What are Streams in Java?

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.


1 Answers

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)
like image 109
Mikhail Golubtsov Avatar answered Oct 05 '22 23:10

Mikhail Golubtsov