Skip to content

Instantly share code, notes, and snippets.

@alandipert
Last active December 17, 2015 02:29
Show Gist options
  • Save alandipert/5536325 to your computer and use it in GitHub Desktop.
Save alandipert/5536325 to your computer and use it in GitHub Desktop.
(defn interleave*
"Like interleave, but doesn't quit on the shortest seq."
[& colls]
(when-let [have-stuff (seq (keep seq colls))]
(lazy-seq (concat (map first have-stuff)
(apply interleave* (map rest have-stuff))))))
(defn pairs-seq [m]
(->> (keys m)
sort
(map #(partition 2 (interleave (repeat %) (get m %))))
(apply interleave*)))
(defn subset [m n]
(reduce #(update-in %1 [(first %2)] (fnil conj []) (second %2)) {}
(take n (pairs-seq m))))
(def m1 {:x [1 2] :y [3 4] :z [5]})
(subset m1 3) ;=> {:x [1], :y [3], :z [5]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment