Skip to content

Instantly share code, notes, and snippets.

@teigen
Created April 29, 2011 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save teigen/948337 to your computer and use it in GitHub Desktop.
Save teigen/948337 to your computer and use it in GitHub Desktop.
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"]]]))
@jhannes
Copy link

jhannes commented Apr 29, 2011

Nice. Clojure has 253 characters with 38 of them being [] or ()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment