如何使用Flatmap为Stream实现Flatten?功能是否保留输入流的内容,但将其结构简化为单个流?
def flatten(ffa: Stream[Stream[A]]):
Stream[A]=
答案 0 :(得分:2)
您可以尝试:
def flatten[A](ffa: Stream[Stream[A]]): Stream[A] = ffa.flatMap(identity)
>flatten(Stream(Stream(1,2,3), Stream(4,5,6), Stream(7,8,9)))
// res1: Stream[Int] = Stream(1, 2, 3, 4, 5, 6, 7, 8, 9)