Skip to content

Instantly share code, notes, and snippets.

@jafingerhut
Created April 5, 2015 01:36
Show Gist options
  • Save jafingerhut/41e9ceb258d1ddf05eff to your computer and use it in GitHub Desktop.
Save jafingerhut/41e9ceb258d1ddf05eff to your computer and use it in GitHub Desktop.
merge-sorted-seqs.clj
;; Example:
;; (merge-sorted-seqs < (range 10 1000 3) (range 12 1000 5))
(defn merge-sorted-seqs [cmpf seq1 seq2]
(let [seq1wins (and (seq seq1)
(or (nil? (seq seq2))
(cmpf (first seq1) (first seq2))))]
(if seq1wins
(lazy-seq (cons (first seq1)
(merge-sorted-seqs cmpf (next seq1) seq2)))
(when (seq seq2)
(lazy-seq (cons (first seq2)
(merge-sorted-seqs cmpf seq1 (next seq2))))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment