Last active
December 21, 2017 20:48
-
-
Save leira/4c8ed5d5177534a236eaf1a3977ca29f to your computer and use it in GitHub Desktop.
Codewars Hamming Numbers https://www.codewars.com/kata/hamming-numbers/train/clojure. A demo of sort-merge and recursive lazy seqs
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
| (defn sort-merge [colls] | |
| (if (<= (count colls) 1) | |
| (first colls) | |
| (let [mi (apply min (map first colls))] | |
| (cons mi | |
| (lazy-seq | |
| (->> (map (fn [[f & r :as coll]] | |
| (if (= f mi) r coll)) | |
| colls) | |
| (filter some?) | |
| sort-merge)))))) | |
| ;(take 20 (sort-merge [(iterate (partial * 2) 1) (iterate (partial * 3) 1)])) | |
| (def hams | |
| (cons 1 | |
| ; (lazy-seq | |
| (sort-merge | |
| [(map (partial * 2) hams) | |
| (map (partial * 3) hams) | |
| (map (partial * 5) hams)]))) | |
| (take 50 hams) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment