I have a chain of computations which changes the state of Context:
case class Context(...)
type Step = (Context => Context)
val step1: Step = ctx => { ctx.copy(...) }
val step2: Step
val step3: Step
// ...
val stepN: Step
val chain = List(step1, step2, step3, ..., stepN).toStream
And I would like to introduce the return value of computation to control the flow of that chain:
trait Cont
case class Break[Context](ctx: Context) extends Cont
case class Pause[Context](ctx: Context) extends Cont
case class Continue[Context](ctx: Context) extends Cont
Break - means cancelling chain after executing current step,
Pause - means suspending computation on current step after executing it, with ability to resume the next step later (and check if it's resumable?)
Continue - means normal continuation of the flow.
Each step should return the current value of context wrapped in Cont.
Currently I'm doing that by unfolding the stream of steps (val chain: Stream[Cont[Step]]) and checking the chain.isEmpty and chain.head to check if there's some computation left.
How can I do that using Free monads in scalaz?