7

Suppose I've got an Iterator[A]. I would like to convert it to Process[Nothing, A] of scalaz stream.

import scalaz.stream._

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

How would you implement foo ?

4

1 回答 1

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)
于 2015-10-19T10:12:27.623 回答