Created
January 24, 2012 17:43
-
-
Save sheki/1671444 to your computer and use it in GitHub Desktop.
Solution to http://puzzles.sheki.in/puzzle/hamming_numbers/
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
| def merge(as: Stream[Int], bs: Stream[Int], cs: Stream[Int]): Stream[Int] = { | |
| val h = as.head min bs.head min cs.head | |
| Stream.cons( | |
| h, | |
| merge(as dropWhile {_ == h}, bs dropWhile {_ == h}, cs dropWhile {_ == h}) | |
| ) | |
| } | |
| This function produces a stream containing a constant times each value from another: | |
| val prod = (k: Int) => (s: Stream[Int]) => s.map{k * _} | |
| With those definitions in hand… | |
| def hamming: Stream[Int] = Stream.cons( | |
| 1, | |
| merge(prod(2)(hamming), prod(3)(hamming), prod(5)(hamming)) | |
| ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment