Created
January 16, 2014 15:41
-
-
Save maxcountryman/8457044 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 []) | |
(setCar [x]) | |
(getCdr []) | |
(setCdr [n]) | |
(cons [x]) | |
(reverse [])) | |
(deftype Node | |
[^:volatile-mutable car ^:volatile-mutable ^INode cdr] | |
INode | |
(getCar [_] car) | |
(setCar [_ x] (set! car x)) | |
(getCdr [_] cdr) | |
(setCdr [_ n] (set! cdr n)) | |
(cons [this x] (Node. x this)) | |
(reverse [this] | |
(loop [cur this new-head nil] | |
(if-not cur | |
(or new-head this) | |
(recur (.getCdr cur) (Node. (.getCar cur) new-head))))) | |
clojure.lang.Seqable | |
(seq [this] | |
(loop [cur this acc ()] | |
(if-not cur | |
acc | |
(recur (.getCdr cur) (concat acc (list (.getCar cur)))))))) | |
(defn linked-list [& [xs]] | |
(let [xs (reverse xs) | |
ll (Node. (first xs) nil)] | |
(if-let [more (next xs)] | |
(reduce #(.cons ^INode %1 %2) ll more) | |
ll))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment