Skip to content

List.separate results in StackOverflowError #1437

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

Closed
scabug opened this issue Oct 20, 2008 · 4 comments
Closed

List.separate results in StackOverflowError #1437

scabug opened this issue Oct 20, 2008 · 4 comments

Comments

@scabug
Copy link

scabug commented Oct 20, 2008

scala> List.separate(List.range(1,100000).map(Left(_)))
java.lang.StackOverflowError
        at scala.List.foldRight(List.scala:1067)
        at scala.List.foldRight(List.scala:1067)
        at scala.List.foldRight(List.scala:1067)
        at scala.List.foldRight(List.scala:1067)
        at scala.List.foldRight(List.scala:1067)
        at scala.List.foldRight(List.scala:1067)
        at scala.List.foldRight(List.scala:1067)
        at scala.List.foldRight(List.scala:1067)
        at scala.List.foldRig...

Why is List.separate using foldRight? (Or maybe the question is: why is foldRight implemented using recursion that can lead to stack overflows?) It should probably use the same logic as Iterable.partition. Here's an implementation that would work:

def separate[A,B](l: List[Either[A,B]]) = {
  val (lefts, rights) = l.partition(_.isLeft)
  (lefts.map(_.left.get), rights.map(_.right.get))
}
@scabug
Copy link
Author

scabug commented Oct 20, 2008

Imported From: https://issues.scala-lang.org/browse/SI-1437?orig=1
Reporter: @pchiusano
Assignee: @tonymorris

@scabug
Copy link
Author

scabug commented Oct 21, 2008

@odersky said:
Tony, one other thing: In 2.8.0 lefts/rights/separate should be moved from
object List to object Either. More precisely, we are deprecating them in List, and will remove them afterwards. If you do the fix, can you already add the fixed versions to Either? Thanks! -- Martin

@scabug
Copy link
Author

scabug commented Oct 22, 2008

@tonymorris said:
No problem, what time frame do we have?

@scabug
Copy link
Author

scabug commented Nov 18, 2009

@paulp said:
Ticket survived long enough for the method in question to transition through the whole deprecation period and reach its EOL.

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

1 participant