如何将 Iterator 转换为 scalaz 流?

How to convert Iterator to scalaz stream?

假设我有一个 Iterator[A]。我想将其转换为 scalaz streamProcess[Nothing, A]

import scalaz.stream._

def foo[A](it: Iterator[A]): Process[Nothing, A] = ???

您将如何实施 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
}

示例:

scala> foo(List(1,2,3,4,5).iterator).toList
res0: List[Int] = List(1,2,3,4,5)