Skip to content

Instantly share code, notes, and snippets.

@maxcountryman
Created January 16, 2014 15:41
Show Gist options
  • Save maxcountryman/8457044 to your computer and use it in GitHub Desktop.
Save maxcountryman/8457044 to your computer and use it in GitHub Desktop.
(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