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

Stream#permutations could produce incorrect value of Iterator[Stream] #5377

Closed
scabug opened this issue Jan 14, 2012 · 3 comments
Closed

Stream#permutations could produce incorrect value of Iterator[Stream] #5377

scabug opened this issue Jan 14, 2012 · 3 comments
Assignees
Milestone

Comments

@scabug
Copy link

scabug commented Jan 14, 2012

I wrote following program to check behavior of Stream#permutations() and it compiled with scalac.

{code:title=StreamPermutationBug.scala|borderStyle=solid}
object StreamPermutationBug {
def main(args: Array[String]) {
val list = List(1, 2)
println("from: " + list)

// expected result
list.permutations.map(_.toList).foreach(println)

println("---")

println("from: " + list.toStream)
// unexpected result
list.toStream.permutations.map(_.toList).foreach(println)

println("---")

val range = Range(1, 3)
println("from: " + range) 
// unexpected result
range.toStream.permutations.map(_.toList).foreach(println) 

}
}
{code}

However, the output of this program is unexpected:

{panel:title=Expected Result| borderStyle=dashed| borderColor=#ccc| titleBGColor=#F7D6C1| bgColor=#FFFFCE}
from: List(1, 2)
List(1, 2)
List(2, 1)

from: Stream(1, ?)
List(1, 2)
List(2, 1)

from: Range(1, 2)
List(1, 2)
List(2, 1)
{panel}

{panel:title=Actual Result(unexpected)| borderStyle=dashed| borderColor=#ccc| titleBGColor=#F7D6C1| bgColor=#FFFFCE}
from: List(1, 2)
List(1, 2) // expected
List(2, 1)

from: Stream(1, ?)
List(1, 1) // unexpected!
List(2, 1)

from: Range(1, 2)
List(1, 1) // unexpected!
List(2, 1)
{panel}

It seems that application of "permutations" method to Stream instances could produce incorrect value of Iterator of Stream.
This problem is reproducable in REPL, too.

@scabug
Copy link
Author

scabug commented Jan 14, 2012

Imported From: https://issues.scala-lang.org/browse/SI-5377?orig=1
Reporter: @kmizu
Affected Versions: 2.9.1

@scabug
Copy link
Author

scabug commented Jan 24, 2012

@axel22 said:
Another stream-related source of fun - the permutations iterator constructs the collection by adding the current elements of the buffer into a builder. If the builder is lazy, it will add the elements from the buffer into the collection only after the elements in the buffer get swapped around.

@scabug scabug closed this as completed Jan 24, 2012
@scabug
Copy link
Author

scabug commented Jan 26, 2012

@kmizu said:
Note that the fix on this issue (I wrote) was marged into master branch already:

commit 26afbf85bf17e3839f03cb9e2d981cdccdd2ee69
Author: Kota Mizushima mizukota@gmail.com
Date: Mon Jan 16 23:20:07 2012 +0900

Fixed a bug SI-5377 (https://issues.scala-lang.org/browse/SI-5377).

Added test cases for SI-5377. 

@scabug scabug added this to the 2.10.0 milestone Apr 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants