Advertisement
Guest User

Untitled

a guest
May 28th, 2015
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.68 KB | None | 0 0
  1. (defn conj-not-empty [coll item]
  2. (if-not item coll (conj coll item)))
  3.  
  4. (defn mergesorted
  5. ([collections]
  6. (mergesorted collections <))
  7.  
  8. ([collections comparator]
  9. (mergesorted collections comparator (transient [])))
  10.  
  11. ([collections comparator sorted]
  12. (if (<= (count collections) 1)
  13. (concat (persistent! sorted) (first collections))
  14.  
  15. (let [[with-smallest & anothers] (sort-by first comparator collections)
  16. [smallest & tail] with-smallest
  17. collections (conj-not-empty anothers tail)]
  18. (recur collections comparator (conj! sorted smallest))))))
  19.  
  20. (mergesorted [[0] [1 5] [2 6] [7] [3] [4] [3] [-1] [8] [5]]) ; (-1 0 1 2 3 3 4 5 5 6 7 8)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement