Created
February 26, 2011 10:42
-
-
Save rik0/845098 to your computer and use it in GitHub Desktop.
Javish Iterator in Clojure
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
(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