merge - Merging streams in scala -
i need help merging 2 streams one. output has follows:
(elem1list1#elem1list2, elem2list1#elem2list2...)
and function breaks if of streams empty
def mergestream(a: stream[a], b: stream[a]):stream[a] = if (a.isempty || b.isempty) nil else (a,b) match { case(x#::xs, y#::ys) => x#::y }
any clue how prepare it?
you can zip
2 stream
s together, truncate longer stream
, , flatmap
them out of tuples:
a.zip(b).flatmap { case (a, b) => stream(a, b) }
though cannot speak it's efficiency.
scala> val = stream(1,2,3,4) a: scala.collection.immutable.stream[int] = stream(1, ?) scala> val b = stream.from(3) b: scala.collection.immutable.stream[int] = stream(3, ?) scala> val c = a.zip(b).flatmap { case (a, b) => stream(a, b) }.take(10).tolist c: list[int] = list(1, 3, 2, 4, 3, 5, 4, 6)
scala merge stream
No comments:
Post a Comment