-
-
Save teigen/948337 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
scala: | |
case class Node(value:String, children:Node*) | |
def pretty(node:Node, i:String = ""):String = | |
node.children.foldLeft(i + node.value){ _ + "\n" + pretty(_:Node, i + "\t") } | |
println(pretty( | |
Node("parent", | |
Node("child1"), | |
Node("child2", | |
Node("grand1"), | |
Node("grand2"))))) | |
clojure: | |
(defn pretty | |
([tree] | |
(pretty tree "")) | |
([[x & xs] i] | |
(reduce #(str %1 "\n" (pretty %2 (str "\t" i))) (str i x) xs))) | |
(println (pretty | |
["parent" | |
["child1"] | |
["child2" | |
["grand1"] | |
["grand2"]]])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice. Clojure has 253 characters with 38 of them being [] or ()