Skip to content

Instantly share code, notes, and snippets.

@rik0
Created February 26, 2011 10:42
Show Gist options
  • Save rik0/845098 to your computer and use it in GitHub Desktop.
Save rik0/845098 to your computer and use it in GitHub Desktop.
Javish Iterator in Clojure
(defn make-iterator [tree]
(let [stack (ref [tree])]
(proxy [Iterator] []
(next []
(if (seq @stack)
(let [{:keys [left right value]} (first @stack)]
(dosync
(alter stack
#(concat (keep identity [left right])
(rest %)))
value))
(throw (NoSuchElementException.))))
(hasNext [] (not (empty? @stack))))))
(def exp {:left {:left {:value 3}
:right {:right {:value 4} :value 1}
:value 6}
:right {:left {:value 7} :value 8}
:value 0})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment