-
-
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.
- Loading branch information
Showing
10 changed files
with
94 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
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,26 @@ | ||
package cats | ||
|
||
/** | ||
* A typeclass which abstracts over monad transformers, providing the | ||
* ability to lift a monad, into the the monad transformer for another | ||
* monad. | ||
*/ | ||
trait MonadTrans[MT[_[_], _]] { | ||
/** | ||
* Lift a value of type M[A] into a monad transformer MT[M, A] | ||
*/ | ||
def liftM[M[_]: Monad, A](ma: M[A]): MT[M, A] | ||
|
||
/** | ||
* Lift a value of type M[A] into a monad transformer MT[M, A] using | ||
* Unapply, this will be useful when the M[A] monad is actually not | ||
* in the * -> * shape. For example Xor[E,A]. | ||
*/ | ||
def liftMU[MA](ma: MA)(implicit U: Unapply[Monad, MA]): MT[U.M, U.A] = { | ||
liftM[U.M, U.A](U.subst(ma))(U.TC) | ||
} | ||
} | ||
|
||
object MonadTrans { | ||
def apply[MT[_[_], _]](implicit MT: MonadTrans[MT]) = MT | ||
} |
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,18 @@ | ||
package cats | ||
package syntax | ||
|
||
trait MonadSyntax1 { | ||
implicit def monadSyntaxU[FA](fa: FA)(implicit U: Unapply[Monad, FA]): Monad.Ops[U.M, U.A] = | ||
new Monad.Ops[U.M, U.A] { | ||
val self = U.subst(fa) | ||
val typeClassInstance = U.TC | ||
} | ||
} | ||
|
||
trait MonadSyntax extends MonadSyntax1 { | ||
implicit def monadSyntax[F[_], A](fa: F[A])(implicit F: Monad[F]): Monad.Ops[F, A] = | ||
new Monad.Ops[F,A] { | ||
val self = fa | ||
val typeClassInstance = F | ||
} | ||
} |
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,20 @@ | ||
package cats | ||
package tests | ||
|
||
import data.{OptionT,XorT,WriterT,StreamingT} | ||
|
||
class MonadTransTests extends CatsSuite { | ||
|
||
test("monadTrans syntax on monad works"){ | ||
|
||
val x: OptionT[List, Int] = List(1).liftM[OptionT] | ||
x.value should === (List(Option(1))) | ||
} | ||
|
||
test("we have monadTrans for XorT, OptionT, StreamingT, WriterT"){ | ||
val a: WriterT[List, Int, Int] = List(1).liftM[({type λ[α[_], β] = WriterT[α, Int, β]})#λ] | ||
val b: StreamingT[List, Int] = List(1).liftM[StreamingT] | ||
val c: OptionT[List, Int] = List(1).liftM[OptionT] | ||
val d: XorT[List, Int, Int] = List(1).liftM[({type λ[α[_], β] = XorT[α, Int, β]})#λ] | ||
} | ||
} |