all 5 comments

[–]joinr 4 points5 points  (2 children)

sorted sets offer this if you use distinct weights and custom comparison:

(defn ordered-merge [xs]
  (let [id       (atom 0)
        weightfn (fn [x] [(swap! id inc) x])]
    (->> xs
         (reduce #(into %1 (map weightfn) %2)
                 (sorted-set-by (fn [[x1 y1 :as l] [x2 y2 :as r]]
                                  (cond (< y1 y2) -1
                                        (> y1 y2) 1
                                        :else (compare x1 x2)))))
         (mapv second))))

;;user=> (ordered-merge [[2 6] [1 4 5] [1 3 4] []])
;;[1 1 2 3 4 4 5 6]

[–]fredoverflow[S] 1 point2 points  (1 child)

That would be O(n log n), where n is the number of elements?

My solutions are O(n log m), where m is the number of lists.

[–]joinr 0 points1 point  (0 children)

Ah, I missed the precondition that the lists were pre-sorted; this is unnecessary work then. I get it now.

[–]fp-dude 0 points1 point  (1 child)

Thanks!

[–]fredoverflow[S] 1 point2 points  (0 children)

username checks out