Cats(1)- 从Free开始,Free cats
cats是scala的一个新的函数式编程工具库,其设计原理基本继承了scalaz:大家都是haskell typeclass的scala版实现。当然,cats在scalaz的基础上从实现细节、库组织结构和调用方式上进行了一些优化,所以对用户来说:cats的基础数据类型、数据结构在功能上与scalaz是大致相同的,可能有一些语法上的变化。与scalaz著名抽象、复杂的语法表现形式相比,cats的语法可能更形象、简单直白。在scalaz的学习过程中,我们了解到所谓函数式编程就是monadic Programming:即用monad这样的数据类型来构建程序。而实际可行的monadic programming就是用Free-Monad编程了。因为Free-Monad程序是真正可运行的,或者说是可以实现安全运行的,因为它可以保证在固定的堆栈内实现无限运算。我们知道:函数式编程模式的运行方式以递归算法为主,flatMap函数本身就是一种递归算法。这就预示着monadic programming很容易造成堆栈溢出问题(StackOverflowError)。当我们把普通的泛函类型F[A]升格成Free-Monad后就能充分利用Free-Monad安全运算能力来构建实际可运行的程序了。由于我们在前面已经详细的了解了scalaz的大部分typeclass,包括Free,对cats的讨论就从Free开始,聚焦在cats.Free编程模式方面。同时,我们可以在使用cats.Free的过程中对cats的其它数据类型进行补充了解。
cats.Free的类型款式如下:
sealed abstract class Free[S[_], A] extends Product with Serializable {...}
S是个高阶类,就是一种函数式运算。值得注意的是:现在S不需要是个Functor了。因为Free的一个实例Suspend类型是这样的:
/** Suspend the computation with the given suspension. */
private final case class Suspend[S[_], A](a: S[A]) extends Free[S, A]
我们不需要map就可以把F[A]升格成Free
/**
* Suspend a value within a functor lifting it to a Free.
*/
def liftF[F[_], A](value: F[A]): Free[F, A] = Suspend(value)
我们在scalaz.Free的讨论中并没能详尽地分析在什么情况下S[_]必须是个Functor。下面我们需要用一些篇幅来解析。
Free程序的特点是算式(description)/算法(implementation)关注分离(separation of concern):我们用一组数据类型来模拟一种编程语句ADT(algebraic data type),这一组ADT就形成了一种定制的编程语言DSL(domain specific language)。Free的编程部分就是用DSL来描述程序功能(description of purpose),即算式了。算法即用DSL描述的功能的具体实现,可以有多种的功能实现方式。我们先看个简单的DSL:
import cats.free._
import cats.Functor
object catsFree {
object ADTs {
sealed trait Interact[+A]
object Interact {
case class Ask(prompt: String) extends Interact[String]
case class Tell(msg: String) extends Interact[Unit] def ask(prompt: String): Free[Interact,String] = Free.liftF(Ask(prompt))
def tell(msg: String): Free[Interact,Unit] = Free.liftF(Tell(msg)) implicit object interactFunctor extends Functor[Interact] {
def map[A,B](ia: Interact[A])(f: A => B): Interact[B] = ???
/* ia match {
case Ask(p) => ???
case Tell(m) => ???
} */
}
}
}
object DSLs {
import ADTs._
import Interact._
val prg: Free[Interact,Unit] = for {
first <- ask("What's your first name?")
last <- ask("What's your last name?")
_ <- tell(s"Hello $first $last")
} yield()
}
在这个例子里Interact并不是一个Functor,因为我们无法获取Interact Functor实例的map函数。先让我们分析一下Functor的map:
implicit object interactFunctor extends Functor[Interact] {
def map[A,B](ia: Interact[A])(f: A => B): Interact[B] = ia match {
case Ask(p) => ???
case Tell(m) => ???
}
}
map的作用是用一个函数A => B把F[A]转成F[B]。也就是把语句状态从F[A]转成F[B],但在Interact的情况里F[B]已经是明确的Interact[Unit]和Interact[String]两种状态,而map的f是A => B,在上面的示范里我们该如何施用f来获取这个Interact[B]呢?从上面的示范里我们观察可以得出Ask和Tell这两个ADT纯粹是为了模拟ask和tell这两个函数。ask和tell分别返回Free版本的String,Unit结果。可以说:Interact并没有转换到下一个状态的要求。那么假如我们把ADT调整成下面这样呢:
sealed trait FunInteract[NS]
object FunInteract {
case class FunAsk[NS](prompt: String, onInput: String => NS) extends FunInteract[NS]
case class FunTell[NS](msg: String, ns: NS) extends FunInteract[NS] def funAsk(prompt: String): Free[FunInteract,String] = Free.liftF(FunAsk(prompt,identity))
def funAskInt(prompt: String): Free[FunInteract,Int] = Free.liftF(FunAsk(prompt,_.toInt))
def funTell(msg: String): Free[FunInteract,Unit] = Free.liftF(FunTell(msg,())) implicit object funInteract extends Functor[FunInteract] {
def map[A,NS](fa: FunInteract[A])(f: A => NS) = fa match {
case FunAsk(prompt,input) => FunAsk(prompt,input andThen f)
case FunTell(msg,ns) => FunTell(msg,f(ns))
}
}
}
现在这两个ADT是有类型参数NS的了:FunAsk[NS],FunTell[NS]。NS代表了ADT当前类型,如FunAsk[Int]、FunTell[String]...,现在这两个ADT都通过类型参数NS变成了可map的对象了,如FunAsk[String] >>> FunAsk[String], FunAsk[String] >>> FunAsk[Int]...。所以我们可以很顺利的实现object funInteract的map函数。但是,一个有趣的现象是:为了实现这种状态转换,如果ADT需要返回操作结果,就必须具备一个引领状态转换的机制,如FunAsk类型里的onInput: String => NS:它代表funAsk函数返回的结果可以指向下一个状态。新增函数funAskInt是个很好的示范:通过返回的String结果将状态转换到FunAsk[Int]状态。函数funTell不返回结果,所以FunTell没有状态转换机制。scalaz旧版本Free.Suspend的类型款式是:Suspend[F[Free,A]],这是一个递归类型,内部的Free代表下一个状态。由于我们必须用F.map才能取出下一个状态,所以F必须是个Functor。我们应该注意到如果ADT是Functor的话会造成Free程序的冗余代码。既然cats.Free对F[A]没有设置Functor门槛,那么我们应该尽量避免使用Functor。
得出对ADT类型要求结论后,我们接着示范cats的Free编程。下面是Free程序的功能实现interpret部分(implementation):
import ADTs._
object iconsole extends (Interact ~> Id) {
def apply[A](ia: Interact[A]): Id[A] = ia match {
case Ask(p) => {println(p); readLine}
case Tell(m) => println(m)
}
}
}
DSL程序的功能实现就是把ADT F[A]对应到实际的指令集G[A],在Free编程里用NaturalTransformation ~>来实现。注意G[A]必须是个Monad。在上面的例子里对应关系是:Interact~>Id,代表直接对应到运算指令println和readLine。我们也可以实现另一个版本:
type Prompt = String
type Reply = String
type Message = String
type Tester[A] = Map[Prompt,Reply] => (List[Message],A)
object tester extends (Interact ~> Tester) {
def apply[A](ia: Interact[A]): Tester[A] = ia match {
case Ask(p) => { m => (List(), m(p)) }
case Tell(m) => { _ => (List(m), ()) }
}
}
import cats.Monad
implicit val testerMonad = new Monad[Tester] {
override def pure[A](a: A): Tester[A] = _ => (List(),a)
override def flatMap[A,B](ta: Tester[A])(f: A => Tester[B]): Tester[B] = m => {
val (o1,a1) = ta(m)
val (o2,a2) = f(a1)(m)
(o1 ++ o2, a2)
}
override def tailRecM[A,B](a: A)(f: A => Tester[Either[A,B]]): Tester[B] =
defaultTailRecM(a)(f)
}
}
上面是个模拟测试:我们用个Map[K,V]来模拟互动,K模拟问prompt,V模拟获取回答Input。测试方式是个Function1,输入测试数据Map,在List[Message]里返回所有Tell产生的信息。上面提到过Tester[A]必须是个Monad,所以我们实现了Tester的Monad实例testMonad。实际上 m=>(List,a)就是个writer函数。所谓的Writer就是包嵌一个对值pair(L,V)的Monad,L代表Log,V代表运算值。Writer的特性就是log所有V的运算过程。我们又可以用Writer来实现这个tester:
import cats.data.WriterT
type WF[A] = Map[Prompt,Reply] => A
type WriterTester[A] = WriterT[WF,List[Message],A]
def testerToWriter[A](f: Map[Prompt,Reply] => (List[Message],A)) =
WriterT[WF,List[Message],A](f)
object testWriter extends (Interact ~> WriterTester) {
import Interact._
def apply[A](ia: Interact[A]): WriterTester[A] = ia match {
case Ask(p) => testerToWriter(m => (List(),m(p)))
case Tell(m) => testerToWriter(_ => (List(m),()))
}
}
如果我们用Writer来实现Interact,实际上就是把Ask和Tell都升格成Writer类型。
我们再来看看在cats里是如何运算Free DSL程序的。相对scalaz而言,cats的运算函数简单的多,就一个foldMap,我们来看看它的定义:
/**
* Catamorphism for `Free`.
*
* Run to completion, mapping the suspension with the given
* transformation at each step and accumulating into the monad `M`.
*
* This method uses `tailRecM` to provide stack-safety.
*/
final def foldMap[M[_]](f: FunctionK[S, M])(implicit M: Monad[M], r: RecursiveTailRecM[M]): M[A] =
r.sameType(M).tailRecM(this)(_.step match {
case Pure(a) => M.pure(Right(a))
case Suspend(sa) => M.map(f(sa))(Right(_))
case FlatMapped(c, g) => M.map(c.foldMap(f))(cc => Left(g(cc)))
})
除了要求M是个Monad之外,cats还要求M的RecursiveTailRecM隐式实例。那么什么是RecursiveTailRecM呢:
/**
* This is a marker type that promises that the method
* .tailRecM for this type is stack-safe for arbitrary recursion.
*/
trait RecursiveTailRecM[F[_]] extends Serializable {
/*
* you can call RecursiveTailRecM[F].sameType(Monad[F]).tailRec
* to have a static check that the types agree
* for safer usage of tailRecM
*/
final def sameType[M[_[_]]](m: M[F]): M[F] = m
}
我们用RecursiveTailRecM来保证这个Monad类型与tailRecM是匹配的,这是一种运算安全措施,所以在foldMap函数里r.sameType(M).tailRecM保证了tailRecM不会造成StackOverflowError。cats.Free里还有一种不需要类型安全检验的函数foldMapUnsafe:
/**
* Same as foldMap but without a guarantee of stack safety. If the recursion is shallow
* enough, this will work
*/
final def foldMapUnsafe[M[_]](f: FunctionK[S, M])(implicit M: Monad[M]): M[A] =
foldMap[M](f)(M, RecursiveTailRecM.create)
这个函数不需要RecursiveTailRecM。下面我们选择能保证运算安全的方法来运算tester:首先我们需要Tester类型的Monad和RecursiveTailRecM实例:
import cats.Monad
implicit val testerMonad = new Monad[Tester] with RecursiveTailRecM[Tester]{
override def pure[A](a: A): Tester[A] = _ => (List(),a)
override def flatMap[A,B](ta: Tester[A])(f: A => Tester[B]): Tester[B] = m => {
val (o1,a1) = ta(m)
val (o2,a2) = f(a1)(m)
(o1 ++ o2, a2)
}
override def tailRecM[A,B](a: A)(f: A => Tester[Either[A,B]]): Tester[B] =
defaultTailRecM(a)(f)
}
然后我们制造一些测试数据:
val testData = Map("What's your first name?" -> "Tiger",
"What's your last name?" -> "Chan") //> testData : scala.collection.immutable.Map[String,String] = Map(What's your first name? -> Tiger, What's your last name? -> Chan)
测试运算:
import ADTs._,DSLs._,IMPLs._
val testData = Map("What's your first name?" -> "Tiger",
"What's your last name?" -> "Chan") //> testData : scala.collection.immutable.Map[String,String] = Map(What's your first name? -> Tiger, What's your last name? -> Chan)
val prgRunner = prg.foldMap(tester) //> prgRunner : demo.ws.catsFree.IMPLs.Tester[Unit] = <function1>
prgRunner(testData) //> res0: (List[demo.ws.catsFree.IMPLs.Message], Unit) = (List(Hello Tiger Chan),())
那么如果运算testWriter呢?我们先取得WriterT的Monad实例:
implicit val testWriterMonad = WriterT.catsDataMonadWriterForWriterT[WF,List[Message]]
然后构建一个RecursiveTailRecM实例后再用同样的测试数据来运算:
implicit val testWriterRecT = new RecursiveTailRecM[WriterTester]{}
//> testWriterRecT : cats.RecursiveTailRecM[demo.ws.catsFree.IMPLs.WriterTester] = demo.ws.catsFree$$anonfun$main$1$$anon$2@6093dd95
val prgRunner = prg.foldMap(testWriter) //> prgRunner : demo.ws.catsFree.IMPLs.WriterTester[Unit] = WriterT(<function1>)
prgRunner.run(testData)._1.map(println) //> Hello Tiger Chan
//| res0: List[Unit] = List(())
运算结果一致。
我们再示范一下cats官方文件里关于free monad例子:模拟一个KVStore的put,get,delete功能。ADT设计如下:
object ADTs {
sealed trait KVStoreA[+A]
case class Put[T](key: String, value: T) extends KVStoreA[Unit]
case class Get[T](key: String) extends KVStoreA[Option[T]]
case class Del(key: String) extends KVStoreA[Unit]
}
对应的模拟功能函数设计如下:
type KVStore[A] = Free[KVStoreA,A]
object KVStoreA {
def put[T](key: String, value: T): KVStore[Unit] =
Free.liftF[KVStoreA,Unit](Put[T](key,value))
def get[T](key: String): KVStore[Option[T]] =
Free.liftF[KVStoreA,Option[T]](Get[T](key))
def del(key: String): KVStore[Unit] =
Free.liftF[KVStoreA,Unit](Del(key))
def mod[T](key: String, f: T => T): KVStore[Unit] =
for {
opt <- get[T](key)
_ <- opt.map {t => put[T](key,f(t))}.getOrElse(Free.pure(()))
} yield()
}
注意一下mod函数:它是由基础函数get和put组合而成。我们要求所有在for内的类型为Free[KVStoreA,?],所以当f函数施用后如果opt变成None时就返回结果Free.pure(()),它的类型是:Free[Nothing,Unit],Nothing是KVStoreA的子类。
现在我们可以用这个DSL来编制KVS程序了:
object DSLs {
import ADTs._
import KVStoreA._
def prg: KVStore[Option[Int]] =
for {
_ <- put[Int]("wild-cats", )
_ <- mod[Int]("wild-cats", (_ + ))
_ <- put[Int]("tame-cats", )
n <- get[Int]("wild-cats")
_ <- del("tame-cats")
} yield n
}
我们可以通过State数据结纯代码(pure code)方式来实现用immutable map的KVStore:
object IMPLs {
import ADTs._
import cats.{~>}
import cats.data.State type KVStoreState[A] = State[Map[String, Any], A]
val kvsToState: KVStoreA ~> KVStoreState = new (KVStoreA ~> KVStoreState) {
def apply[A](fa: KVStoreA[A]): KVStoreState[A] =
fa match {
case Put(key, value) => State { (s:Map[String, Any]) =>
(s.updated(key, value),()) }
case Get(key) => State { (s:Map[String, Any]) =>
(s,s.get(key).asInstanceOf[A]) }
case Del(key) => State { (s:Map[String, Any]) =>
(s - key, (())) }
}
}
}
我们把KVStoreA ADT模拟成对State结构的S转换(mutation),返回State{S=>(S,A)}。KVStoreState[A]类型的S参数为immutable.Map[String, Any],所以我们在S转换操作时用immutable map的操作函数来构建新的map返回,典型的pure code。我们来运算一下KVStoreA程序:
import ADTs._,DSLs._,IMPLs._
val prgRunner = prg.foldMap(kvsToState) //> prgRunner : demo.ws.catsFreeKVS.IMPLs.KVStoreState[Option[Int]] = cats.data.StateT@2cfb4a64
prgRunner.run(Map.empty).value //> res0: (Map[String,Any], Option[Int]) = (Map(wild-cats -> 14),Some(14))
但是难道不需要Monad、RecursiveTailRecM实例了吗?实际上cats已经提供了State的Monad和RecursiveTailRecM实例:
import cats.{Monad,RecursiveTailRecM}
implicitly[Monad[KVStoreState]] //> res1: cats.Monad[demo.ws.catsFreeKVS.IMPLs.KVStoreState] = cats.data.StateT Instances$$anon$2@71bbf57e
implicitly[RecursiveTailRecM[KVStoreState]] //> res2: cats.RecursiveTailRecM[demo.ws.catsFreeKVS.IMPLs.KVStoreState] = cats.RecursiveTailRecM$$anon$1@7f13d6e
在cats的StateT.scala里可以找到这段代码:
private[data] sealed trait StateTInstances2 {
implicit def catsDataMonadForStateT[F[_], S](implicit F0: Monad[F]): Monad[StateT[F, S, ?]] =
new StateTMonad[F, S] { implicit def F = F0 } implicit def catsDataRecursiveTailRecMForStateT[F[_]: RecursiveTailRecM, S]: RecursiveTailRecM[StateT[F, S, ?]] = RecursiveTailRecM.create[StateT[F, S, ?]] implicit def catsDataSemigroupKForStateT[F[_], S](implicit F0: Monad[F], G0: SemigroupK[F]): SemigroupK[StateT[F, S, ?]] =
new StateTSemigroupK[F, S] { implicit def F = F0; implicit def G = G0 }
}
我把上面两个示范的源代码提供在下面:
Interact:
import cats.free._
import cats.{Functor, RecursiveTailRecM}
object catsFree {
object ADTs {
sealed trait Interact[+A]
object Interact {
case class Ask(prompt: String) extends Interact[String]
case class Tell(msg: String) extends Interact[Unit] def ask(prompt: String): Free[Interact,String] = Free.liftF(Ask(prompt))
def tell(msg: String): Free[Interact,Unit] = Free.liftF(Tell(msg)) implicit object interactFunctor extends Functor[Interact] {
def map[A,B](ia: Interact[A])(f: A => B): Interact[B] = ???
/* ia match {
case Ask(p) => ???
case Tell(m) => ???
} */
} sealed trait FunInteract[NS]
object FunInteract {
case class FunAsk[NS](prompt: String, onInput: String => NS) extends FunInteract[NS]
case class FunTell[NS](msg: String, ns: NS) extends FunInteract[NS] def funAsk(prompt: String): Free[FunInteract,String] = Free.liftF(FunAsk(prompt,identity))
def funAskInt(prompt: String): Free[FunInteract,Int] = Free.liftF(FunAsk(prompt,_.toInt))
def funTell(msg: String): Free[FunInteract,Unit] = Free.liftF(FunTell(msg,())) implicit object funInteract extends Functor[FunInteract] {
def map[A,NS](fa: FunInteract[A])(f: A => NS) = fa match {
case FunAsk(prompt,input) => FunAsk(prompt,input andThen f)
case FunTell(msg,ns) => FunTell(msg,f(ns))
}
}
}
}
}
object DSLs {
import ADTs._
import Interact._
val prg: Free[Interact,Unit] = for {
first <- ask("What's your first name?")
last <- ask("What's your last name?")
_ <- tell(s"Hello $first $last")
} yield()
}
object IMPLs {
import cats.{Id,~>}
import ADTs._
import Interact._
object iconsole extends (Interact ~> Id) {
def apply[A](ia: Interact[A]): Id[A] = ia match {
case Ask(p) => {println(p); readLine}
case Tell(m) => println(m)
}
} type Prompt = String
type Reply = String
type Message = String
type Tester[A] = Map[Prompt,Reply] => (List[Message],A)
object tester extends (Interact ~> Tester) {
def apply[A](ia: Interact[A]): Tester[A] = ia match {
case Ask(p) => { m => (List(), m(p)) }
case Tell(m) => { _ => (List(m), ()) }
}
}
import cats.Monad
implicit val testerMonad = new Monad[Tester] with RecursiveTailRecM[Tester]{
override def pure[A](a: A): Tester[A] = _ => (List(),a)
override def flatMap[A,B](ta: Tester[A])(f: A => Tester[B]): Tester[B] = m => {
val (o1,a1) = ta(m)
val (o2,a2) = f(a1)(m)
(o1 ++ o2, a2)
}
override def tailRecM[A,B](a: A)(f: A => Tester[Either[A,B]]): Tester[B] =
defaultTailRecM(a)(f)
}
import cats.data.WriterT
import cats.instances.all._
type WF[A] = Map[Prompt,Reply] => A
type WriterTester[A] = WriterT[WF,List[Message],A]
def testerToWriter[A](f: Map[Prompt,Reply] => (List[Message],A)) =
WriterT[WF,List[Message],A](f)
implicit val testWriterMonad = WriterT.catsDataMonadWriterForWriterT[WF,List[Message]]
object testWriter extends (Interact ~> WriterTester) {
import Interact._
def apply[A](ia: Interact[A]): WriterTester[A] = ia match {
case Ask(p) => testerToWriter(m => (List(),m(p)))
case Tell(m) => testerToWriter(_ => (List(m),()))
}
}
} import ADTs._,DSLs._,IMPLs._
val testData = Map("What's your first name?" -> "Tiger",
"What's your last name?" -> "Chan")
//val prgRunner = prg.foldMap(tester)
//prgRunner(testData)
implicit val testWriterRecT = new RecursiveTailRecM[WriterTester]{}
val prgRunner = prg.foldMap(testWriter)
prgRunner.run(testData)._1.map(println)
}
KVStore:
import cats.free._
import cats.instances.all._
object catsFreeKVS {
object ADTs {
sealed trait KVStoreA[+A]
case class Put[T](key: String, value: T) extends KVStoreA[Unit]
case class Get[T](key: String) extends KVStoreA[Option[T]]
case class Del(key: String) extends KVStoreA[Unit]
type KVStore[A] = Free[KVStoreA,A]
object KVStoreA {
def put[T](key: String, value: T): KVStore[Unit] =
Free.liftF[KVStoreA,Unit](Put[T](key,value))
def get[T](key: String): KVStore[Option[T]] =
Free.liftF[KVStoreA,Option[T]](Get[T](key))
def del(key: String): KVStore[Unit] =
Free.liftF[KVStoreA,Unit](Del(key))
def mod[T](key: String, f: T => T): KVStore[Unit] =
for {
opt <- get[T](key)
_ <- opt.map {t => put[T](key,f(t))}.getOrElse(Free.pure(()))
} yield()
}
}
object DSLs {
import ADTs._
import KVStoreA._
def prg: KVStore[Option[Int]] =
for {
_ <- put[Int]("wild-cats", )
_ <- mod[Int]("wild-cats", (_ + ))
_ <- put[Int]("tame-cats", )
n <- get[Int]("wild-cats")
_ <- del("tame-cats")
} yield n
}
object IMPLs {
import ADTs._
import cats.{~>}
import cats.data.State type KVStoreState[A] = State[Map[String, Any], A]
val kvsToState: KVStoreA ~> KVStoreState = new (KVStoreA ~> KVStoreState) {
def apply[A](fa: KVStoreA[A]): KVStoreState[A] =
fa match {
case Put(key, value) => State { (s:Map[String, Any]) =>
(s.updated(key, value),()) }
case Get(key) => State { (s:Map[String, Any]) =>
(s,s.get(key).asInstanceOf[A]) }
case Del(key) => State { (s:Map[String, Any]) =>
(s - key, (())) }
}
}
}
import ADTs._,DSLs._,IMPLs._
val prgRunner = prg.foldMap(kvsToState)
prgRunner.run(Map.empty).value import cats.{Monad,RecursiveTailRecM}
implicitly[Monad[KVStoreState]]
implicitly[RecursiveTailRecM[KVStoreState]]
}
Cats(1)- 从Free开始,Free cats的更多相关文章
- [POJ 3735] Training little cats (结构矩阵、矩阵高速功率)
Training little cats Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9613 Accepted: 2 ...
- Training little cats poj3735
Training little cats Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9299 Accepted: 2 ...
- Training little cats(poj3735,矩阵快速幂)
Training little cats Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10737 Accepted: ...
- POJ 3735 Training little cats<矩阵快速幂/稀疏矩阵的优化>
Training little cats Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13488 Accepted: ...
- POJ 3735 Training little cats(矩阵快速幂)
Training little cats Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11787 Accepted: 2892 ...
- POJ 3735:Training little cats 联想到矩阵相乘
Training little cats Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11208 Accepted: ...
- poj3757 Training little cats
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11496 Accepted: 2815 Description Face ...
- Cats(4)- 叠加Free程序运算结果,Stacking monadic result types
在前面的几篇关于Free编程的讨论示范中我们均使用了基础类型的运算结果.但在实际应用中因为需要考虑运算中出现异常的情况,常常会需要到更高阶复杂的运算结果类型如Option.Xor等.因为Monad无法 ...
- Cats(3)- freeK-Free编程更轻松,Free programming with freeK
在上一节我们讨论了通过Coproduct来实现DSL组合:用一些功能简单的基础DSL组合成符合大型多复杂功能应用的DSL.但是我们发现:cats在处理多层递归Coproduct结构时会出现编译问题.再 ...
随机推荐
- 《Entity Framework 6 Recipes》翻译系列 (3) -----第二章 实体数据建模基础之创建一个简单的模型
第二章 实体数据建模基础 很有可能,你才开始探索实体框架,你可能会问“我们怎么开始?”,如果你真是这样的话,那么本章就是一个很好的开始.如果不是,你已经建模,并在实体分裂和继承方面感觉良好,那么你可以 ...
- 上传伪技术~很多人都以为判断了后缀,判断了ContentType,判断了头文件就真的安全了。是吗?
今天群里有人聊图片上传,简单说下自己的经验(大牛勿喷) 0.如果你的方法里面是有指定路径的,记得一定要过滤../,比如你把 aa文件夹设置了权限,一些类似于exe,asp,php之类的文件不能执行,那 ...
- 移动web app开发必备 - 异步队列 Deferred
背景 移动web app开发,异步代码是时常的事,比如有常见的异步操作: Ajax(XMLHttpRequest) Image Tag,Script Tag,iframe(原理类似) setTimeo ...
- jQuery动画的实现
没有引入deferred机制,其余流程都有了 //////////// //创建动画缓动对象 // //////////// function Tween(value, prop, animation ...
- .NET平台开源项目速览(2)Compare .NET Objects对象比较组件
.NET平台开源项目速览今天介绍一款小巧强大的对象比较组件.可以更详细的获取2个对象的差别,并记录具体差别,比较过程和要求可以灵活配置. .NET开源目录:[目录]本博客其他.NET开源项目文章目录 ...
- 连接 insance 到 vlan101 - 每天5分钟玩转 OpenStack(97)
前面我们创建了 vlan101,今天继续部署 instance 到该 vlan network, 并讨论 instance 之间的连通性. launch 新的 instance "cirro ...
- Web APi入门之移除XML格式(一)
前言 回头想来,没想到自己却坚持下来了,EntityFramework系列终于全部完成了,给自己点个赞先.本系列将着手于Web API,关于一些基础的介绍及定义就不再叙述,请参考园友们文章,非常详细, ...
- android标题栏上面弹出提示框(二) PopupWindow实现,带动画效果
需求:上次用TextView写了一个从标题栏下面弹出的提示框.android标题栏下面弹出提示框(一) TextView实现,带动画效果, 总在找事情做的产品经理又提出了奇葩的需求.之前在通知栏显示 ...
- MySQL学习笔记五:数据类型
MySQL支持多种数据类型,大致可以分为数值,日期/时间和字符类型. 数值类型 MySQL支持所有标准SQL数值数据类型,包括严格数值数据类型(INTEGER.SMALLINT.DECIMAL和NUM ...
- 出操队形(LIS)
题目来源:微策略2013年校园招聘面试一面试题 题目描述: 在读高中的时候,每天早上学校都要组织全校的师生进行跑步来锻炼身体,每当出操令吹响时,大家就开始往楼下跑了,然后身高矮的排在队伍的前面,身高较 ...