Skip to content

Commit

Permalink
CLJS-255: fix counting LazySeq
Browse files Browse the repository at this point in the history
(count (lazy-seq nil)) now correctly evaluates to 0.
  • Loading branch information
michalmarczyk authored and David Nolen committed May 15, 2012
1 parent e67af95 commit 83f9d91
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/cljs/cljs/core.cljs
Expand Up @@ -606,18 +606,19 @@ reduces them without incurring seq initialization"

(declare counted?)

(defn- accumulating-seq-count [coll acc]
(if (counted? coll) ; assumes nil is counted, which it currently is
(+ acc (-count coll))
(recur (next coll) (inc acc))))
(defn- accumulating-seq-count [coll]
(loop [s (seq coll) acc 0]
(if (counted? s) ; assumes nil is counted, which it currently is
(+ acc (-count s))
(recur (next s) (inc acc)))))

(defn count
"Returns the number of items in the collection. (count nil) returns
0. Also works on strings, arrays, and Maps"
[coll]
(if (counted? coll)
(-count coll)
(accumulating-seq-count coll 0)))
(accumulating-seq-count coll)))

(declare indexed?)

Expand Down

0 comments on commit 83f9d91

Please sign in to comment.