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
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,8 @@
"Returns a number one greater than num."
[x] (cljs.core/+ x 1))

(declare reduced? deref)

(defn- ci-reduce
"Accepts any collection which satisfies the ICount and IIndexed protocols and
reduces them without incurring seq initialization"
Expand All @@ -380,17 +382,26 @@ reduces them without incurring seq initialization"
(f)
(loop [val (-nth cicoll 0), n 1]
(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))))
([cicoll f val]
(loop [val val, n 0]
(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)))
([cicoll f val idx]
(loop [val val, n idx]
(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))))

(declare hash-coll cons pr-str)
Expand Down Expand Up @@ -904,7 +915,10 @@ reduces them without incurring seq initialization"
([f val coll]
(loop [val val, coll (seq 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))))

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

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

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

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

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

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

IFn
(-invoke [node k]
Expand Down

0 comments on commit a8ac1f4

Please sign in to comment.