Skip to content

Commit

Permalink
internal-reduce implementation for String
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera authored and mikera committed Dec 2, 2011
1 parent 7ab01ab commit 9866d59
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/clj/clojure/core/protocols.clj
Expand Up @@ -71,6 +71,15 @@
(internal-reduce s f val))
val)))

java.lang.String
(internal-reduce
[s f val]
(loop [i 0
val val]
(if (< i (.length s))
(recur (inc i) (f val (.charAt s i)))
val)))

java.lang.Object
(internal-reduce
[s f val]
Expand Down
7 changes: 5 additions & 2 deletions test/clojure/test_clojure/reduce.clj
Expand Up @@ -27,7 +27,7 @@
(is (= 1 (reduce multiply 1 (char-array 0)))))

(deftest test-map-reductions
;reduce over a large map
;reduce over a 100-element map
(let [ms (zipmap (range 100) (range 100))]
(is (= 4950 (reduce (fn [acc [k v]] (+ acc v)) 0 ms)))))

Expand All @@ -47,4 +47,7 @@
; reduce on two-element collections applies function once
(is (= 6 (reduce multiply [2 3])))
(is (= 6 (reduce multiply '(2 3))))
(is (= 6 (reduce multiply #{2 3}))))
(is (= 6 (reduce multiply #{2 3}))))

(deftest test-string-reductions
(is (= "Hello World" (reduce str "" "Hello World"))))

0 comments on commit 9866d59

Please sign in to comment.