-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changed streams to use himBHnotation and deflazy
- Loading branch information
Showing
4 changed files
with
125 additions
and
15 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
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)) | ||
|
This file contains 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
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))))) | ||
|
This file contains 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