diff options
author | scgilardi <scgilardi@gmail.com> | 2009-02-15 05:35:40 +0000 |
---|---|---|
committer | scgilardi <scgilardi@gmail.com> | 2009-02-15 05:35:40 +0000 |
commit | f4bc2008141857fbe32eeb54c8fcd0183b27ffee (patch) | |
tree | bbd5ce54b5d96a27ff0678d51ddaa0413e257a06 /src/clojure | |
parent | 08806a7a5ebc62d8243b1389eaf6fe00b3f6674f (diff) |
fix issue 29: move sequence functions from lazy_seqs to seq_utils
Diffstat (limited to 'src/clojure')
-rw-r--r-- | src/clojure/contrib/lazy_seqs.clj | 42 | ||||
-rw-r--r-- | src/clojure/contrib/seq_utils.clj | 31 |
2 files changed, 32 insertions, 41 deletions
diff --git a/src/clojure/contrib/lazy_seqs.clj b/src/clojure/contrib/lazy_seqs.clj index 9b40af26..1806cca2 100644 --- a/src/clojure/contrib/lazy_seqs.clj +++ b/src/clojure/contrib/lazy_seqs.clj @@ -21,17 +21,8 @@ ;; ;; == Lazy sequence functions == ;; -;; rotations - returns a lazy seq of all the rotations of a seq -;; -;; partition-all - like built-in partition, but does not throw away -;; the last partition if it is too short -;; -;; (permutations and combinations used to be here, but have -;; been moved to combinatorics.clj) -;; -;; random-permutation - from Jason Wolfe -;; -;; random-element - from Jason Wolfe +;; (rotations, partition-all, shuffle, rand-elt moved to seq_utils.clj) +;; (permutations and combinations moved to combinatorics.clj) ;; ;; [1] http://www.cs.hmc.edu/~oneill/papers/Sieve-JFP.pdf ;; [2] http://clj-me.blogspot.com/2008/06/primes.html @@ -63,32 +54,3 @@ (defvar powers-of-2 (lazy-cons 1 (map #(bit-shift-left % 1) powers-of-2)) "A lazy sequence of all the powers of 2") - -(defn rotations - "Returns a lazy seq of all rotations of a seq" - [x] - (if (seq x) - (map - (fn [n _] - (lazy-cat (drop n x) (take n x))) - (iterate inc 0) x) - (list nil))) - -(defn partition-all - "Lazily break s into chunks of length n (or less, for the - final chunk)." - [n s] - (when (seq s) - (lazy-cons (take n s) - (partition-all n (drop n s))))) - -(defn random-permutation - "Return a random permutation of coll" - [coll] - (let [l (java.util.ArrayList. coll)] - (java.util.Collections/shuffle l) - (seq l))) - -(defn random-element [s] - "Return a random element of this seq" - (nth s (rand-int (count s)))) diff --git a/src/clojure/contrib/seq_utils.clj b/src/clojure/contrib/seq_utils.clj index c559cb1b..36a8e07d 100644 --- a/src/clojure/contrib/seq_utils.clj +++ b/src/clojure/contrib/seq_utils.clj @@ -121,4 +121,33 @@ (rec-cons self (first coll) (map f self (rest coll))) (cons (f) nil))) ([f init coll] - (rec-cons self init (map f self coll))))
\ No newline at end of file + (rec-cons self init (map f self coll)))) + +(defn rotations + "Returns a lazy seq of all rotations of a seq" + [x] + (if (seq x) + (map + (fn [n _] + (lazy-cat (drop n x) (take n x))) + (iterate inc 0) x) + (list nil))) + +(defn partition-all + "Lazily break s into chunks of length n (or less, for the + final chunk)." + [n s] + (when (seq s) + (lazy-cons (take n s) + (partition-all n (drop n s))))) + +(defn shuffle + "Return a random permutation of coll" + [coll] + (let [l (java.util.ArrayList. coll)] + (java.util.Collections/shuffle l) + (seq l))) + +(defn rand-elt [s] + "Return a random element of this seq" + (nth s (rand-int (count s)))) |