Skip to content

Instantly share code, notes, and snippets.

@leira
Last active December 21, 2017 20:48
Show Gist options
  • Select an option

  • Save leira/4c8ed5d5177534a236eaf1a3977ca29f to your computer and use it in GitHub Desktop.

Select an option

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