Created
April 29, 2011 10:22
-
-
Save jhannes/948150 to your computer and use it in GitHub Desktop.
Tree pretty printer 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 print-subtree [subtree prefix] | |
(if (empty? subtree) | |
"" | |
(apply str prefix (first subtree) "\n" | |
(map #(print-subtree %1 (str "\t" prefix)) (rest subtree))))) | |
(defn print-tree [tree] | |
(print-subtree tree "")) | |
(deftest print-empty-tree | |
(is (= (print-tree | |
'("parent" | |
("child1") | |
("child2" | |
("grand1") | |
("grand2")))) | |
"parent\n\tchild1\n\tchild2\n\t\tgrand1\n\t\tgrand2\n"))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ganske lik mitt forsøk på å skrive lisp i Java, men det ser litt mer elegant ut :-)
https://github.com/fatso83/Code-Snippets/blob/master/Functional%20Dojo/src/no/kopseng/dojo/functional/Node.java (tabbedString3 versjonen)