Created
April 5, 2015 01:36
-
-
Save jafingerhut/41e9ceb258d1ddf05eff to your computer and use it in GitHub Desktop.
merge-sorted-seqs.clj
This file contains 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
;; 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