如何将Iterator转换为scalaz流?

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

Ald*_*nio 8

我认为你可以使用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)