Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make LazyList's empty-or-not status lazy too #7000

Merged
merged 2 commits into from
Aug 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions src/library/scala/collection/LinearSeq.scala
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,6 @@ trait LinearSeqOps[+A, +CC[X] <: LinearSeq[X], +C <: LinearSeq[A] with LinearSeq

override def isDefinedAt(x: Int): Boolean = x >= 0 && lengthCompare(x) > 0

// Optimized version of `drop` that avoids copying
override def drop(n: Int): C = {
@tailrec def loop(n: Int, s: C): C =
if (n <= 0 || s.isEmpty) s
else loop(n - 1, s.tail)
loop(n, coll)
}

override def dropWhile(p: A => Boolean): C = {
@tailrec def loop(s: C): C =
if (s.nonEmpty && p(s.head)) loop(s.tail)
else s
loop(coll)
}

// `apply` is defined in terms of `drop`, which is in turn defined in
// terms of `tail`.
@throws[IndexOutOfBoundsException]
Expand Down Expand Up @@ -184,6 +169,21 @@ trait StrictOptimizedLinearSeqOps[+A, +CC[X] <: LinearSeq[X], +C <: LinearSeq[A]
def hasNext = !current.isEmpty
def next() = { val r = current.head; current = current.tail; r }
}

// Optimized version of `drop` that avoids copying
override def drop(n: Int): C = {
@tailrec def loop(n: Int, s: C): C =
if (n <= 0 || s.isEmpty) s
else loop(n - 1, s.tail)
loop(n, coll)
}

override def dropWhile(p: A => Boolean): C = {
@tailrec def loop(s: C): C =
if (s.nonEmpty && p(s.head)) loop(s.tail)
else s
loop(coll)
}
}

/** A specialized Iterator for LinearSeqs that is lazy enough for Stream and LazyList. This is accomplished by not
Expand Down
Loading