Skip to content

Instantly share code, notes, and snippets.

@maxcountryman
Created December 4, 2013 17:23
Show Gist options
  • Save maxcountryman/7791707 to your computer and use it in GitHub Desktop.
Save maxcountryman/7791707 to your computer and use it in GitHub Desktop.
(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