Search code examples
scalazscalaz-stream

Apply a Channel N times from an initial value


I have a function f and a channel c

def f(i: Int) = Task.now(i + 1)

val c = channel.lift(f)

I would like to continuously apply the function f an arbitrary number of times (or indefinitely) to the output of the previous computation. I'm providing the initial value.

I can define a process p

val p = Process.emit(1).through(c)

but this only gets executed once.

How can I keep applying c to the output of the last computation ?


Solution

  • I can use the iterateEval[F[_], A](start: A)(f: A => F[A]) function to do that

    val p = Process.iterateEval(1)(f).take(10)