Created
August 24, 2014 17:49
-
-
Save krishnabhargav/4da9378614dce56a4bdd to your computer and use it in GitHub Desktop.
merging two sequences in clojure
This file contains hidden or 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
(defn seq-merge [a-seq b-seq] | |
(cond (empty? a-seq) b-seq ;; retur non-empty sequence if any of the sequence is empty | |
(empty? b-seq) a-seq | |
:else (let [first-a (first a-seq) | |
first-b (first b-seq)] | |
(if (< first-a first-b) ;;if picking the first item, the rest of a-seq or vice versa | |
(cons first-a | |
(seq-merge (rest a-seq) b-seq)) | |
(cons first-b | |
(seq-merge a-seq (rest b-seq))))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment