Skip to content

Commit

Permalink
make join faster #359
Browse files Browse the repository at this point in the history
Signed-off-by: Stuart Halloway <stu@thinkrelevance.com>
  • Loading branch information
David Liebke and Stuart Halloway authored and stuarthalloway committed Jun 4, 2010
1 parent c426505 commit 4997e80
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
13 changes: 11 additions & 2 deletions src/clj/clojure/string.clj
Expand Up @@ -70,8 +70,17 @@
(defn ^String join
"Returns a string of all elements in coll, separated by
separator. Like Perl's join."
[^String separator coll]
(apply str (interpose separator coll)))
([coll]
(apply str coll))
([separator [x & more]]
(loop [sb (StringBuilder. (str x))
more more
sep (str separator)]
(if more
(recur (-> sb (.append sep) (.append (str (first more))))
(next more)
sep)
(str sb)))))

(defn ^String chop
"Removes the last character of string, does nothing on a zero-length
Expand Down
13 changes: 10 additions & 3 deletions test/clojure/test_clojure/string.clj
Expand Up @@ -15,9 +15,16 @@
(is (= "FOObarfoo" (s/replace-first-by #"foo" s/upper-case "foobarfoo"))))

(deftest t-join
(is (= "1,2,3" (s/join \, [1 2 3])))
(is (= "" (s/join \, [])))
(is (= "1 and-a 2 and-a 3" (s/join " and-a " [1 2 3]))))
(are [x coll] (= x (s/join coll))
"" nil
"" []
"1" [1]
"12" [1 2])
(are [x sep coll] (= x (s/join sep coll))
"1,2,3" \, [1 2 3]
"" \, []
"1" \, [1]
"1 and-a 2 and-a 3" " and-a " [1 2 3]))

(deftest t-chop
(is (= "fo" (s/chop "foo")))
Expand Down

0 comments on commit 4997e80

Please sign in to comment.