aboutsummaryrefslogtreecommitdiff
path: root/src/clojure/contrib/lazy_seqs.clj
diff options
context:
space:
mode:
authorscgilardi <scgilardi@gmail.com>2009-02-10 04:12:41 +0000
committerscgilardi <scgilardi@gmail.com>2009-02-10 04:12:41 +0000
commit69a3e673fb2a6323dd203a1f8645f49f8593e1be (patch)
treecfb659d8bbfba024fae56b833e4fdbf515240e13 /src/clojure/contrib/lazy_seqs.clj
parent93e6eb3633d533b3340d40ede107638a2a447077 (diff)
fix issue 8, lazy_seqs.clj, from Jason Wolfe
Diffstat (limited to 'src/clojure/contrib/lazy_seqs.clj')
-rw-r--r--src/clojure/contrib/lazy_seqs.clj35
1 files changed, 13 insertions, 22 deletions
diff --git a/src/clojure/contrib/lazy_seqs.clj b/src/clojure/contrib/lazy_seqs.clj
index 87a256e9..53104124 100644
--- a/src/clojure/contrib/lazy_seqs.clj
+++ b/src/clojure/contrib/lazy_seqs.clj
@@ -26,6 +26,10 @@
;; (permutations and combinations used to be here, but have
;; been moved to combinatorics.clj)
;;
+;; random-permutation - from Jason Wolfe
+;;
+;; random-element - from Jason Wolfe
+;;
;; [1] http://www.cs.hmc.edu/~oneill/papers/Sieve-JFP.pdf
;; [2] http://clj-me.blogspot.com/2008/06/primes.html
;;
@@ -67,26 +71,13 @@
(iterate inc 0) x)
(list nil)))
+(defn random-permutation
+ "Return a random permutation of coll"
+ [coll]
+ (let [l (java.util.ArrayList. coll)]
+ (java.util.Collections/shuffle l)
+ (seq l)))
-;; See combinatorics.clj for faster versions of these functions.
-
-;; (defn permutations
-;; "Returns a lazy seq of all permutations of a seq"
-;; [x]
-;; (if (seq x)
-;; (mapcat
-;; (fn [[f & r]]
-;; (map #(cons f %) (permutations r)))
-;; (rotations x))
-;; (list nil)))
-
-;; (defn combinations
-;; "Returns a lazy seq of all combinations built of one item from each seq given.
-;; See also (doc for)"
-;; [& acs]
-;; (let [step (fn step [head [s & cs :as acs]]
-;; (if acs
-;; (mapcat #(step (conj head %) cs) s)
-;; (list head)))]
-;; (when acs
-;; (step [] acs))))
+(defn random-element [s]
+ "Return a random element of this seq"
+ (nth s (rand-int (count s))))