How to convert Iterator to scalaz stream?
Asked Answered
C

1

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 ?

Cuttler answered 19/10, 2015 at 6:29 Comment(0)
L
8

I think that you can do it using 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
}

Example:

scala> foo(List(1,2,3,4,5).iterator).toList
res0: List[Int] = List(1,2,3,4,5)
Laceration answered 19/10, 2015 at 10:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.