Skip to content

Commit

Permalink
Merge pull request #1526 from tpolecat/inj
Browse files Browse the repository at this point in the history
inject for free programs
  • Loading branch information
adelbertc authored Jan 25, 2017
2 parents be176fa + 40041dd commit d0166d8
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions free/src/main/scala/cats/free/Free.scala
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,24 @@ sealed abstract class Free[S[_], A] extends Product with Serializable {
λ[FunctionK[S, Free[T, ?]]](fa => Suspend(f(fa)))
}(Free.catsFreeMonadForFree)

/**
* Lift into `G` (typically a `Coproduct`) given `Inject`. Analogous
* to `Free.inject` but lifts programs rather than constructors.
*
*{{{
*scala> type Lo[A] = cats.data.Coproduct[List, Option, A]
*defined type alias Lo
*
*scala> val fo = Free.liftF(Option("foo"))
*fo: cats.free.Free[Option,String] = Free(...)
*
*scala> fo.inject[Lo]
*res4: cats.free.Free[Lo,String] = Free(...)
*}}}
*/
final def inject[G[_]](implicit ev: Inject[S, G]): Free[G, A] =
compile(λ[S ~> G](ev.inj(_)))

override def toString: String =
"Free(...)"
}
Expand Down

0 comments on commit d0166d8

Please sign in to comment.