Last active
December 17, 2015 02:29
-
-
Save alandipert/5536325 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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