Created
May 20, 2010 19:37
-
-
Save drewr/407988 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
| (ns foo.randomizer | |
| (:use [clojure.test])) | |
| (defn delete [coll idx] | |
| (if (<= (inc idx) (count coll)) | |
| (let [coll (vec coll)] | |
| (concat (subvec coll 0 idx) (subvec coll (inc idx) (count coll)))) | |
| coll)) | |
| (defn randomize [coll] | |
| (loop [from coll to []] | |
| (if (seq from) | |
| (let [idx (rand-int (count from))] | |
| (recur (delete from idx) (conj to (nth (vec from) idx)))) | |
| to))) | |
| (deftest test-rand | |
| (testing "should return the same number of elements" | |
| (is (= 3 (count (randomize [1 2 3]))))) | |
| (testing "should not be the same vector" | |
| (let [v (range 100) | |
| vprime (randomize v)] | |
| (is (not= v vprime)) | |
| (is (apply = (map #(into #{} %) [v vprime])))))) | |
| (comment | |
| (randomize ['tim 'phil 'kevin 'jim 'drew 'dan 'steve 'paul]) | |
| ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment