Skip to content

Instantly share code, notes, and snippets.

@krasserm
Created September 26, 2010 08:02
Show Gist options
  • Save krasserm/597718 to your computer and use it in GitHub Desktop.
Save krasserm/597718 to your computer and use it in GitHub Desktop.
def merge(a: Stream[Int], b: Stream[Int], c: Stream[Int]): Stream[Int] = {
val m = a.head min b.head min c.head
Stream.cons(m, merge(
if (m < a.head) a else a.tail,
if (m < b.head) b else b.tail,
if (m < c.head) c else c.tail))
}
def hamming: Stream[Int] = Stream.cons(1, merge(
hamming.map(_ * 2),
hamming.map(_ * 3),
hamming.map(_ * 5)))
hamming.take(20).print
// Prints
// 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, 16, 18, 20, 24, 25, 27, 30, 32, 36, empty
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment