aboutsummaryrefslogtreecommitdiff
path: root/src/clojure/contrib
diff options
context:
space:
mode:
authorStuart Halloway <stu@thinkrelevance.com>2009-04-06 13:00:43 +0000
committerStuart Halloway <stu@thinkrelevance.com>2009-04-06 13:00:43 +0000
commitb5c63ac653ad023e8902e56f7236cfb74156cb9e (patch)
tree3e09c4c793ea4001f6008e74b2c6b315152e4d83 /src/clojure/contrib
parentdbe731701f9a2ad9f8a41fa2aee2dfe8513be8ec (diff)
converted lazy seqs to functions so they won't hold their heads
Diffstat (limited to 'src/clojure/contrib')
-rw-r--r--src/clojure/contrib/lazy_seqs.clj42
-rw-r--r--src/clojure/contrib/test_contrib.clj3
-rw-r--r--src/clojure/contrib/test_contrib/test_lazy_seqs.clj21
3 files changed, 48 insertions, 18 deletions
diff --git a/src/clojure/contrib/lazy_seqs.clj b/src/clojure/contrib/lazy_seqs.clj
index 118a5602..cf83b5de 100644
--- a/src/clojure/contrib/lazy_seqs.clj
+++ b/src/clojure/contrib/lazy_seqs.clj
@@ -33,24 +33,32 @@
(ns clojure.contrib.lazy-seqs
(:use clojure.contrib.def))
+; primes cannot be written efficiently as a function, because
+; it needs to look back on the whole sequence. contrast with
+; fibs and powers-of-2 which only need a fixed buffer of 1 or 2
+; previous values.
(defvar primes
- (lazy-cat [2 3 5 7]
+ (concat
+ [2 3 5 7]
+ (lazy-seq
(let [primes-from
- (fn primes-from [n [f & r]]
- (if (some #(zero? (rem n %))
- (take-while #(<= (* % %) n) primes))
- (recur (+ n f) r)
- (lazy-seq (cons n (primes-from (+ n f) r)))))
- wheel (cycle [2 4 2 4 6 2 6 4 2 4 6 6 2 6 4 2
- 6 4 6 8 4 2 4 2 4 8 6 4 6 2 4 6
- 2 6 6 4 2 4 6 2 6 4 2 4 2 10 2 10])]
- (primes-from 11 wheel)))
- "A lazy sequence of all the prime numbers.")
+ (fn primes-from [n [f & r]]
+ (if (some #(zero? (rem n %))
+ (take-while #(<= (* % %) n) primes))
+ (recur (+ n f) r)
+ (lazy-seq (cons n (primes-from (+ n f) r)))))
+ wheel (cycle [2 4 2 4 6 2 6 4 2 4 6 6 2 6 4 2
+ 6 4 6 8 4 2 4 2 4 8 6 4 6 2 4 6
+ 2 6 6 4 2 4 6 2 6 4 2 4 2 10 2 10])]
+ (primes-from 11 wheel))))
+ "Lazy sequence of all the prime numbers.")
+
+(defn fibs []
+ "Returns a lazy sequence of all the Fibonacci numbers."
+ (map first (iterate (fn [[a b]] [b (+ a b)]) [0 1])))
+
+(defn powers-of-2 []
+ "Returns a lazy sequence of all the powers of 2"
+ (iterate #(bit-shift-left % 1) 1))
-(defvar fibs
- (lazy-cat [0 1] (map + fibs (rest fibs)))
- "A lazy sequence of all the fibonacci numbers.")
-(defvar powers-of-2
- (lazy-seq (cons 1 (map #(bit-shift-left % 1) powers-of-2)))
- "A lazy sequence of all the powers of 2")
diff --git a/src/clojure/contrib/test_contrib.clj b/src/clojure/contrib/test_contrib.clj
index 0aa7432d..7c1444e0 100644
--- a/src/clojure/contrib/test_contrib.clj
+++ b/src/clojure/contrib/test_contrib.clj
@@ -17,7 +17,8 @@
(:gen-class))
(def test-names [:complex-numbers :monads :pprint.pretty :pprint.cl-format
- :str-utils :shell-out :test-graph :test-dataflow :test-java-utils])
+ :str-utils :shell-out :test-graph :test-dataflow :test-java-utils
+ :test-lazy-seqs])
(def test-namespaces
(map #(symbol (str "clojure.contrib.test-contrib." (name %)))
diff --git a/src/clojure/contrib/test_contrib/test_lazy_seqs.clj b/src/clojure/contrib/test_contrib/test_lazy_seqs.clj
new file mode 100644
index 00000000..3bf4ba78
--- /dev/null
+++ b/src/clojure/contrib/test_contrib/test_lazy_seqs.clj
@@ -0,0 +1,21 @@
+(ns clojure.contrib.test-contrib.test-lazy-seqs
+ (:use clojure.contrib.test-is
+ clojure.contrib.lazy-seqs))
+
+(deftest test-fibs
+ (is (= [0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946
+ 17711 28657 46368 75025 121393 196418 317811 514229 832040 1346269 2178309
+ 3524578 5702887 9227465 14930352 24157817 39088169 63245986 102334155
+ 165580141 267914296 433494437 701408733 1134903170 1836311903 2971215073
+ 4807526976 7778742049]
+ (take 50 (fibs)))))
+
+(deftest test-powers-of-2
+ (is (= [1 2 4 8 16 32 64 128 256 512]
+ (take 10 (powers-of-2)))))
+
+(deftest test-primes
+ (is (= [2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101
+ 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197
+ 199 211 223 227 229]
+ (take 50 primes))))