Created
January 10, 2010 19:38
-
-
Save neotyk/273716 to your computer and use it in GitHub Desktop.
clojure.core/str version that shows how it works
This file contains hidden or 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 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))) |
This file contains hidden or 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
user> (mystr) | |
0: | |
"" |
This file contains hidden or 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
user> (mystr \a) | |
1: x: a | |
"a" |
This file contains hidden or 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
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" |
This file contains hidden or 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
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