Mic*_*ael 7 scala scalaz-stream
假设我有一个Iterator[A]
.我想将其转换Process[Nothing, A]
为scalaz流.
import scalaz.stream._
def foo[A](it: Iterator[A]): Process[Nothing, A] = ???
Run Code Online (Sandbox Code Playgroud)
你会如何实施foo
?
我认为你可以使用unfold
:
import scalaz.stream._
def foo[A](it: Iterator[A]): Process[Nothing, A] = Process.unfold(it) { it =>
if (it.hasNext) Some((it.next, it))
else None
}
Run Code Online (Sandbox Code Playgroud)
例:
scala> foo(List(1,2,3,4,5).iterator).toList
res0: List[Int] = List(1,2,3,4,5)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
337 次 |
最近记录: |