Scala Programming Language
  1. Scala Programming Language
  2. SI-6271

View issue in M7 - NoSuchElementException thrown from iterator.toIterable.flatten (via flatMaps on views)

    Details

      Description

      affects 2.10.0 M7.

      Reproducable via the repl (as long as -optimise isn't on of course).

      val viewed : Iterable[Iterable[Iterable[Int]]] =
      List(List(List(0).view).view).view
      
      val filtered = viewed.flatMap{ x => x.flatMap( y => List( y.filter{ _
      > 0 } ) ) }
      
      filtered.iterator.toIterable.flatten
      

      will throw the following exception and stack trace:

      NoSuchElementException("next on empty iterator") from
      at scala.collection.Iterator$$anon$2.next(Iterator.scala:38)
      at scala.collection.Iterator$$anon$2.next(Iterator.scala:36)
      at scala.collection.Iterator$$anon$14.next(Iterator.scala:394)
      at scala.collection.IterableLike$class.head(IterableLike.scala:91)
      at scala.collection.SeqViewLike$AbstractTransformed.head(SeqViewLike.scala:43)
      at scala.collection.immutable.Stream.scala$collection$immutable$Stream$$flatten1$1(Stream.scala:909)
      at scala.collection.immutable.Stream.flatten(Stream.scala:914)
      at scala.collection.immutable.Stream.flatten(Stream.scala:185)
      

      Drop the innermost view and it won't and you'll get the
      correct Stream() as the result. Using Nil.view will work, using
      List(1 ...) will work, anything as long as the first predicate to
      Iterator.filter works.

        Issue Links

          Activity

          Hide
          Chris added a comment -

          Jason investigated suggesting https://github.com/scala/scala/commit/caf7eb6b is the culprit.

          The code works as expected in M6 (and below to 2.8.1).

          Show
          Chris added a comment - Jason investigated suggesting https://github.com/scala/scala/commit/caf7eb6b is the culprit. The code works as expected in M6 (and below to 2.8.1).
          Hide
          Josh Suereth added a comment -

          Reduced test case:

          List(0).view.flatMap(_ => List(List(1).view filter (_ < 1))).iterator.toIterable.flatten
          
          Show
          Josh Suereth added a comment - Reduced test case: List(0).view.flatMap(_ => List(List(1).view filter (_ < 1))).iterator.toIterable.flatten
          Show
          Josh Suereth added a comment - https://github.com/scala/scala/pull/1262

            People

            • Assignee:
              Josh Suereth
              Reporter:
              Chris
            • Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Development