-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This typeclass adds a `liftT` function which is similar to the liftM function on the scalaz MonadTrans typeclass, however this one takes into account that you might not need the power of a monad in order to be able to lift into a transformer, for example, we are able to Lift any M[A] into a Kleisli[M, B, A] regardless of whether or not M is a Monad, Functor, or any other such thing. Thish would be useful in cases where you are constructing an applicative computation using values for which you don't have a monad.
- Loading branch information
Showing
11 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package cats | ||
|
||
|
||
/** | ||
* A typeclass which abstracts over the ability to lift an M[A] into a | ||
* MonadTransformer | ||
*/ | ||
trait TransLift[MT[_[_], _], M[_]] { | ||
/** | ||
* Lift a value of type M[A] into a monad transformer MT[M, A] | ||
*/ | ||
def liftT[A](ma: M[A]): MT[M,A] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package cats | ||
package syntax | ||
|
||
trait TransLiftSyntax { | ||
implicit def transLiftSyntax[M[_], A](ma: M[A]): TransLiftOps[M, A] = new TransLiftOps(ma) | ||
} | ||
|
||
final class TransLiftOps[M[_], A](val ma: M[A]) extends AnyVal { | ||
def liftT[MT[_[_],_]](implicit TL: TransLift[MT, M]): MT[M,A] = TL.liftT(ma) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package cats | ||
package tests | ||
|
||
import data.{OptionT,XorT,WriterT,StreamingT, Kleisli, StateT} | ||
|
||
class TransLiftTests extends CatsSuite { | ||
|
||
case class NoTypeclass[A](a: A) | ||
|
||
case class JustFunctor[A](a: A) | ||
|
||
implicit val jfFunctor: Functor[JustFunctor] = new Functor[JustFunctor] { | ||
override def map[A,B](fa: JustFunctor[A])(f: A => B): JustFunctor[B] = JustFunctor(f(fa.a)) | ||
} | ||
|
||
case class JustAp[A](a: A) | ||
|
||
implicit val jfApp: Applicative[JustAp] = new Applicative[JustAp] { | ||
override def pure[A](a: A): JustAp[A] = JustAp(a) | ||
override def ap[A, B](ff: JustAp[A => B])(fa: JustAp[A]): JustAp[B] = JustAp(ff.a(fa.a)) | ||
override def product[A, B](fa: JustAp[A],fb: JustAp[B]): JustAp[(A, B)] = JustAp(fa.a -> fb.a) | ||
override def map[A, B](fa: JustAp[A])(f: A => B): JustAp[B] = JustAp(f(fa.a)) | ||
} | ||
|
||
test("transLift for XorT, OptionT, WriterT requires only Functor") { | ||
val d: XorT[JustFunctor, Int, Int] = JustFunctor(1).liftT[({type λ[α[_], β] = XorT[α, Int, β]})#λ] | ||
val c: OptionT[JustFunctor, Int] = JustFunctor(1).liftT[OptionT] | ||
val a: WriterT[JustFunctor, Int, Int] = JustFunctor(1).liftT[({type λ[α[_], β] = WriterT[α, Int, β]})#λ] | ||
|
||
} | ||
|
||
test("transLift for StreamingT, StateT require Applicative Functor") { | ||
import StreamingT._ | ||
|
||
val b: StreamingT[JustAp, Int] = JustAp(1).liftT[StreamingT] | ||
val f: StateT[JustAp, Int, Int] = JustAp(1).liftT[({type λ[α[_], β] = StateT[α, Int, β]})#λ] | ||
} | ||
|
||
test("transLift for, Kleisli doesn't require anything of the wrapped value"){ | ||
val e: Kleisli[NoTypeclass, Int, Int] = NoTypeclass(1).liftT[({type λ[α[_], β] = Kleisli[α, Int, β]})#λ] | ||
} | ||
} |