Skip to content

Instantly share code, notes, and snippets.

@amalloy
Created February 1, 2011 07:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save amalloy/805546 to your computer and use it in GitHub Desktop.
Save amalloy/805546 to your computer and use it in GitHub Desktop.
(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