Created
February 1, 2011 07:32
-
-
Save amalloy/805546 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 take-randnth [num coll] | |
(take num | |
(rest | |
(map first | |
(iterate (fn [[ret items]] | |
(let [idx (rand-int (count items))] | |
[(items idx) | |
(subvec (assoc items idx (items 0)) | |
1)])) | |
[nil | |
(vec coll)]))))) | |
(defn lazy-shuffle [coll] | |
((fn shuffle [^clojure.lang.ITransientVector coll] | |
(lazy-seq | |
(let [c (count coll)] | |
(when-not (zero? c) | |
(let [n (rand-int c)] | |
(cons (get coll n) | |
(shuffle (.pop (assoc! coll n (get coll (dec c))))))))))) | |
(transient (vec coll)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment