# Scalaz（13）－ Monad：Writer - some kind of logger

在上一篇讨论中我们用一个Logger的实现例子示范了如何在flatMap函数实现过程中增加附加作用；一个跟踪功能（logging）,我们在F[T]运算结构中增加了一个String类型值作为跟踪记录（log）。在本篇讨论中我们首先会对上篇的Logger例子进行一些log类型的概括，设计一个新的Logger结构：

case class Logger[LOG, A](log: LOG, value: A) {
def map[B](f: A => B): Logger[LOG,B] = Logger(log, f(value))
def flatMap[B](f: A => Logger[LOG,B])(implicit M: Monoid[LOG]): Logger[LOG,B] = {
val nxLogger = f(value)
Logger(log |+| nxLogger.log, nxLogger.value)
}

}


object Logger {
implicit def toLogger[LOG](implicit M: Monoid[LOG]) = new Monad[({type L[x] = Logger[LOG,x]})#L] {
def point[A](a: => A) = Logger(M.zero,a)
def bind[A,B](la: Logger[LOG,A])(f: A => Logger[LOG,B]): Logger[LOG,B] = la flatMap f
}
}


def enterInt(x: Int): Logger[String, Int] = Logger("Entered Int:"+x, x)
//> enterInt: (x: Int)Exercises.logger.Logger[String,Int]
def enterStr(x: String): Logger[String, String] = Logger("Entered String:"+x, x)
//> enterStr: (x: String)Exercises.logger.Logger[String,String]

for {
a <- enterInt(3)
b <- enterInt(4)
c <- enterStr("Result:")
} yield c + (a * b).shows                         //> res0: Exercises.logger.Logger[String,String] = Logger(Entered Int:3Entered I
//| nt:4Entered String:Result:,Result:12)

final class LoggerOps[A](a: A) {
def applyLog[LOG](log: LOG): Logger[LOG,A] = Logger(log,a)
}
implicit def toLoggerOps[A](a: A) = new LoggerOps[A](a)
//> toLoggerOps: [A](a: A)Exercises.logger.LoggerOps[A]


3.applyLog("Int three")                           //> res1: Exercises.logger.Logger[String,Int] = Logger(Int three,3)
"hi" applyLog "say hi"                            //> res2: Exercises.logger.Logger[String,String] = Logger(say hi,hi)
for {
a <- 3 applyLog "Entered Int 3"
b <- 4 applyLog "Entered Int 4"
c <- "Result:" applyLog "Entered String 'Result'"
} yield c + (a * b).shows                         //> res3: Exercises.logger.Logger[String,String] = Logger(Entered Int 3Entered
//| Int 4Entered String 'Result',Result:12)


for {
a <- 3 applyLog Vector("Entered Int 3")
b <- 4 applyLog Vector("Entered Int 4")
c <- "Result:" applyLog Vector("Entered String 'Result'")
} yield c + (a * b).shows                         //> res4: Exercises.logger.Logger[scala.collection.immutable.Vector[String],Str
//| ing] = Logger(Vector(Entered Int 3, Entered Int 4, Entered String 'Result')
//| ,Result:12)


for {
oa <- 3.some applyLog Vector("Entered Some(3)")
ob <- 4.some applyLog Vector("Entered Some(4)")
} yield ^(oa,ob){_ * _}                           //> res0: Exercises.logger.Logger[scala.collection.immutable.Vector[String],Opti
//| on[Int]] = Logger(Vector(Entered Some(3), Entered Some(4)),Some(12))


def gcd(x: Int, y: Int): Logger[Vector[String], Int] = {
if (y == 0 ) for {
_ <- x applyLog Vector("Finished at " + x)
} yield x
else
x applyLog Vector(x.shows + " mod " + y.shows + " = " + (x % y).shows) >>= {_ => gcd(y, x % y) }

}                                                 //> gcd: (x: Int, y: Int)Exercises.logger.Logger[Vector[String],Int]
gcd(18,6)                                         //> res5: Exercises.logger.Logger[Vector[String],Int] = Logger(Vector(18 mod 6
//| = 0, Finished at 6),6)
gcd(8,3)                                          //> res6: Exercises.logger.Logger[Vector[String],Int] = Logger(Vector(8 mod 3 =
//|  2, 3 mod 2 = 1, 2 mod 1 = 0, Finished at 1),1)


type Writer[+W, +A] = WriterT[Id, W, A]

final case class WriterT[F[_], W, A](run: F[(W, A)]) { self =>
...

WriterT在运算值A之外增加了状态值W，形成一个对值（paired value）。这是一种典型的FP状态维护模式。不过WriterT的这个(W,A)是在运算模型F[]内的。这样可以实现更高层次的概括，为这种状态维护的运算增加多一层运算协议（F[]）影响。我们看到Writer运算是WriterT运算模式的一个特例，它直接计算运算值，不需要F[]影响，所以Writer的F[]采用了Id，因为Id[A] = A。我们看看WriterT是如何通过flatMap来实现状态维护的：scalaz/WriterT.scala:

 def flatMap[B](f: A => WriterT[F, W, B])(implicit F: Bind[F], s: Semigroup[W]): WriterT[F, W, B] =
flatMapF(f.andThen(_.run))

def flatMapF[B](f: A => F[(W, B)])(implicit F: Bind[F], s: Semigroup[W]): WriterT[F, W, B] =
writerT(F.bind(run){wa =>
val z = f(wa._2)
F.map(z)(wb => (s.append(wa._1, wb._1), wb._2))
})


package scalaz
package syntax

final class WriterOps[A](self: A) {
def set[W](w: W): Writer[W, A] = WriterT.writer(w -> self)

def tell: Writer[A, Unit] = WriterT.tell(self)
}

trait ToWriterOps {
implicit def ToWriterOps[A](a: A) = new WriterOps(a)
}


3 set Vector("Entered Int 3")                     //> res2: scalaz.Writer[scala.collection.immutable.Vector[String],Int] = WriterT
//| ((Vector(Entered Int 3),3))
"hi" set Vector("say hi")                         //> res3: scalaz.Writer[scala.collection.immutable.Vector[String],String] = Writ
//| erT((Vector(say hi),hi))
List(1,2,3) set Vector("list 123")                //> res4: scalaz.Writer[scala.collection.immutable.Vector[String],List[Int]] = W
//| riterT((Vector(list 123),List(1, 2, 3)))
3.some set List("some 3")                         //> res5: scalaz.Writer[List[String],Option[Int]] = WriterT((List(some 3),Some(3
//| )))
Vector("just say hi").tell                        //> res6: scalaz.Writer[scala.collection.immutable.Vector[String],Unit] = Writer
//| T((Vector(just say hi),()))


for {
a <- 3 set "Entered Int 3 "
b <- 4 set "Entered Int 4 "
c <- "Result:" set "Entered String 'Result'"
} yield c + (a * b).shows                         //> res7: scalaz.WriterT[scalaz.Id.Id,String,String] = WriterT((Entered Int 3 En
//| tered Int 4 Entered String 'Result',Result:12))


for {
la <- List(1,2,3) set Vector("Entered List(1,2,3)")
lb <- List(4,5) set Vector("Entered List(4,5)")
lc <- List(6) set Vector("Entered List(6)")
} yield (la |@| lb |@| lc) {_ + _ + _}            //> res1: scalaz.WriterT[scalaz.Id.Id,scala.collection.immutable.Vector[String]
//| ,List[Int]] = WriterT((Vector(Entered List(1,2,3), Entered List(4,5), Enter
//| ed List(6)),List(11, 12, 12, 13, 13, 14)))


def gcd(a: Int, b: Int): Writer[Vector[String],Int] =
if (b == 0 ) for {
_ <- Vector("Finished at "+a.shows).tell
} yield a
else
Vector(a.shows+" mod "+b.shows+" = "+(a % b).shows).tell >>= {_ => gcd(b,a % b)}
//> gcd: (a: Int, b: Int)scalaz.Writer[Vector[String],Int]

gcd(8,3)                                          //> res8: scalaz.Writer[Vector[String],Int] = WriterT((Vector(8 mod 3 = 2, 3 mo
//| d 2 = 1, 2 mod 1 = 0, Finished at 1),1))
gcd(16,4)                                         //> res9: scalaz.Writer[Vector[String],Int] = WriterT((Vector(16 mod 4 = 0, Fin
//| ished at 4),4))


def listLogCount(c: Int): Writer[List[String],Unit] = {
@annotation.tailrec
def countDown(c: Int, w: Writer[List[String],Unit]): Writer[List[String],Unit] = c match {
case 0 => w >>= {_ => List("0").tell }
case x => countDown(x-1, w >>= {_ => List(x.shows).tell })
}
val t0 = System.currentTimeMillis
val r = countDown(c,List[String]().tell)
val t1 = System.currentTimeMillis
r >>= {_ => List((t1 -t0).shows+"msec").tell }
}                                                 //> listLogCount: (c: Int)scalaz.Writer[List[String],Unit]
def vectorLogCount(c: Int): Writer[Vector[String],Unit] = {
@annotation.tailrec
def countDown(c: Int, w: Writer[Vector[String],Unit]): Writer[Vector[String],Unit] = c match {
case 0 => w >>= {_ => Vector("0").tell }
case x => countDown(x-1, w >>= {_ => Vector(x.shows).tell })
}
val t0 = System.currentTimeMillis
val r = countDown(c,Vector[String]().tell)
val t1 = System.currentTimeMillis
r >>= {_ => Vector((t1 -t0).shows+"msec").tell }
}                                                 //> vectorLogCount: (c: Int)scalaz.Writer[Vector[String],Unit]

(listLogCount(10000).run)._1.last                 //> res10: String = 361msec
(vectorLogCount(10000).run)._1.last               //> res11: String = 49msec


vectorLogCount(10000)只用了49msec，快了8，9倍呢。

11-07 516

06-20 114

04-16 2036

12-07 1787

12-17 419

03-05 4699

03-29 1.5万

#### 写操作系统基础知识 OS writer

2009年07月24日 683KB 下载