leira
12/21/2017 - 6:45 PM

Codewars Hamming Numbers https://www.codewars.com/kata/hamming-numbers/train/clojure. A demo of sort-merge and recursive lazy seqs

Codewars Hamming Numbers https://www.codewars.com/kata/hamming-numbers/train/clojure. A demo of sort-merge and recursive lazy seqs

(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)