Created
December 4, 2013 17:23
-
-
Save maxcountryman/7791707 to your computer and use it in GitHub Desktop.
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
(definterface INode | |
(getCar []) | |
(getCdr []) | |
(setCar [x]) | |
(setCdr [x]) | |
(valAt [i])) | |
(deftype Node [^:volatile-mutable car ^:volatile-mutable cdr] | |
INode | |
(getCar [_] car) | |
(getCdr [_] cdr) | |
(setCar [this x] (set! car x) this) | |
(setCdr [this x] (set! cdr x) this) | |
(valAt [this index] (loop [i index cursor this] | |
(if (zero? i) | |
cursor | |
(recur (dec i) (.cdr cursor))))) | |
clojure.lang.Indexed | |
(nth [this i] (.valAt this i)) | |
(nth [this i v] (if-let [n (nth this i)] (Node. v n))) | |
clojure.lang.Seqable | |
(seq [this] this) | |
clojure.lang.ISeq | |
(first [this] this) | |
(more [this] (if-let [n (next this)] n ())) | |
(next [this] (if-let [n (.getCdr this)] n))) | |
(defn new-node [car & [cdr]] | |
(Node. car cdr)) | |
(first (new-node "foo")) ;; StackOverFlow |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment