Skip to content

Commit a5cb207

Browse files
committed
added bounded-count
same Clojure 85a90b2e
1 parent 3cf2e51 commit a5cb207

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

src/main/cljs/cljs/core.cljs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3538,13 +3538,17 @@ reduces them without incurring seq initialization"
35383538
(aset a i init-val-or-seq))
35393539
a)))))
35403540

3541-
(defn- bounded-count [s n]
3542-
(if (counted? s)
3543-
(count s)
3544-
(loop [s s i n sum 0]
3545-
(if (and (pos? i) (seq s))
3546-
(recur (next s) (dec i) (inc sum))
3547-
sum))))
3541+
(defn bounded-count
3542+
"If coll is counted? returns its count, else will count at most the first n
3543+
elements of coll using its seq"
3544+
{:added "1.9"}
3545+
[n coll]
3546+
(if (counted? coll)
3547+
(count coll)
3548+
(loop [i 0 s (seq coll)]
3549+
(if (and (not (nil? s)) (< i n))
3550+
(recur (inc i) (next s))
3551+
i))))
35483552

35493553
(defn spread
35503554
[arglist]
@@ -3668,7 +3672,7 @@ reduces them without incurring seq initialization"
36683672
([f args]
36693673
(let [fixed-arity (.-cljs$lang$maxFixedArity f)]
36703674
(if (.-cljs$lang$applyTo f)
3671-
(let [bc (bounded-count args (inc fixed-arity))]
3675+
(let [bc (bounded-count (inc fixed-arity) args)]
36723676
(if (<= bc fixed-arity)
36733677
(apply-to f bc args)
36743678
(.cljs$lang$applyTo f args)))
@@ -3677,7 +3681,7 @@ reduces them without incurring seq initialization"
36773681
(let [arglist (list* x args)
36783682
fixed-arity (.-cljs$lang$maxFixedArity f)]
36793683
(if (.-cljs$lang$applyTo f)
3680-
(let [bc (bounded-count arglist (inc fixed-arity))]
3684+
(let [bc (bounded-count (inc fixed-arity) arglist)]
36813685
(if (<= bc fixed-arity)
36823686
(apply-to f bc arglist)
36833687
(.cljs$lang$applyTo f arglist)))
@@ -3686,7 +3690,7 @@ reduces them without incurring seq initialization"
36863690
(let [arglist (list* x y args)
36873691
fixed-arity (.-cljs$lang$maxFixedArity f)]
36883692
(if (.-cljs$lang$applyTo f)
3689-
(let [bc (bounded-count arglist (inc fixed-arity))]
3693+
(let [bc (bounded-count (inc fixed-arity) arglist)]
36903694
(if (<= bc fixed-arity)
36913695
(apply-to f bc arglist)
36923696
(.cljs$lang$applyTo f arglist)))
@@ -3695,7 +3699,7 @@ reduces them without incurring seq initialization"
36953699
(let [arglist (list* x y z args)
36963700
fixed-arity (.-cljs$lang$maxFixedArity f)]
36973701
(if (.-cljs$lang$applyTo f)
3698-
(let [bc (bounded-count arglist (inc fixed-arity))]
3702+
(let [bc (bounded-count (inc fixed-arity) arglist)]
36993703
(if (<= bc fixed-arity)
37003704
(apply-to f bc arglist)
37013705
(.cljs$lang$applyTo f arglist)))
@@ -3704,7 +3708,7 @@ reduces them without incurring seq initialization"
37043708
(let [arglist (cons a (cons b (cons c (cons d (spread args)))))
37053709
fixed-arity (.-cljs$lang$maxFixedArity f)]
37063710
(if (.-cljs$lang$applyTo f)
3707-
(let [bc (bounded-count arglist (inc fixed-arity))]
3711+
(let [bc (bounded-count (inc fixed-arity) arglist)]
37083712
(if (<= bc fixed-arity)
37093713
(apply-to f bc arglist)
37103714
(.cljs$lang$applyTo f arglist)))

0 commit comments

Comments
 (0)