Skip to content

Commit

Permalink
Support terminating reduce
Browse files Browse the repository at this point in the history
  • Loading branch information
michalmarczyk authored and David Nolen committed May 1, 2012
1 parent b97dd87 commit a8ac1f4
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions src/cljs/cljs/core.cljs
Expand Up @@ -372,6 +372,8 @@
"Returns a number one greater than num." "Returns a number one greater than num."
[x] (cljs.core/+ x 1)) [x] (cljs.core/+ x 1))


(declare reduced? deref)

(defn- ci-reduce (defn- ci-reduce
"Accepts any collection which satisfies the ICount and IIndexed protocols and "Accepts any collection which satisfies the ICount and IIndexed protocols and
reduces them without incurring seq initialization" reduces them without incurring seq initialization"
Expand All @@ -380,17 +382,26 @@ reduces them without incurring seq initialization"
(f) (f)
(loop [val (-nth cicoll 0), n 1] (loop [val (-nth cicoll 0), n 1]
(if (< n (-count cicoll)) (if (< n (-count cicoll))
(recur (f val (-nth cicoll n)) (inc n)) (let [nval (f val (-nth cicoll n))]
(if (reduced? nval)
@nval
(recur nval (inc n))))
val)))) val))))
([cicoll f val] ([cicoll f val]
(loop [val val, n 0] (loop [val val, n 0]
(if (< n (-count cicoll)) (if (< n (-count cicoll))
(recur (f val (-nth cicoll n)) (inc n)) (let [nval (f val (-nth cicoll n))]
(if (reduced? nval)
@nval
(recur nval (inc n))))
val))) val)))
([cicoll f val idx] ([cicoll f val idx]
(loop [val val, n idx] (loop [val val, n idx]
(if (< n (-count cicoll)) (if (< n (-count cicoll))
(recur (f val (-nth cicoll n)) (inc n)) (let [nval (f val (-nth cicoll n))]
(if (reduced? nval)
@nval
(recur nval (inc n))))
val)))) val))))


(declare hash-coll cons pr-str) (declare hash-coll cons pr-str)
Expand Down Expand Up @@ -904,7 +915,10 @@ reduces them without incurring seq initialization"
([f val coll] ([f val coll]
(loop [val val, coll (seq coll)] (loop [val val, coll (seq coll)]
(if coll (if coll
(recur (f val (first coll)) (next coll)) (let [nval (f val (first coll))]
(if (reduced? nval)
@nval
(recur nval (next coll))))
val)))) val))))


(extend-type default (extend-type default
Expand Down Expand Up @@ -4489,10 +4503,10 @@ reduces them without incurring seq initialization"


IReduce IReduce
(-reduce [node f] (-reduce [node f]
(f key val)) (ci-reduce node f))


(-reduce [node f start] (-reduce [node f start]
(f (f start key))) (ci-reduce node f start))


IFn IFn
(-invoke [node k] (-invoke [node k]
Expand Down Expand Up @@ -4636,10 +4650,10 @@ reduces them without incurring seq initialization"


IReduce IReduce
(-reduce [node f] (-reduce [node f]
(f key val)) (ci-reduce node f))


(-reduce [node f start] (-reduce [node f start]
(f (f start key))) (ci-reduce node f start))


IFn IFn
(-invoke [node k] (-invoke [node k]
Expand Down

0 comments on commit a8ac1f4

Please sign in to comment.