如何将Iterator转换为scalaz流?

时间:2015-10-19 06:29:56

标签: scala scalaz-stream

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

import scalaz.stream._

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

您将如何实施foo

1 个答案:

答案 0 :(得分: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
}

示例:

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