在对上一次3月份的scala-meetup里我曾分享了关于Future在函数组合中的问题及如何用Monix.Task来替代。具体分析可以查阅这篇博文。在上篇示范里我们使用了Future来实现某种non-blocking数据库操作,现在可以用Task替换Future部分:

  class KVStore[K,V] {
private val kvs = new ConcurrentHashMap[K,V]()
def create(k: K, v: V): Task[Unit] = Task.delay(kvs.putIfAbsent(k,v))
def read(k: K): Task[Option[V]] = Task.delay(Option(kvs.get(k)))
def update(k: K, v: V): Task[Unit] = Task.delay(kvs.put(k,v))
def delete(k: K): Task[Boolean] = Task.delay(kvs.remove(k) != null)
}

Task是一个真正的Monad,我们可以放心的用来实现函数组合:

  type FoodName = String
type Quantity = Int
type FoodStore = KVStore[String,Int] def addFood(food: FoodName, qty: Quantity)(implicit fs: FoodStore): Task[Quantity] = for {
current <- fs.read(food)
newQty = current.map(cq => cq + qty).getOrElse(qty)
_ <- fs.update(food,newQty)
} yield newQty def takeFood(food: FoodName, qty: Quantity)(implicit fs: FoodStore): Task[Quantity] = for {
current <- fs.read(food)
cq = current.getOrElse()
taken = Math.min(cq,qty)
left = cq - taken
_ <- if(left > ) fs.update(food,left) else fs.delete(food)
} yield taken def cookSauce(qty: Quantity)(get: (FoodName,Quantity) => Task[Quantity],
put: (FoodName,Quantity) => Task[Quantity]): Task[Quantity] = for {
tomato <- get("Tomato",qty)
vaggies <- get("Veggies",qty)
_ <- get("Galic",)
sauceQ = tomato/ + vaggies * /
_ <- put("Sauce",sauceQ)
} yield sauceQ def cookPasta(qty: Quantity)(get: (FoodName,Quantity) => Task[Quantity],
put: (FoodName,Quantity) => Task[Quantity]): Task[Quantity] = for {
pasta <- get("Pasta", qty)
sauce <- get("Sauce", qty)
_ <- get("Spice", )
portions = Math.min(pasta, sauce)
_ <- put("Meal", portions)
} yield portions

跟上次我们使用Future时的方式没有两样。值得研究的是如何获取Task运算结果,及如何更精确的控制Task运算如取消运行中的Task:

  implicit val refridge = new FoodStore

  val shopping: Task[Unit] = for {
_ <- addFood("Tomato",)
_ <- addFood("Veggies",)
_ <- addFood("Garlic", )
_ <- addFood("Spice", )
_ <- addFood("Pasta", )
} yield() val cooking: Task[Quantity] = for {
_ <- shopping
sauce <- cookSauce()(takeFood(_,_),addFood(_,_))
meals <- cookPasta()(takeFood(_,_),addFood(_,_))
} yield meals import scala.util._
import monix.execution.Scheduler.Implicits.global val cancellableCooking = Cooking.runOnComplete { result =>
result match {
case Success(meals) => println(s"we have $meals pasta meals for the day.")
case Failure(err) => println(s"cooking trouble: ${err.getMessage}")
}
} global.scheduleOnce( second) {
println(s"its taking too long, cancelling cooking ...")
cancellableCooking.cancel()
}

在上面例子里的addFood,takeFood函数中都有个fs:FoodStore参数。这样做可以使函数更加通用,可以对用不同方式实施的FoodStore进行操作。这里FoodStore就是函数的依赖,我们是通过函数参数来传递这个依赖的。重新组织一下代码使这种关系更明显:

  class Refridge {
def addFood(food: FoodName, qty: Quantity): FoodStore => Task[Quantity] = { foodStore =>
for {
current <- foodStore.read(food)
newQty = current.map(c => c + qty).getOrElse(qty)
_ <- foodStore.update(food, newQty)
} yield newQty
} def takeFood(food: FoodName, qty: Quantity): FoodStore => Task[Quantity] = { foodStore =>
for {
current <- foodStore.read(food)
cq = current.getOrElse()
taken = Math.min(cq, qty)
left = cq - taken
_ <- if (left > ) foodStore.update(food, left) else foodStore.delete(food)
} yield taken
} }

