Skip to content

Instantly share code, notes, and snippets.

@maxcountryman
Last active December 30, 2015 06:39
Show Gist options
  • Save maxcountryman/7790657 to your computer and use it in GitHub Desktop.
Save maxcountryman/7790657 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))))))
(defn new-node [car & [cdr]]
(Node. car cdr))
(defn list-reverse [nodes]
(loop [cur nodes more (.getCdr nodes) reversed nil]
(if-not cur
reversed
(recur more (if more (.getCdr more)) (do (.setCdr cur reversed) cur)))))
(defn list-eval [l]
(loop [cur (first l) more (next l) nodes nil]
(if-not cur
(list-reverse nodes)
(recur (first more) (next more) (new-node cur nodes)))))
(.getCar (list-reverse (list-eval '(1 2 3 4))))
(.. (list-eval '(1 2 3 4)) (valAt 0) getCar)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment