Skip to content

Commit

Permalink
Changed streams to use himBHnotation and deflazy
Browse files Browse the repository at this point in the history
  • Loading branch information
jmgimeno committed Jan 7, 2012
1 parent 7f76024 commit 43eeb68
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 15 deletions.
43 changes: 30 additions & 13 deletions src/okasaki/streams.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,39 @@
(:use datatype.core))

(defdatatype
::Streams
::StreamCell
Nil
(^:datatype.core/lazy Cons first rest))
(Cons first rest))

;; Streams are ($ StreamCells)

(defun s-first [stream]
[[Cons x _]] x)
[($ [Cons x _])] x)

(defunlazy s-rest [stream]
[($ [Cons _ r])] r)

(defunlazy s-append [stream1 stream2]
[($ Nil) t] t
[($ [Cons x s]) t] ($ (->Cons x (s-append s t))))

(defunlazy s-take [number stream]
[0 s ] ($ Nil)
[_ ($ Nil) ] ($ Nil)
[n ($ [Cons x s])] ($ (->Cons x (s-take (dec n) s))))

(defun s-drop_ [number stream]
[0 s] s
[n ($ Nil)] ($ Nil)
[n ($ [Cons x s])] (recur (dec n) s))

(defunlazy s-drop [number stream]
[n s] (s-drop_ n s))

(defun s-rest [stream]
[[Cons _ r]] r)
(defun s-reverse_ [stream result]
[($ Nil) r] r
[($ [Cons x s]) r] (recur s ($ (->Cons x r))))

(defun s-take [number stream]
[0 s] s
[_ Nil] Nil
[n [Cons x s]] (->Cons x (s-take (dec n) s)))
(defunlazy s-reverse [stream]
[s] (s-reverse_ s ($ Nil)))

(defun s-drop [number stream]
[0 s] s
[_ Nil] Nil
[n [Cons _ s]] (recur (dec n) s))
24 changes: 24 additions & 0 deletions src/okasaki/streams2.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
(ns okasaki.streams2
(:use datatype.core))

(defdatatype
::Streams
Nil
(^:datatype.core/lazy Cons first rest))

(defun s-first [stream]
[[Cons x _]] x)

(defun s-rest [stream]
[[Cons _ r]] r)

(defun s-take [number stream]
[0 s] s
[_ Nil] Nil
[n [Cons x s]] (->Cons x (s-take (dec n) s)))

(defun s-drop [number stream]
[0 s] s
[_ Nil] Nil
[n [Cons _ s]] (recur (dec n) s))

18 changes: 18 additions & 0 deletions test/okasaki/streams2_tests.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
(ns okasaki.streams2-tests
(:use okasaki.streams2
clojure.test))

(defn nats
([] (nats 0))
([n] (->Cons n (nats (inc n)))))

(deftest first-element-stream-of-length-one
(is (= 0 (s-first (->Cons 0 Nil)))))

(deftest first-nat-is-zero
(is (zero? (s-first (nats)))))

(deftest we-can-get-the-nth-nat
(let [nth #(s-first (s-drop % (nats)))]
(is (= 10 (nth 10)))))

55 changes: 53 additions & 2 deletions test/okasaki/streams_tests.clj
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
(ns okasaki.streams-tests
(:use okasaki.streams
datatype.core
clojure.test))

(defn nats
([] (nats 0))
([n] (->Cons n (nats (inc n)))))
([n] ($ (->Cons n (nats (inc n))))))

(deftest first-element-stream-of-length-one
(is (= 0 (s-first (->Cons 0 Nil)))))
(is (= 0 (s-first ($ (->Cons 0 Nil))))))

(deftest first-nat-is-zero
(is (zero? (s-first (nats)))))
Expand All @@ -16,4 +17,54 @@
(let [nth #(s-first (s-drop % (nats)))]
(is (= 10 (nth 10)))))

(defn- counted [counter value]
(swap! counter inc)
value)

(deftest we-can-append-lazyly
(let [num-evals (atom 0)
nil1 ($ (counted num-evals Nil))
str1 ($ (counted num-evals (->Cons 3 nil1)))
str1 ($ (counted num-evals (->Cons 2 str1)))
str1 ($ (counted num-evals (->Cons 1 str1)))
nil2 ($ (counted num-evals Nil))
str2 ($ (counted num-evals (->Cons 5 nil2)))
str2 ($ (counted num-evals (->Cons 4 str2)))
res (s-append str1 str2)]
(is (= 0 @num-evals))
(is (= 1 (s-first res)))
(is (= 1 @num-evals))
(is (= 2 (s-first (s-rest res))))
(is (= 2 @num-evals))
(is (= 3 (s-first (s-rest (s-rest res)))))
(is (= 3 @num-evals))
(is (= 4 (s-first (s-rest (s-rest (s-rest res))))))
(is (= 5 @num-evals))))

(deftest we-can-take-lazyly
(let [num-evals (atom 0)
str (reduce #($ (counted num-evals (->Cons %2 %1))) ($ (counted num-evals Nil)) [5 4 3 2 1])]
(is (= 0 @num-evals))
(is (= 1 (s-first (s-take 3 str))))
(is (= 1 @num-evals))
(is (= 2 (s-first (s-rest (s-take 3 str)))))
(is (= 2 @num-evals))))

(deftest we-can-take-lazyly
(let [num-evals (atom 0)
str (reduce #($ (counted num-evals (->Cons %2 %1))) ($ (counted num-evals Nil)) [5 4 3 2 1])]
(is (= 0 @num-evals))
(is (= 1 (s-first (s-drop 0 str))))
(is (= 1 @num-evals))
(s-drop 3 str)
(is (= 1 @num-evals))
(is (= 4 (s-first (s-drop 3 str))))
(is (= 4 @num-evals))))

(deftest we-can-reverse-a-stream
(let [num-evals (atom 0)
str (reduce #($ (counted num-evals (->Cons %2 %1))) ($ (counted num-evals Nil)) [5 4 3 2 1])]
(is (= 0 @num-evals))
(is (= 5 (s-first (s-reverse str))))
(is (= 6 @num-evals))))

0 comments on commit 43eeb68

Please sign in to comment.