问题
Consider the following:
Stream.continually(List(1,2,3)).transpose
You would expect to get something equivalent to:
Stream(
Stream.continually(1),
Stream.continually(2),
Stream.continually(3)
)
However, the upper construct loops infinitely. Is there a workaround for this? Is this to be considered a bug?
Question came up discussing this answer.
EDIT
I know that a function like this writable, see the following example. But is there something in the library?
def myTranspose[A,B](in: Stream[A])
(implicit asTraversable: A => GenTraversableOnce[B]): Stream[Stream[B]] = {
val len = asTraversable(in.head).size
for (i <- (0 until len).toStream)
yield in.map(el => asTraversable(el).toIndexedSeq(i))
}
来源:https://stackoverflow.com/questions/17116061/transpose-on-infinite-stream-loops-forever