Reputation: 5023
I am learning scalaz Free monad. I wonder how to can I get Seq[Free[Functor, A]]
to Free[Functor, Seq[A]]
similar like Future.sequence
Here is the code
type Functor[A] = Coyoneda[Command, A]
type Script[A]= Free[Functor, A]
case instruction(...)
def sequence(): Seq[Script[Instruction]] = {...}
I wonder that is that possible that I can flip the result to Script[Seq[Instruction]]
If it can be like Future does, Is that the reason Free is a monad ?
Many thanks in advance
Upvotes: 2
Views: 322
Reputation: 6537
There is the sequence
operation for F[G[A]]
(e.g. Seq[Script[Instruction]]
) whenever there are Traverse[F]
and Applicative[G]
instances. You do have the Applicative[Script]
instance (Script
is a Monad
, so it is also Applicative
), but scalaz
does not provide a Traverse
instance for Seq
. (Immutable Seq
is in principle traversable, but the authors chose not to provide Traverse[Seq]
instance, presumably because Seq
is abstract and hence scalaz cannot guarantee to give you back the same Seq
implementation.)
I will therefore answer for List
instead of Seq
.
import scalaz.std.list._
import scalaz.syntax.traverse._
val scripts: List[Script[A]] = ???
val script: Script[List[A]] = scripts.sequence[Script, A]
Upvotes: 1
Reputation: 684
Free.freeMonad#sequence will help you flip the type constructors in both Cats and Scalaz. Alternative require the monad instance via implicit evidence and use the compiler injected implicit instance instead.
Upvotes: 0