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

Bug in Iterator.span #9332

Closed
scabug opened this issue May 27, 2015 · 3 comments
Closed

Bug in Iterator.span #9332

scabug opened this issue May 27, 2015 · 3 comments

Comments

@scabug
Copy link

scabug commented May 27, 2015

Example code:

val (x, y) = Iterator.iterate(0)(_ + 1).take(6).span(_ != 1)
println(x.next(), x.hasNext, y.next(), x.hasNext, x.toVector)

Expected result:

(0,false,1,false,Vector())

Actual result:

(0,false,1,true,Vector(2, 3, 4, 5))

The problem is in the implementation of the Leading class in Iterator.span, which doesn't remember that the test condition (p(self.head) == false) has already been reached.

A solution is to add a class variable var finished = false, which is set when the condition fails in advance():

def advance() = {
  !finished && self.hasNext && (if (p(self.head)) {
      lookahead += self.next
      true
    } else {
      finished = true
      false
    })
}

The implementation remains non-thread-safe.

@scabug
Copy link
Author

scabug commented May 27, 2015

Imported From: https://issues.scala-lang.org/browse/SI-9332?orig=1
Reporter: Tom Primozic (tomp)
Affected Versions: 2.11.6

@scabug
Copy link
Author

scabug commented May 27, 2015

@adriaanm said:
Ouch! Thank you for reporting, diagnosing and suggesting a fix! Would you like to take the logical step.next() and submit a PR?

@scabug
Copy link
Author

scabug commented May 27, 2015

@som-snytt said:
Implemented as suggested: scala/scala#4530

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants