Skip to content

Instantly share code, notes, and snippets.

@fffej
Created July 13, 2009 06:49
Show Gist options
  • Save fffej/145970 to your computer and use it in GitHub Desktop.
Save fffej/145970 to your computer and use it in GitHub Desktop.
(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 (partial = true) (map (fn [x] (= (first x) n)) l)))
r (map (fn [x] (if (= (first x) n) (rest x) x)) l)]
(lazy-seq
(concat
(repeat c n)
(apply join-all (cons f r))))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment