File tree 1 file changed +8
-0
lines changed
src/main/scala/scala/collection/immutable/next
1 file changed +8
-0
lines changed Original file line number Diff line number Diff line change @@ -21,10 +21,18 @@ package object next {
21
21
* The result is a true cycle occupying only constant memory.
22
22
*/
23
23
def cycle : LazyList [T ] =
24
+ // case 1: the input is already known to be empty
25
+ // (the test can be changed to ll.knownIsEmpty when this code moves to stdlib)
24
26
if (ll.knownSize == 0 ) LazyList .empty
27
+ // we don't want to force the input's empty-or-not status until we must.
28
+ // `LazyList.empty #:::` accomplishes that delay
25
29
else LazyList .empty #::: {
30
+ // case 2: the input is later discovered to be empty
31
+ // (distinguishing this from case 3 isn't necessary for correctness, but we
32
+ // might as well avoid the lazy val path if we can)
26
33
if (ll.isEmpty) LazyList .empty
27
34
else {
35
+ // case 3: non-empty
28
36
lazy val result : LazyList [T ] = ll #::: result
29
37
result
30
38
}
You can’t perform that action at this time.
0 commit comments