Skip to content

Instantly share code, notes, and snippets.

@ray1729
Created March 6, 2015 07:31
Show Gist options
  • Save ray1729/c09c002d3d6adba4cad2 to your computer and use it in GitHub Desktop.
Save ray1729/c09c002d3d6adba4cad2 to your computer and use it in GitHub Desktop.
(def xs (sort (repeatedly 30 #(rand-int 20))))
(def ys (sort (repeatedly 25 #(rand-int 20))))
(defn merge-sorted-seqs
[xs ys]
(lazy-seq (cond
(empty? xs) (seq ys)
(empty? ys) (seq xs)
:else (let [x (first xs) y (first ys)]
(if (<= x y)
(cons x (merge-sorted-seqs (rest xs) ys))
(cons y (merge-sorted-seqs xs (rest ys))))))))
;; If you then want to process equal elements together:
(partition-by identity (merge-sorted-seqs xs ys))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment