Created
November 26, 2014 17:05
-
-
Save ummels/db802124955f8d10e609 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object Util { | |
/** Merges a collection of streams into a single stream. | |
* | |
* Returns a sorted stream if all input streams are sorted. | |
*/ | |
def mergeStreams[A](streams: Traversable[Stream[A]])(implicit ord: Ordering[A]): Stream[A] = { | |
val streams1 = streams.toList filterNot (_.isEmpty) sortBy (_.head) | |
if (streams1.isEmpty) | |
Stream.empty | |
else { | |
val first = streams1.head | |
first.head #:: mergeStreams(first.tail :: streams1.tail) | |
} | |
} | |
/** Merges several streams into a single stream. | |
* | |
* Returns a sorted stream if all input streams are sorted. | |
*/ | |
def mergeStreams[A](streams: Stream[A]*)(implicit ord: Ordering[A]): Stream[A] = | |
mergeStreams(streams) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment