1
votes

Is there in Scalaz any concept of a "runnable" monad? - one from which the result value can be extracted, potentially by hidden side effects?

trait RunnableM[M[_]] {
  def runM[T](m: M[T]): T
}

I would need this to evaluate a monadic computation and exit from the monad at the end. In Haskell we have different "run" functions for monads which might or might not involve IO, so a typeclass of this sort might not be possible, at least to my thinking. But in Scala it could be done.

1

1 Answers

3
votes

Not a "correct" solution, but scalaz's Comonad has copoint, which can be (ab)used to achieve this; Comonad instances already exist for a lot of standard scalaz monads, and you can cheat and define one for e.g. Task (this may make theorists angry; be sure to limit the scope of any such instance to where you need it and are happy for IO to happen, don't let it infect distant code). It's pimped onto instances:

def runM[T](m: M[T])(implicit cm: Comonad[M]) = m.copoint