Skip to content

Instantly share code, notes, and snippets.

@ummels
Created November 26, 2014 17:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ummels/db802124955f8d10e609 to your computer and use it in GitHub Desktop.
Save ummels/db802124955f8d10e609 to your computer and use it in GitHub Desktop.
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