Weird exception when using .view on list
Asked Answered
C

1

7

I had quite a big list of numbers. I needed to apply some operation on them, then take only those results that satisfy some condition. The list is sequential, so once I find the number that does not satisfy the condition, I can stop looking.

I wanted to avoid doing too much computation, so I moved on like in this example:

List(1,2,3,4,5).view.map(2 *).takeWhile(_ < 8)

But it gives me an exception:

java.lang.UnsupportedOperationException: SeqViewM(...).newBuilder
at scala.collection.TraversableViewLike$class.newBuilder(TraversableViewLike.scala:69)
at scala.collection.SeqViewLike$$anon$3.newBuilder(SeqViewLike.scala:77)
at scala.collection.IterableLike$class.takeWhile(IterableLike.scala:139)
at scala.collection.SeqViewLike$$anon$3.takeWhile(SeqViewLike.scala:77)
at scala.collection.SeqViewLike$$anon$3.takeWhile(SeqViewLike.scala:77)

Using Scala 2.9.0.1 (same behavior with 2.9.1). What is wrong here?

Chrysarobin answered 27/11, 2011 at 8:46 Comment(1)
Works just fine with 2.8Jojo
P
5

Looks like a bug. (File a bug report, if it's not already reported and/or fixed!)

In the meantime, you can use iterator as a workaround for this particular code:

List(1,2,3,4,5).iterator.map(2 *).takeWhile(8 >).toList

(drop the .toList if you're happy to end up with an iterator).

Proteose answered 27/11, 2011 at 9:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.