Created
May 2, 2015 00:14
-
-
Save aphyr/4b52097592c82101c173 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 mostly-small-nonempty-subset | |
"Returns a subset of the given collection, with a logarithmically decreasing | |
probability of selecting more elements. Always selects at least one element. | |
(->> #(mostly-small-nonempty-subset [1 2 3 4 5]) | |
repeatedly | |
(map count) | |
(take 10000) | |
frequencies | |
sort) | |
; => ([1 3824] [2 2340] [3 1595] [4 1266] [5 975])" | |
[xs] | |
(-> xs | |
count | |
inc | |
Math/log | |
rand | |
Math/exp | |
long | |
(take (shuffle xs)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment