说道FP,我们马上会联想到Monad。我们说过Monad的代表函数flatMap可以把两个运算F[A],F[B]连续起来,这样就可以从程序的意义上形成一种串型的流程(workflow)。更直白的讲法是:任何类型只要实现了flatMap就可以用for-comprehension, for{...}yield。在这个for{...}里我们可以好像OOP一样编写程序。这个for就是一种运算模式,它规范了在for{...}里指令的行为。我们正从OOP风格走入FP编程模式,希望有个最基本的FP编程模式使我们能够沿用OOP编程风格的语法和思维。Monad应该就是最合适的泛函数据类型了。我们先从最基本的开始:假如我们有一段行令程序:
/* val a = e1 val b = e2(a) val c = e3(a,b) val d = e2(c) */
通过这些函数e1,e2,e3最后计算出d值。如果是用FP风格来编这段程序的话,首先我们必须把函数的结果d放入F[d]的F里。F就是上面所说的运算模式,在这里可以用大家熟悉的context(上下文)来表示。F必须是个Monad,F[]相当于for{...}yield。我们先试试用Id,虽然Id[A]对A不做任何处理,直接返回,好像没什么意义,但这种类型具备了map和flatMap,应该可以用for-comprehension:
import scalaz._ import Scalaz._ def e1:Id[Int] = 10 //> e1: => scalaz.Scalaz.Id[Int] def e2(a: Int): Id[Int] = a + 1 //> e2: (a: Int)scalaz.Scalaz.Id[Int] def e3(a: Int, b: Int): Id[Int] = a + b //> e3: (a: Int, b: Int)scalaz.Scalaz.Id[Int] for { a <- e1 b <- e2(a) c <- e3(a,b) d <- e2(c) } yield d //> res0: scalaz.Scalaz.Id[Int] = 22
可以看到,在for-loop里就是OOP的行令程序。不过如果觉着这个Id没什么意义,可以试试Option看:
import scalaz._ import Scalaz._ def e1:Option[Int] = 10.some //> e1: => Option[Int] def e2(a: Int): Option[Int] = (a + 1).some //> e2: (a: Int)Option[Int] def e3(a: Int, b: Int): Option[Int] = (a + b).some//> e3: (a: Int, b: Int)Option[Int] for { a <- e1 b <- e2(a) c <- e3(a,b) d <- e2(c) } yield d //> res0: Option[Int] = Some(22)
看,虽然换了个壳子(context), 但for-loop里的程序没有变化。换一句话讲就是for-loop里的程序根本不理会包裹的context。
Reader也是一种Monad,用它又怎样呢:
import scalaz._ import Scalaz._ def e1:Reader[Int,Int] = Reader[Int,Int](a => a) //> e1: => scalaz.Reader[Int,Int] def e2(a: Int): Reader[Int,Int] = Reader[Int,Int](_ => a + 1) //> e2: (a: Int)scalaz.Reader[Int,Int] def e3(a: Int, b: Int): Reader[Int, Int] = Reader[Int,Int](_ => a+b) //> e3: (a: Int, b: Int)scalaz.Reader[Int,Int] val prg = for { a <- e1 b <- e2(a) c <- e3(a,b) d <- e2(c) } yield d //> prg : scalaz.Kleisli[scalaz.Id.Id,Int,Int] = Kleisli(<function1>) prg.run(10) //> res0: scalaz.Id.Id[Int] = 22
虽然在语法上有些蹩脚,但还是证明了for-loop里的程序是不理会外面context的。那么我们可不可以说这个prg就是一个简单的FP编程语言。它把运算结果放在context里,直至运行了某种interpreter才能取得实际的运算值(用run(10)得到22)。当然,一段程序,它的运算行为受制于单一种类型的context可能有些弱了。如果需要获得一种可用的FP编程语言,我们可能还是要探讨如何把单一类型context组合成多类型混合的context。
我们发现在scalaz里有些type class的名称是以T结束的如:ReaderT,WriterT,StateT等等。这个T指的是变形器Transformer,意思是用它可以堆砌(stacking)context。看看StateT,简单定义应该是这样的:
case class StateT[F[_],S,A](run: S => F[(S,A)])
我们可以把F类堆砌在State上。实践证明如果这个F实现了flatMap,那么堆砌成的类型也能实现flatMap。好,scalaz的Option是实现了flatMap的,那么能不能把它和State堆砌在一起呢?堆砌而成的context会有什么效果呢?我们先看看单一Option和State作为一种context的效果:
for { a <- 3.some b <- (None: Option[Int]) c <- 4.some } yield c //> res1: Option[Int] = None val statePrg = for { a <- get[Int] b <- State[Int,Int](s => (s, s + a)) _ <- put(9) } yield b //> statePrg : scalaz.IndexedStateT[scalaz.Id.Id,Int,Int,Int] = scalaz.IndexedS //| tateT$$anon$10@15ff3e9e statePrg.run(3) //> res2: scalaz.Id.Id[(Int, Int)] = (9,6)
依我来看,Option主要效果是在遇到None值时立即退出。而State的主要作用是在运算同时可以维护一个状态。那么如果把Option和State叠加起来就会同时具备这两种类型的特点了吧?也就是既能维护状态又能在遇到None值时立即终止运算退出了。首先验证一下用Option的flatMap来实现叠加context的flatMap:
case class OptionState[S,A](run: S => Option[(S,A)]) { def map[B](f: A => B): OptionState[S,B] = OptionState { s => run(s) map { case (s1,a1) => (s1,f(a1)) } } def flatMap[B](f: A => OptionState[S,B]): OptionState[S,B] = OptionState { s => run(s) flatMap { case (s1,a1) => f(a1).run(s1) } } }
是的,我们可以用Option的map和flatMap来实现OptionState的map和flatMap。当然,如果我们想在一个for-comprehension里同时使用Option和State就必须把它们升格成OptionState类型:
def liftOption[S,A](oa: Option[A]): OptionState[S,A] = oa match { case Some(a) => OptionState {s => (s,a).some } case None => OptionState {_ => none} } def liftState[S,A](sa: State[S,A]): OptionState[S,A] = OptionState {s => sa(s).some}
现在试试用叠加效果的for-comprehension:
val osprg: OptionState[Int,Int] = for { a <- liftOption(3.some) b <- liftState(put(a)) c <- liftState(get[Int]) d <- liftState(State[Int,Int](s => (s+c, s+a))) } yield c //> osprg : Exercises.rws.OptionState[Int,Int] = OptionState(<function1>) osprg.run(2) //> res3: Option[(Int, Int)] = Some((6,3)) val osprg1: OptionState[Int,Int] = for { a <- liftOption(3.some) b <- liftState(put(a)) _ <- liftOption((None: Option[Int])) c <- liftState(get[Int]) d <- liftState(State[Int,Int](s => (s+c, s+a))) } yield c //> osprg1 : Exercises.rws.OptionState[Int,Int] = OptionState(<function1>) osprg1.run(2) //> res4: Option[(Int, Int)] = None
看,既可以维护状态又具备None处理机制。
好了,scalaz里有个ReaderWriterState这么个type class,就是一个Reader+Writer+State堆砌的Monad。相信scalaz特别提供了这么个type class应该有它的用意。我的猜想是这个Monad是个功能比较完整的组合Monad。作为for-comprehension的context应该能提供比较全面的效果。从字意上解释就是在由它形成的Monadic编程语言里可以同时提供运算(compute)、跟踪(logging)和状态维护功能。它的基础类型是IndexedReaderWriterStateT:scalaz/package.scala
type ReaderWriterStateT[F[_], -R, W, S, A] = IndexedReaderWriterStateT[F, R, W, S, S, A] object ReaderWriterStateT extends ReaderWriterStateTInstances with ReaderWriterStateTFunctions { def apply[F[_], R, W, S, A](f: (R, S) => F[(W, A, S)]): ReaderWriterStateT[F, R, W, S, A] = IndexedReaderWriterStateT[F, R, W, S, S, A] { (r: R, s: S) => f(r, s) } } type IndexedReaderWriterState[-R, W, -S1, S2, A] = IndexedReaderWriterStateT[Id, R, W, S1, S2, A] object IndexedReaderWriterState extends ReaderWriterStateTInstances with ReaderWriterStateTFunctions { def apply[R, W, S1, S2, A](f: (R, S1) => (W, A, S2)): IndexedReaderWriterState[R, W, S1, S2, A] = IndexedReaderWriterStateT[Id, R, W, S1, S2, A] { (r: R, s: S1) => f(r, s) } } type ReaderWriterState[-R, W, S, A] = ReaderWriterStateT[Id, R, W, S, A] object ReaderWriterState extends ReaderWriterStateTInstances with ReaderWriterStateTFunctions { def apply[R, W, S, A](f: (R, S) => (W, A, S)): ReaderWriterState[R, W, S, A] = IndexedReaderWriterStateT[Id, R, W, S, S, A] { (r: R, s: S) => f(r, s) } } type IRWST[F[_], -R, W, -S1, S2, A] = IndexedReaderWriterStateT[F, R, W, S1, S2, A] val IRWST: IndexedReaderWriterStateT.type = IndexedReaderWriterStateT type IRWS[-R, W, -S1, S2, A] = IndexedReaderWriterState[R, W, S1, S2, A] val IRWS: IndexedReaderWriterState.type = IndexedReaderWriterState type RWST[F[_], -R, W, S, A] = ReaderWriterStateT[F, R, W, S, A] val RWST: ReaderWriterStateT.type = ReaderWriterStateT type RWS[-R, W, S, A] = ReaderWriterState[R, W, S, A] val RWS: ReaderWriterState.type = ReaderWriterState
如果把Reader,Writer,State款式分开来对比分析的话:
case class Reader[R, A](f: R => A) //传入R,返回A后不理会R case class Writer[W, A](w: (W, A)) //直接返回W,A case class State[S, A](f: S => (A, S)) //传入S, 返回A和S
那么把以上三个结合起来后它的款式应该是这样的了吧:
case class ReaderWriterState[R, W, S, A]( run: (R, S) => (W, A, S) //传入R,S 返回W,A,S ) case class ReaderWriterStateT[F[_],R, W, S, A]( run: (R, S) => F[(W, A, S)] //传入R,S 返回W,A,S。只是包在了F内 )
/** A monad transformer stack yielding `(R, S1) => F[(W, A, S2)]`. */ sealed abstract class IndexedReaderWriterStateT[F[_], -R, W, -S1, S2, A] { self => def run(r: R, s: S1): F[(W, A, S2)] /** Discards the writer component. */ def state(r: R)(implicit F: Functor[F]): IndexedStateT[F, S1, S2, A] = IndexedStateT((s: S1) => F.map(run(r, s)) { case (w, a, s1) => (s1, a) }) /** Calls `run` using `Monoid[S].zero` as the initial state */ def runZero[S <: S1](r: R)(implicit S: Monoid[S]): F[(W, A, S2)] = run(r, S.zero) /** Run, discard the final state, and return the final value in the context of `F` */ def eval(r: R, s: S1)(implicit F: Functor[F]): F[(W, A)] = F.map(run(r,s)) { case (w,a,s2) => (w,a) } /** Calls `eval` using `Monoid[S].zero` as the initial state */ def evalZero[S <: S1](r:R)(implicit F: Functor[F], S: Monoid[S]): F[(W,A)] = eval(r,S.zero) /** Run, discard the final value, and return the final state in the context of `F` */ def exec(r: R, s: S1)(implicit F: Functor[F]): F[(W,S2)] = F.map(run(r,s)){case (w,a,s2) => (w,s2)} /** Calls `exec` using `Monoid[S].zero` as the initial state */ def execZero[S <: S1](r:R)(implicit F: Functor[F], S: Monoid[S]): F[(W,S2)] = exec(r,S.zero) ...
def map[B](f: A => B)(implicit F: Functor[F]): IndexedStateT[F, S1, S2, B] = IndexedStateT(s => F.map(apply(s)) { case (s1, a) => (s1, f(a)) }) def flatMap[S3, B](f: A => IndexedStateT[F, S2, S3, B])(implicit F: Bind[F]): IndexedStateT[F, S1, S3, B] = IndexedStateT(s => F.bind(apply(s)) { case (s1, a) => f(a)(s1) })
与我们前面所做的OptionState例子一样:如果F能实现map和flatMap则IndexedReaderWriterStateT就能实现map和flatMap。为了省却在for-loop里每行命令都使用lift进行类型升格,IndexedReaderWriterStateT重新实现了大部分操作函数:
private trait ReaderWriterStateTMonad[F[_], R, W, S] extends MonadReader[({type λ[r, α]=ReaderWriterStateT[F, r, W, S, α]})#λ, R] with MonadState[({type f[s, α] = ReaderWriterStateT[F, R, W, s, α]})#f, S] with MonadListen[({type f[w, α] = ReaderWriterStateT[F, R, w, S, α]})#f, W] with IndexedReaderWriterStateTFunctor[F, R, W, S, S] { implicit def F: Monad[F] implicit def W: Monoid[W] def bind[A, B](fa: ReaderWriterStateT[F, R, W, S, A])(f: A => ReaderWriterStateT[F, R, W, S, B]): ReaderWriterStateT[F, R, W, S, B] = fa flatMap f def point[A](a: => A): ReaderWriterStateT[F, R, W, S, A] = ReaderWriterStateT((_, s) => F.point((W.zero, a, s))) def ask: ReaderWriterStateT[F, R, W, S, R] = ReaderWriterStateT((r, s) => F.point((W.zero, r, s))) def local[A](f: R => R)(fa: ReaderWriterStateT[F, R, W, S, A]): ReaderWriterStateT[F, R, W, S, A] = ReaderWriterStateT((r, s) => fa.run(f(r), s)) override def scope[A](k: R)(fa: ReaderWriterStateT[F, R, W, S, A]): ReaderWriterStateT[F, R, W, S, A] = ReaderWriterStateT((_, s) => fa.run(k, s)) override def asks[A](f: R => A): ReaderWriterStateT[F, R, W, S, A] = ReaderWriterStateT((r, s) => F.point((W.zero, f(r), s))) def init: ReaderWriterStateT[F, R, W, S, S] = ReaderWriterStateT((_, s) => F.point((W.zero, s, s))) def get = init def put(s: S): ReaderWriterStateT[F, R, W, S, Unit] = ReaderWriterStateT((r, _) => F.point((W.zero, (), s))) override def modify(f: S => S): ReaderWriterStateT[F, R, W, S, Unit] = ReaderWriterStateT((r, s) => F.point((W.zero, (), f(s)))) override def gets[A](f: S => A): ReaderWriterStateT[F, R, W, S, A] = ReaderWriterStateT((_, s) => F.point((W.zero, f(s), s))) def writer[A](w: W, v: A): ReaderWriterStateT[F, R, W, S, A] = ReaderWriterStateT((_, s) => F.point((w, v, s))) override def tell(w: W): ReaderWriterStateT[F, R, W, S, Unit] = ReaderWriterStateT((_, s) => F.point((w, (), s))) def listen[A](ma: ReaderWriterStateT[F, R, W, S, A]): ReaderWriterStateT[F, R, W, S, (A, W)] = ReaderWriterStateT((r, s) => F.map(ma.run(r, s)) { case (w, a, s1) => (w, (a, w), s1)}) }
我们示范用这个ReaderWriterState来写一段程序:模拟一段通讯端口使用程序并把使用情况记录下来。先传入一个端口号,在程序中可以重设使用的端口号:
val program: ReaderWriterState[Config, List[String], Int, Int] = for { _ <- log("Start - r: %s, s: %s") res <- invokeService _ <- log("Between - r: %s, s: %s") _ <- setService(8,"Com8") _ <- invokeService _ <- log("Done - r: %s, s: %s") } yield res //> program : scalaz.RWS[Exercises.rws.Config,List[String],Int,Int] = scalaz.I //| ndexedReaderWriterStateT$$anon$5@223191a6
这倒像是一段高级语言写的程序。细节都在几个功能函数里。它们都必须返回ReaderWriterState类型:
case class Config(var port: Int, var portName: String) def log[R, S](msg: String): RWS[R, List[String], S, Unit] = ReaderWriterState { case (r, s) => (msg.format(r, s) :: Nil, (), s) //.point[Identity] } //> log: [R, S](msg: String)scalaz.RWS[R,List[String],S,Unit] def invokeService: ReaderWriterState[Config, List[String], Int, Int] = ReaderWriterState { case (cfg, invocationCount) => ( List("Invoking service with port: " + cfg.portName), scala.util.Random.nextInt(100), invocationCount + 1 ) //.point[Identity] } //> invokeService: => scalaz.ReaderWriterState[Exercises.rws.Config,List[String //| ],Int,Int] def setService(p: Int, n: String): ReaderWriterState[Config, List[String], Int, Int] = ReaderWriterState { case (cfg, invocationCount) => cfg.port=p; cfg.portName=n (List("Changing service port to " + cfg.portName), scala.util.Random.nextInt(100), invocationCount) } //> setService: (p: Int, n: String)scalaz.ReaderWriterState[Exercises.rws.Confi //| g,List[String],Int,Int] val program: ReaderWriterState[Config, List[String], Int, Int] = for { _ <- log("Start - r: %s, s: %s") res <- invokeService _ <- log("Between - r: %s, s: %s") _ <- setService(8,"Com8") _ <- invokeService _ <- log("Done - r: %s, s: %s") } yield res //> program : scalaz.RWS[Exercises.rws.Config,List[String],Int,Int] = scalaz.I //| ndexedReaderWriterStateT$$anon$5@223191a6 val r = program run (Config(443,"Com3"), 0) //> r : scalaz.Id.Id[(List[String], Int, Int)] = (List(Start - r: Config(443,C //| om3), s: 0, Invoking service with port: Com3, Between - r: Config(443,Com3) //| , s: 1, Changing service port to Com8, Invoking service with port: Com8, Do //| ne - r: Config(88,Com8), s: 2),68,2) println("Result: " + r._2) //> Result: 68 println("Service invocations: " + r._3) //> Service invocations: 2 println("Log: %n%s".format(r._1.mkString("\t", "%n\t".format(), ""))) //> Log: //| Start - r: Config(443,Com3), s: 0 //| Invoking service with port: Com3 //| Between - r: Config(443,Com3), s: 1 //| Changing service port to Com8 //| Invoking service with port: Com8 //| Done - r: Config(88,Com8), s: 2