-
-
Save sudodoki/7141ce03f934a47fe13ff41df51387f2 to your computer and use it in GitHub Desktop.
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 dfs | |
[tree] | |
(if (nil? tree) | |
nil | |
(concat | |
(dfs (.l tree)) | |
(list (.val tree)) | |
(dfs (.r tree))))) | |
(defn leftmost-in | |
[tree] | |
(cond | |
(nil? tree) nil | |
(nil? (.l tree)) (.val tree) | |
:else (leftmost-in (.l tree)))) | |
(deftype TreeNode [val l r] | |
clojure.lang.ISeq | |
(first [this] (leftmost-in this)) | |
(next [this] (drop 1 (dfs this))) | |
(more [this] (drop 1 (dfs this))) | |
(cons [this v] | |
(cond | |
(nil? this) (TreeNode. v nil nil) | |
(< v (.val this)) (TreeNode. | |
(.val this), | |
(.-cons (.l this) v), | |
(.r this)) | |
:else (TreeNode. | |
(.val this), | |
(.l this), | |
(.-cons (.r this) v)))) | |
clojure.lang.Seqable | |
(seq [this] (dfs this))) | |
(def test-tree (conj) | |
(TreeNode. 5 | |
(TreeNode. 2 | |
(TreeNode. 1 nil nil) | |
(TreeNode. 3 nil nil)) | |
(TreeNode. 7 | |
(TreeNode. 6 nil nil) | |
(TreeNode. 8 nil nil)))) | |
(map println test-tree) | |
; 1 | |
; 2 | |
; 3 | |
; 5 | |
; 6 | |
; 7 | |
; 8 | |
; (nil nil nil nil nil nil nil) |
с дефтайпами значения всегда читаються из аргументов конструктора, методы протокола как бы замыкаються вокруг аргументов
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
🤔 how to make into/conj work with this?