Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

make join faster #359

Signed-off-by: Stuart Halloway <stu@thinkrelevance.com>
  • Loading branch information...
commit 4997e807b5962443eb2c41463cb1432b7cc5f613 1 parent c426505
David Liebke and Stuart Halloway authored stuarthalloway committed
Showing with 21 additions and 5 deletions.
  1. +11 −2 src/clj/clojure/string.clj
  2. +10 −3 test/clojure/test_clojure/string.clj
View
13 src/clj/clojure/string.clj
@@ -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
View
13 test/clojure/test_clojure/string.clj
@@ -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")))
Please sign in to comment.
Something went wrong with that request. Please try again.