Skip to content

Commit

Permalink
adding helper method toFreeT to Free (#2724)
Browse files Browse the repository at this point in the history
* Free should able to convert to FreeT

* refactor

* fix format with sbt freeJVM/scalafmt

* mend test format

* address comments

* no need to specify freet monad instance

* one liner
  • Loading branch information
jcouyang authored and kailuowang committed Feb 27, 2019
1 parent bc54c8a commit 2ad9901
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
5 changes: 4 additions & 1 deletion free/src/main/scala/cats/free/Free.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ sealed abstract class Free[S[_], A] extends Product with Serializable {
final def mapK[T[_]](f: S ~> T): Free[T, A] =
foldMap[Free[T, ?]] { // this is safe because Free is stack safe
λ[FunctionK[S, Free[T, ?]]](fa => Suspend(f(fa)))
}(Free.catsFreeMonadForFree)
}

/**
* Bind the given continuation to the result of this computation.
Expand Down Expand Up @@ -183,6 +183,9 @@ sealed abstract class Free[S[_], A] extends Product with Serializable {
final def inject[G[_]](implicit ev: InjectK[S, G]): Free[G, A] =
mapK(λ[S ~> G](ev.inj(_)))

final def toFreeT[G[_]: Applicative]: FreeT[S, G, A] =
foldMap[FreeT[S, G, ?]](λ[S ~> FreeT[S, G, ?]](FreeT.liftF(_)))

override def toString: String =
"Free(...)"
}
Expand Down
15 changes: 10 additions & 5 deletions free/src/test/scala/cats/free/FreeSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,9 @@ class FreeSuite extends CatsSuite {
fa should ===(Free.pure[Option, Int](n))
}

test("foldMap is stack safe") {
trait FTestApi[A]
case class TB(i: Int) extends FTestApi[Int]

trait FTestApi[A]
case class TB(i: Int) extends FTestApi[Int]
object FTestApi {
type FTest[A] = Free[FTestApi, A]

def tb(i: Int): FTest[Int] = Free.liftF(TB(i))
Expand All @@ -100,8 +99,14 @@ class FreeSuite extends CatsSuite {
def runner: FunctionK[FTestApi, Id] = λ[FunctionK[FTestApi, Id]] {
case TB(i) => i + 1
}
}

test("foldMap is stack safe") {
assert(10000 == FTestApi.a(0).foldMap(FTestApi.runner))
}

assert(10000 == a(0).foldMap(runner))
test("toFreeT is stack-safe") {
FTestApi.a(0).toFreeT[Id].foldMap(FTestApi.runner) should ===(FTestApi.a(0).foldMap(FTestApi.runner))
}

test(".runTailRec") {
Expand Down

0 comments on commit 2ad9901

Please sign in to comment.