Skip to content

Instantly share code, notes, and snippets.

@krishnabhargav
Created August 24, 2014 17:49
Show Gist options
  • Save krishnabhargav/4da9378614dce56a4bdd to your computer and use it in GitHub Desktop.
Save krishnabhargav/4da9378614dce56a4bdd to your computer and use it in GitHub Desktop.
merging two sequences in clojure
(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