Last active
March 27, 2016 20:06
-
-
Save gfredericks/4341fbb1aafbadbb5a63 to your computer and use it in GitHub Desktop.
a lazy shuffle function in clojure; probably not original
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 lazy-shuffle | |
"Returns a locally-shuffled lazy seq from the given collection. The first | |
arg has something to do with how it works so make sure it's a good one." | |
[window-size coll] | |
(let [[xs more] (split-at window-size coll)] | |
((fn self [v more] | |
(lazy-seq | |
(if (empty? more) | |
v | |
(let [[x & more] more | |
idx (rand-int window-size)] | |
(cons (get v idx) | |
(self (assoc v idx x) more)))))) | |
(vec (shuffle xs)) | |
more))) | |
user> (take 100 (lazy-shuffle 10 (range))) | |
=> (7 0 8 6 2 9 11 1 10 5 16 4 13 12 22 20 17 26 18 14 27 29 21 15 32 23 35 24 33 34 25 28 30 40 36 19 37 45 31 43 3 38 49 39 51 50 44 46 42 57 53 47 52 59 48 56 55 63 41 64 66 67 5\ | |
4 58 71 74 69 73 76 68 65 80 60 79 62 72 84 85 70 61 75 78 88 81 82 89 95 86 90 96 83 87 77 100 98 101 94 103 99 93) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment