TraverseM

Traverse acts as a generator and works with any G[_] for which a cats.Foldable instance is available. The target runtime M[_] requires a MonadCombine[M] instance.

Traverse includes two basic operations fromTraversable and empty.

fromTraversable

fromTraversable allows the lifting of any G[_]: Foldable into the context of FreeS:

import freestyle._
// import freestyle._

import freestyle.implicits._
// import freestyle.implicits._

import freestyle.effects._
// import freestyle.effects._

import cats.implicits._
// import cats.implicits._

val list = traverse[List]
// list: freestyle.effects.traverse.TraverseProvider[List] = freestyle.effects.traverse$TraverseProvider@5b2dbb8e

import list._, list.implicits._
// import list._
// import list.implicits._

def programTraverse[F[_]: TraverseM] =
  for {
    a <- TraverseM[F].fromTraversable(1 :: 2 :: 3 :: Nil)
    b <- (a + 1).pure[FreeS[F, ?]]
  } yield b
// programTraverse: [F[_]](implicit evidence$1: list.TraverseM[F])cats.free.Free[[β$0$]cats.free.FreeApplicative[F,β$0$],Int]

programTraverse[TraverseM.Op].interpret[List]
// res0: List[Int] = List(2, 3, 4)

empty

empty allows the short circuiting of programs providing the empty value for the G[_] through the final MonadCombine. In the same way as OptionM#none, the empty value is determined by how the MonadCombine instance for the final M[_] is implemented.

def programEmpty[F[_]: TraverseM] =
  for {
    _ <- TraverseM[F].empty[Int]
    a <- TraverseM[F].fromTraversable(1 :: 2 :: 3 :: Nil)
    b <- FreeS.pure(a + 1)
    c <- FreeS.pure(b + 1)
  } yield c
// programEmpty: [F[_]](implicit evidence$1: list.TraverseM[F])cats.free.Free[[β$0$]cats.free.FreeApplicative[F,β$0$],Int]

programEmpty[TraverseM.Op].interpret[List]
// res1: List[Int] = List()