Skip to content

Instantly share code, notes, and snippets.

@neotyk
Created January 10, 2010 19:38
Show Gist options
  • Save neotyk/273716 to your computer and use it in GitHub Desktop.
Save neotyk/273716 to your computer and use it in GitHub Desktop.
clojure.core/str version that shows how it works
(defn mystr
"With no args, returns the empty string. With one arg x, returns
x.toString(). (str nil) returns the empty string. With more than
one arg, returns the concatenation of the str values of the args."
{:tag String}
([] (println "0:")
"")
([#^Object x] (println "1: x:" x)
(if (nil? x) "" (. x (toString))))
([x & ys] (println "2: x: " x ", ys: " ys)
((fn [#^StringBuilder sb more]
(if more
(do (println "2i: sb: " sb ", more: " more)
(recur (. sb (append (mystr (first more)))) (next more)))
(do (println "2i: sb: " sb)
(mystr sb))))
(new StringBuilder #^String (mystr x)) ys)))
user> (mystr)
0:
""
user> (mystr \a)
1: x: a
"a"
user> (mystr \a \b)
2: x: a , ys: (b)
1: x: a
2i: sb: #<StringBuilder a> , more: (b)
1: x: b
2i: sb: #<StringBuilder ab>
1: x: #<StringBuilder ab>
"ab"
user> (mystr \a \b \c)
2: x: a , ys: (b c)
1: x: a
2i: sb: #<StringBuilder a> , more: (b c)
1: x: b
2i: sb: #<StringBuilder ab> , more: (c)
1: x: c
2i: sb: #<StringBuilder abc>
1: x: #<StringBuilder abc>
"abc"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment