## 动力学知识库

` def composeFunctor[M[_],N[_]](fa: Functor[M], fb: Functor[N]): Functor[({type mn[x] = M[N[x]]})#mn] =new Functor[({type mn[x] = M[N[x]]})#mn] {def map[A,B](fab: M[N[A]])(f: A => B): M[N[B]] =fa.map(fab)(n => fb.map(n)(f))} //> composeFunctor: [M[_], N[_]](fa: scalaz.Functor[M], fb: scalaz.Functor[N])s//| calaz.Functor[[x]M[N[x]]]`

`1 val stringlen: String => Int = _.length //> stringlen : String => Int = <function1>2 val optionInList = List("1".some,"12".some,"123".some)3 //> optionInList : List[Option[String]] = List(Some(1), Some(12), Some(123))45 val mnFunctor = composeFunctor(Functor[List],Functor[Option])6 //> mnFunctor : scalaz.Functor[[x]List[Option[x]]] = Exercises.monadtrans\$\$ano7 //| [email protected]8 mnFunctor.map(optionInList)(stringlen) //> res3: List[Option[Int]] = List(Some(1), Some(2), Some(3))`

` def composeMonad[M[_],N[_]](ma: Monad[M], mb: Monad[N]): Monad[({type mn[x] = M[N[x]]})#mn] =new Monad[({type mn[x] = M[N[x]]})#mn] {def point[A](a: => A) = ma.point(mb.point(a))def bind[A,B](mab: M[N[A]])(f: A => M[N[B]]): M[N[B]] =??? ...}`

`1 type Result[A] = String \/ Option[A]2 val result: Result[Int] = 62.some.right //> result : Exercises.monadtxns.Result[Int] = \/-(Some(62))3 for {4 optionValue <- result5 } yield {6 for {7 valueA <- optionValue8 } yield valueA + 18 //> res0: scalaz.\/[String,Option[Int]] = \/-(Some(80))9 }`

`1 for {2 optionData <- IO3 } yield {4 for {5 data <- optionData6 } yield Process(data)7 }`

`1 type Error[A] = \/[String, A]2 type Result[A] = OptionT[Error, A]34 val result: Result[Int] = 62.point[Result] //> result : Exercises.monadtxns.Result[Int] = OptionT(\/-(Some(62)))5 val transformed =6 for {7 value <- result8 } yield value + 18 //> transformed : scalaz.OptionT[Exercises.monadtxns.Error,Int] = OptionT(\/-(S9 //| ome(80)))`

`type Result[A] = OptionT[({type l[x] = \/[String,x]})#l,A]`

`final case class OptionT[F[_], A](run: F[Option[A]]) {...final case class EitherT[F[_], A, B](run: F[A \/ B]) {...final case class ListT[F[_], A](run: F[List[A]]){...trait IndexedStateT[F[_], -S1, S2, A] { self =>/** Run and return the final value and state in the context of `F` */def apply(initial: S1): F[(S2, A)]`

OptionT[Either,A](run: Either[Option[A]])，这个Either[Option[A]]就是我们的目标类型。而我们在操作时如在for-comprehension中运算时使用的类型则必须统一为OptionT[Either,A]。

`//point升格Applicative[Result].point(62) //> res0: Exercises.monadtxns.Result[Int] = OptionT(\/-(Some(62)))//简写版本62.point[Result] //> res1: Exercises.monadtxns.Result[Int] = OptionT(\/-(Some(62)))//会产生错误结果None.point[Result] //> res2: Exercises.monadtxns.Result[None.type] = OptionT(\/-(Some(None)))"Oh,shit!".left.point[Result] //> res3: Exercises.monadtxns.Result[scalaz.\/[String,Nothing]] = OptionT(\/-(So//| me(-\/(Oh,shit!))))//用构建器OptionT((None: Option[Int]).point[Error]) //> res4: scalaz.OptionT[Exercises.monadtxns.Error,Int] = OptionT(\/-(None))OptionT(none[Int].point[Error]) //> res5: scalaz.OptionT[Exercises.monadtxns.Error,Int] = OptionT(\/-(None))OptionT("Oh,shit!".left: Error[Option[Int]]) //> res6: scalaz.OptionT[Exercises.monadtxns.Error,Int] = OptionT(-\/(Oh,shit!))`

` 1 type Error[A] = \/[String, A] 2 type Result[A] = OptionT[Error, A] 3 4 def getString: Option[String] = "Hello ".some //> getString: => Option[String] 5 def getResult: Error[String] = "how are you!".right 6 //> getResult: => Exercises.monadtxns.Error[String] 7 val prg: Result[String] = for { 8 s1 <- OptionT.optionT(getString.point[Error]) 9 s2 <- "World,".point[Result]10 s3 <- getResult.liftM[OptionT]11 } yield s1+s2+s3 //> prg : Exercises.monadtxns.Result[String] = OptionT(\/-(Some(Hello World,how12 //| are you!)))13 prg.run //> res0: Exercises.monadtxns.Error[Option[String]] = \/-(Some(Hello World,how a14 //| re you!))`

`trait ApplicativeIdV[A] extends Ops[A] {def point(implicit F: Applicative[F]): F[A] = Applicative[F].point(self)...trait OptionTFunctions {def optionT[M[_]] = new (({type λ[α] = M[Option[α]]})#λ ~> ({type λ[α] = OptionT[M, α]})#λ) {def apply[A](a: M[Option[A]]) = new OptionT[M, A](a)}...final class MonadOps[F[_],A] private[syntax](val self: F[A])(implicit val F: Monad[F]) extends Ops[F[A]] {////def liftM[G[_[_], _]](implicit G: MonadTrans[G]): G[F, A] = G.liftM(self)...`

`1 val prg: Result[String] = for {2 s1 <- OptionT.optionT(getString.point[Error])3 s0 <- OptionT(none[String].point[Error])4 s2 <- "World,".point[Result]5 s3 <- getResult.liftM[OptionT]6 } yield s1+s2+s3 //> prg : Exercises.monadtxns.Result[String] = OptionT(\/-(None))7 prg.run //> res0: Exercises.monadtxns.Error[Option[String]] = \/-(None)`

`1 val prg: Result[String] = for {2 s1 <- OptionT.optionT(getString.point[Error])3 s0 <- OptionT("Catch Error!".left: Error[Option[String]])4 s2 <- "World,".point[Result]5 s3 <- getResult.liftM[OptionT]6 } yield s1+s2+s3 //> prg : Exercises.monadtxns.Result[String] = OptionT(-\/(Catch Error!))7 prg.run //> res0: Exercises.monadtxns.Error[Option[String]] = -\/(Catch Error!)`

`type StringEither[A] = String \/ Atype StringEitherT[M[_],A] = EitherT[M,String,A]type IntState[A] = State[Int,A]type IntStateT[M[_],A] = StateT[M,Int,A]type StateEither[A] = StringEitherT[IntState,A]type StateEitherOption[A] = OptionT[StateEither,A]`

`//常量升格val m: StateEitherOption[Int] = 3.point[StateEitherOption]//> m : Exercises.monad_txnfm.StateEitherOption[Int] = OptionT(EitherT(scalaz.p//| [email protected]))//option类升格val o: Option[Int] = 3.some //> o : Option[Int] = Some(3)val o1: StateEither[Option[Int]]= o.point[StateEither]//> o1 : Exercises.monad_txnfm.StateEither[Option[Int]] = EitherT(scalaz.packag//| [email protected])val o2: StateEitherOption[Int] = OptionT.optionT(o1)//> o2 : Exercises.monad_txnfm.StateEitherOption[Int] = OptionT(EitherT(scalaz.//| [email protected]))//val o2: OptionT[StateEither,Int] = OptionT.optionT(o1)//either类升格val e: StringEither[Int] = 3.point[StringEither] //> e : Exercises.monad_txnfm.StringEither[Int] = \/-(3)val e1: IntState[StringEither[Int]] = e.point[IntState]//> e1 : Exercises.monad_txnfm.IntState[Exercises.monad_txnfm.StringEither[Int]//| ] = [email protected]val e2: StateEither[Int] = EitherT.eitherT(e1) //> e2 : Exercises.monad_txnfm.StateEither[Int] = EitherT(scalaz.package\$StateT//| [email protected])//val e2: StringEitherT[IntState,Int] = EitherT.eitherT(e1)val e3: StateEitherOption[Int] = e2.liftM[OptionT]//> e3 : Exercises.monad_txnfm.StateEitherOption[Int] = OptionT(EitherT(scalaz.//| [email protected]))//val e3: OptionT[StateEither,Int] = e2.liftM[OptionT]//state类升格val s: IntState[Int] = get[Int] //> s : Exercises.monad_txnfm.IntState[Int] = [email protected]//| 7db1fval s1: StateEither[Int] = s.liftM[StringEitherT] //> s1 : Exercises.monad_txnfm.StateEither[Int] = EitherT(scalaz.IndexedStateT//| [email protected])//val s1: StringEitherT[IntState,Int] = s.liftM[StringEitherT]val s2: StateEitherOption[Int] = s1.liftM[OptionT]//> s2 : Exercises.monad_txnfm.StateEitherOption[Int] = OptionT(EitherT(scalaz//| [email protected]))//val s2: OptionT[StateEither,Int] = s1.liftM[OptionT]//把State升格成StateTval s3: IntStateT[StringEither,Int] = get[Int].lift[StringEither]//> s3 : Exercises.monad_txnfm.IntStateT[Exercises.monad_txnfm.StringEither,In//| t] = [email protected]`

` def lift[M[_]: Applicative]: IndexedStateT[({type λ[α]=M[F[α]]})#λ, S1, S2, A] = new IndexedStateT[({type λ[α]=M[F[α]]})#λ, S1, S2, A] {def apply(initial: S1): M[F[(S2, A)]] = Applicative[M].point(self(initial))}...trait EitherTFunctions {def eitherT[F[_], A, B](a: F[A \/ B]): EitherT[F, A, B] = EitherT[F, A, B](a)...`

` 1 def getString: Option[String] = "Hello ".some //> getString: => Option[String] 2 def getResult: StringEither[String] = "how are you!".right[String] 3 //> getResult: => Exercises.monad_txnfm.StringEither[String] 4 def modState(s:Int): IntState[Unit] = put(s) //> modState: (s: Int)Exercises.monad_txnfm.IntState[Unit] 5 val prg: StateEitherOption[String] = for { 6 s1 <- OptionT.optionT(getString.point[StateEither]) 7 s2 <- "World,".point[StateEitherOption] 8 s3 <- (EitherT.eitherT(getResult.point[IntState]): StateEither[String]).liftM[OptionT] 9 _ <- (modState(99).liftM[StringEitherT]: StateEither[Unit]).liftM[OptionT]10 } yield s1+s2+s3 //> prg : Exercises.monad_txnfm.StateEitherOption[String] = OptionT(EitherT(sc11 //| [email protected]))12 prg.run //> res0: Exercises.monad_txnfm.StateEither[Option[String]] = EitherT(scalaz.In13 //| [email protected])`

A.point[F[_]] >>> F[A]   "hi".point[Option] = Option[String] = Some("hi")

M[A].liftM[T[_[_],_]] >>> T[M,A]   List(3).liftM[OptionT] = OptionT[List,Int] = OptionT(List(Some(3)))

OptionT.optionT(M[Option[A]]) >>> OptionT[M,A]  OptionT.optionT(List(3.some)) = OptionT[List,Int] = OptionT(List(Some(3)

EitherT.eitherT(M[Either[A]]) >>> EitherT[M,A] EitherT.eitherT(List(3.right[String])) = EitherT(List(\/-(3))

State.lift[M[A]] >>> StateT[M,A]  get[Int].lift[Option] = StateT[Option,Int]