The problem is a delayed estimate from Stream/ Iterable. It is very easy to see if you decompose it into smaller pieces.
scala> Stream("A", "").toIterable.map(_.head)
res7: Iterable[Char] = Stream(A, ?)
Stream _.head , , head, . , Iterable , Stream, . . , :
scala> Stream("A", "").toIterable.map(_.head)
java.util.NoSuchElementException: next on empty iterator
, "A" "", , Stream head, .
scala> Stream("", "").toIterable.map(_.head).toList
java.util.NoSuchElementException: next on empty iterator
, , - head Stream Iterable . , map , flatMap ? , map Stream:
scala> List(1).map { _ => Stream("A", "").toIterable.map(_.head) }
res11: List[Iterable[Char]] = List(Stream(A, ?))
, , . flatMap Iterable , , . :
scala> List(1).flatMap { _ => Stream("A", "").toIterable.map(_.head) }
java.util.NoSuchElementException: next on empty iterator