Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
(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