Skip to content

Instantly share code, notes, and snippets.

@purcell
Created December 9, 2009 15:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save purcell/252528 to your computer and use it in GitHub Desktop.
Save purcell/252528 to your computer and use it in GitHub Desktop.
;; Feedback on http://www.fatvat.co.uk/2009/07/merging-rss-feeds.html
;; Original
(defn join-all
"Join the lists provided using f to select an element each time"
[f & lists]
(let [l (remove empty? lists)]
(when-not (empty? l)
(let [n (reduce f (map first l))
c (count (filter #(= (first %) n) l))
r (map #(if (= (first %) n) (rest %) %) l)]
(lazy-seq
(concat
(repeat c n)
(apply join-all (cons f r))))))))
;; Alternative version
(defn join-all [f & lists]
(when-let [all (not-empty (apply concat lists))]
(let [chosen (apply f all)
marked (map #(vector (= chosen %) %) all)]
(lazy-seq
(concat (map second (filter first marked))
(join-all f (map second (filter (complement first) marked))))))))
(use 'clojure.test)
(deftest examples
(is (= nil
(join-all min '())))
(is (= '(1 1 1 2 3 3)
(join-all min [1 2 3] [1 1 3])))
(is (= '(1 1 1 2 3 3)
(join-all min [3 2 1] [3 1 1])))
(is (= '(1 2 3 4 5 6 7 8 9 10)
(join-all min [2 4 6 8 10] [1 3 5 7 9]))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment