Transpose on infinite stream loops forever

假装没事ソ 提交于 2019-12-22 08:26:44

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!