For example, how to combine two streams of sorted integers? I thought it was very thorough, but just found it non-trivial at all. Below, one is not tail recursive, and it will overflow the stack when the threads are large.
def merge(as: Stream[Int], bs: Stream[Int]): Stream[Int] = { (as, bs) match { case (Stream.Empty, bss) => bss case (ass, Stream.Empty) => ass case (a
We might want to turn it into a tail recursive by introducing a battery. However, if we first close the battery, we will only get a stream of reverse order; if we add a concatenated battery (# :), it will no longer be lazy (strict).
What could be the solution? Thanks
source share