现在我们用一个函数类型的结果来代表依赖注入。这样做的好处是简化了函数主体,彻底把依赖与函数进行了分割,使用函数时不必考虑依赖。

scala的函数式组件库cats提供了一个Kleisli类型,reader monad就是从它推导出来的:

 final case class Kleisli[M[_], A, B](run: A => M[B]) { self =>
...
trait KleisliFunctions {
/**Construct a Kleisli from a Function1 */
def kleisli[M[_], A, B](f: A => M[B]): Kleisli[M, A, B] = Kleisli(f)

def >=>[C](k: Kleisli[M, B, C])(implicit b: Bind[M]): Kleisli[M, A, C] =
kleisli((a: A) => b.bind(this(a))(k.run))

Kleisli的用途就是进行函数的转换
// (A=>M[B]) >=> (B=>M[C]) >=> (C=>M[D]) = M[D]

实际上Kleisli就是ReaderT:

 type ReaderT[F[_], E, A] = Kleisli[F, E, A]
val ReaderT = Kleisli
val reader = ReaderT[F,B,A](A => F[B])
val readerTask = ReaderT[Task,B,A](A => Task[B])
val injection = ReaderT { foodStore => Task.delay { foodStore.takeFood } }
val food = injection.run(db) // run(kvs), run(dbConfig) …

这段代码里我们也针对上面的例子示范了ReaderT的用法。现在我们可以把例子改成下面这样:

  type FoodName = String
type Quantity = Int
type FoodStore = KVStore[String,Int] class Refridge {
def addFood(food: FoodName, qty: Quantity): ReaderT[Task,FoodStore,Quantity] = ReaderT{ foodStore =>
for {
current <- foodStore.read(food)
newQty = current.map(c => c + qty).getOrElse(qty)
_ <- foodStore.update(food, newQty)
} yield newQty
} def takeFood(food: FoodName, qty: Quantity): ReaderT[Task,FoodStore,Quantity] = ReaderT{ foodStore =>
for {
current <- foodStore.read(food)
cq = current.getOrElse()
taken = Math.min(cq, qty)
left = cq - taken
_ <- if (left > ) foodStore.update(food, left) else foodStore.delete(food)
} yield taken
} }

ReaderT[F[_],E,A]就是ReaderT[Task,FoodStore,Quantity]. FoodStore是注入的依赖,ReaderT.run返回Task:

  val cooking: ReaderT[Task,FoodStore,Quantity] = for {
_ <- shopping
sauce <- cooker.cookSauce()
pasta <- cooker.cookPasta()
} yield pasta import scala.concurrent.duration._
import scala.util._
import monix.execution.Scheduler.Implicits.global
val timedCooking = cooking.run(foodStore).timeoutTo( seconds, Task.raiseError( new RuntimeException(
"oh no, take too long to cook ...")))
val cancellableCooking = timedCooking.runOnComplete { result =>
result match {
case Success(meals) => println(s"we have $meals specials for the day.")
case Failure(exception) => println(s"kitchen problem! ${exception.getMessage}")
}
}
global.scheduleOnce( seconds) {
println("3 seconds passed,cancelling ...")
cancellableCooking.cancel()
}

我们知道cooking是个ReaderT,用run(foodStore)来注入依赖foodStore。那么如果我们还有一个kvStore或者jdbcDB,mongoDB可以直接用run(kvStore), run(jdbcDB), run(mongoDB) ... 返回的结果都是Task。

深圳scala-meetup-20180902(2)- Future vs Task and ReaderMonad依赖注入的更多相关文章

  1. dotnet core在Task中使用依赖注入的Service/EFContext

    C#:在Task中使用依赖注入的Service/EFContext dotnet core时代,依赖注入基本已经成为标配了,这就不多说了. 前几天在做某个功能的时候遇到在Task中使用EF DbCon ...

  2. Scala依赖注入

    控制反转(Inversion of Control,简称IoC),是面向对象编程中的一种设计原则,可以用来降低计算机代码之间的耦合程度.其中最常见的方式叫做依赖注入(Dependency Inject ...

  3. asyncio模块中的Future和Task

      task是可以理解为单个coroutine,经过ensure_future方法处理而形成,而众多task所组成的集合经过asyncio.gather处理而形成一个future. 再不精确的粗略的说 ...

  4. 深圳scala-meetup-20180902(1)- Monadic 编程风格

    刚完成了9月份深圳scala-meetup,趁刮台风有空,把我在meetup里的分享在这里发表一下.我这次的分享主要分三个主题:“Monadic编程风格“.”Future vs Task and Re ...

  5. PICE(1):Programming In Clustered Environment - 集群环境内编程模式

    首先声明:标题上的所谓编程模式是我个人考虑在集群环境下跨节点(jvm)的流程控制编程模式,纯粹按实际需要构想,没什么理论支持.在5月份的深圳scala meetup上我分享了有关集群环境下的编程模式思 ...

  6. SDP(13): Scala.Future - far from completion,绝不能用来做甩手掌柜

    在前面几篇关于数据库引擎的讨论里很多的运算函数都返回了scala.Future类型的结果,因为我以为这样就可以很方便的实现了non-blocking效果.无论任何复杂的数据处理操作,只要把它们包在一个 ...

  7. PYTHON ASYNCIO: FUTURE, TASK AND THE EVENT LOOP

    from :http://masnun.com/2015/11/20/python-asyncio-future-task-and-the-event-loop.html Event Loop On ...

  8. 参加完Rocket MQ Meetup深圳站,回顾和想法

    最近一段时间才开始关注云栖社区的公众号,在两周前看到要在深圳科兴科学园办一场Rocket MQ的Meetup.因为从来没有参加过这种线下活动,而且对Rocket MQ比较感兴趣,所以就立即报名参加. ...

  9. Scalaz(44)- concurrency :scalaz Future,尚不完整的多线程类型

    scala已经配备了自身的Future类.我们先举个例子来了解scala Future的具体操作: import scala.concurrent._ import ExecutionContext. ...

随机推荐

  1. C#调用java代码(IKVMC)

    参考资料:https://blog.csdn.net/threadroc/article/details/51406587 参考1:http://www.cnblogs.com/Jack-Blog/p ...

  2. Fiddler使用

    1.下载安装 百度下载后,傻瓜式安装. 2.设置 Tools->options->https->选中"Decrpt HTTPS traffic"(Fiddler就 ...

  3. 一个ipv4到ipv6的移植问题

    之前在使用ipv4的时候,有一个模块是使用raw socket来发包,它使用的一个option是:IP_HDRINCL. 如果设置了IP_HDRINCL选项,则raw会绕过source validat ...

  4. Codeforces Round #437 E. Buy Low Sell High

    题意:买卖股票,给你n个数,你可以选择买进或者卖出或者什么都不做,问你最后获得的最大收益是多少. Examples Input 910 5 4 7 9 12 6 2 10 Output 20 Inpu ...

  5. python的int方法实现数据类型转换

    int方法默认以十进制来实现数据类型的转换: 举例: str1=" #给定的内容最好是纯数字,当然也可以是数字再掺杂点别的,最好别掺杂,因为会报错 print(type(str1),str) ...

  6. Python中使用%还是format来格式化字符串?

    Python中应该使用%还是format来格式化字符串?   %还是format Python中格式化字符串目前有两种阵营:%和format,我们应该选择哪种呢? 自从Python2.6引入了form ...

  7. [leetcode]58. Length of Last Word最后一个词的长度

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...

  8. [leetcode]96. Unique Binary Search Trees给定节点形成不同BST的个数

    Given n, how many structurally unique BST's (binary search trees) that store values 1 ... n? Input: ...

  9. [leetcode]28. Implement strStr()实现strStr()

    Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...

  10. spring 自定义标签的实现

    在我们进行Spring 框架开发中,估计用到最多的就是bean 标签吧,其实在Spring中像<mvc/><context/>这类标签以及在dubbo配置的标签都是属于自定义的 ...