Skip to content

Commit a91249e

Browse files
committed
[lazy] moving off of lazy-cons, lazy-seq calls seq on body
1 parent 688633e commit a91249e

File tree

2 files changed

+26
-28
lines changed

2 files changed

+26
-28
lines changed

src/clj/clojure/core.clj

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -418,20 +418,20 @@
418418

419419
(defn concat
420420
"Returns a lazy sequence representing the concatenation of the elements in the supplied colls."
421-
([] nil)
422-
([x] (seq x))
421+
([] (lazy-seq nil))
422+
([x] (lazy-seq x))
423423
([x y]
424424
(lazy-seq
425425
(if (seq x)
426426
(cons (first x) (concat (more x) y))
427-
(seq y))))
427+
y)))
428428
([x y & zs]
429429
(let [cat (fn cat [xys zs]
430430
(lazy-seq
431431
(if (seq xys)
432432
(cons (first xys) (cat (more xys) zs))
433433
(when zs
434-
(seq (cat (first zs) (rest zs)))))))]
434+
(cat (first zs) (rest zs))))))]
435435
(cat (concat x y) zs))))
436436

437437
;;;;;;;;;;;;;;;;at this point all the support for syntax-quote exists;;;;;;;;;;;;;;;;;;;;;;
@@ -1507,15 +1507,8 @@
15071507
(lazy-seq (step pred coll))))
15081508

15091509
(defn cycle
1510-
"Returns a lazy (infinite!) seq of repetitions of the items in
1511-
coll."
1512-
[coll]
1513-
(when (seq coll)
1514-
(let [rep (fn thisfn [xs]
1515-
(if xs
1516-
(lazy-cons (first xs) (thisfn (rest xs)))
1517-
(recur (seq coll))))]
1518-
(rep (seq coll)))))
1510+
"Returns a lazy (infinite!) sequence of repetitions of the items in coll."
1511+
[coll] (lazy-seq (concat coll (cycle coll))))
15191512

15201513
(defn split-at
15211514
"Returns a vector of [(take n coll) (drop n coll)]"
@@ -1528,16 +1521,17 @@
15281521
[(take-while pred coll) (drop-while pred coll)])
15291522

15301523
(defn repeat
1531-
"Returns a lazy (infinite!) seq of xs."
1532-
[x] (lazy-cons x (repeat x)))
1524+
"Returns a lazy (infinite!, or length n if supplied) sequence of xs."
1525+
([x] (lazy-seq (cons x (repeat x))))
1526+
([n x] (take n (repeat x))))
15331527

15341528
(defn replicate
15351529
"Returns a lazy seq of n xs."
15361530
[n x] (take n (repeat x)))
15371531

15381532
(defn iterate
1539-
"Returns a lazy seq of x, (f x), (f (f x)) etc. f must be free of side-effects"
1540-
[f x] (lazy-cons x (iterate f (f x))))
1533+
"Returns a lazy sequence of x, (f x), (f (f x)) etc. f must be free of side-effects"
1534+
[f x] (cons x (lazy-seq (iterate f (f x)))))
15411535

15421536
(defn range
15431537
"Returns a lazy seq of nums from start (inclusive) to end
@@ -1596,8 +1590,9 @@
15961590
rdr must implement java.io.BufferedReader."
15971591
[#^java.io.BufferedReader rdr]
15981592
(let [line (. rdr (readLine))]
1599-
(when line
1600-
(lazy-cons line (line-seq rdr)))))
1593+
(lazy-seq
1594+
(when line
1595+
(cons line (line-seq rdr))))))
16011596

16021597
(defn comparator
16031598
"Returns an implementation of java.util.Comparator based upon pred."
@@ -1634,10 +1629,11 @@
16341629
([n coll]
16351630
(partition n n coll))
16361631
([n step coll]
1637-
(when (seq coll)
1638-
(let [p (take n coll)]
1639-
(when (= n (count p))
1640-
(lazy-cons p (partition n step (drop step coll))))))))
1632+
(lazy-seq
1633+
(when (seq coll)
1634+
(let [p (take n coll)]
1635+
(when (= n (count p))
1636+
(cons p (partition n step (drop step coll)))))))))
16411637

16421638
;; evaluation
16431639

@@ -2237,8 +2233,9 @@
22372233
row-struct (apply create-struct keys)
22382234
row-values (fn [] (map (fn [#^Integer i] (. rs (getObject i))) idxs))
22392235
rows (fn thisfn []
2240-
(when (. rs (next))
2241-
(lazy-cons (apply struct row-struct (row-values)) (thisfn))))]
2236+
(lazy-seq
2237+
(when (. rs (next))
2238+
(cons (apply struct row-struct (row-values)) (thisfn)))))]
22422239
(rows)))
22432240

22442241
(defn set
@@ -2381,8 +2378,9 @@
23812378
(defn take-nth
23822379
"Returns a lazy seq of every nth item in coll."
23832380
[n coll]
2384-
(when (seq coll)
2385-
(lazy-cons (first coll) (take-nth n (drop n coll)))))
2381+
(lazy-seq
2382+
(when (seq coll)
2383+
(cons (first coll) (take-nth n (drop n coll))))))
23862384

23872385
(defn interleave
23882386
"Returns a lazy seq of the first item in each coll, then the second

src/jvm/clojure/lang/LazySeq.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ final synchronized public ISeq seq() {
2424
{
2525
try
2626
{
27-
s = (ISeq) invoke();
27+
s = RT.seq(invoke());
2828
}
2929
catch (Exception e)
3030
{

0 commit comments

Comments
 (0)