File tree Expand file tree Collapse file tree 1 file changed +31
-2
lines changed Expand file tree Collapse file tree 1 file changed +31
-2
lines changed Original file line number Diff line number Diff line change @@ -2373,6 +2373,29 @@ reduces them without incurring seq initialization"
2373
2373
(garray/shuffle a)
2374
2374
(vec a)))
2375
2375
2376
+ (defn- iter-reduce
2377
+ ([coll f]
2378
+ (let [iter (-iterator coll)]
2379
+ (if (.hasNext iter)
2380
+ (let [init (.next iter)]
2381
+ (loop [acc init]
2382
+ (if (.hasNext iter)
2383
+ (let [nacc (f acc (.next iter))]
2384
+ (if (reduced? nacc)
2385
+ @nacc
2386
+ (recur nacc)))
2387
+ acc)))
2388
+ (f ))))
2389
+ ([coll f init]
2390
+ (let [iter (-iterator coll)]
2391
+ (loop [acc init]
2392
+ (if (.hasNext iter)
2393
+ (let [nacc (f acc (.next iter))]
2394
+ (if (reduced? nacc)
2395
+ @nacc
2396
+ (recur nacc)))
2397
+ acc)))))
2398
+
2376
2399
(defn reduce
2377
2400
" f should be a function of 2 arguments. If val is not supplied,
2378
2401
returns the result of applying f to the first 2 items in coll, then
@@ -2397,6 +2420,9 @@ reduces them without incurring seq initialization"
2397
2420
(native-satisfies? IReduce coll)
2398
2421
(-reduce coll f)
2399
2422
2423
+ (iterable? coll)
2424
+ (iter-reduce coll f)
2425
+
2400
2426
:else
2401
2427
(seq-reduce f coll)))
2402
2428
([f val coll]
@@ -2413,6 +2439,9 @@ reduces them without incurring seq initialization"
2413
2439
(native-satisfies? IReduce coll)
2414
2440
(-reduce coll f val)
2415
2441
2442
+ (iterable? coll)
2443
+ (iter-reduce coll f val)
2444
+
2416
2445
:else
2417
2446
(seq-reduce f val coll))))
2418
2447
@@ -6505,9 +6534,9 @@ reduces them without incurring seq initialization"
6505
6534
6506
6535
IReduce
6507
6536
(-reduce [coll f]
6508
- (seq -reduce f coll))
6537
+ (iter -reduce coll f ))
6509
6538
(-reduce [coll f start]
6510
- (seq -reduce f start coll ))
6539
+ (iter -reduce coll f start))
6511
6540
6512
6541
IFn
6513
6542
(-invoke [coll k]
You can’t perform that action at this time.
0 commit comments