繁体   English   中英

免费Monad的条件行为

[英]Conditional Behavior With Free Monads

我在这里遵循该教程: http : //typelevel.org/cats/datatypes/freemonad.html,并尝试对其进行修改以与键值存储区前面的缓存一起使用。 到目前为止,这是我想出的,但是我遇到了valueGetOperation的编译器错误。 我了解为什么会收到编译错误,但我只是不了解如何解决该错误。 使用免费monad时有条件行为的最佳做法是什么?

import cats.data.Coproduct
import cats.free.{Free, Inject}

object KvStore {
  sealed trait KvOp[A]
  case class Get[T](key: String) extends KvOp[Option[T]]
  case class Put[T](key: String, value: T) extends KvOp[Unit]
  case class Delete[T](key: String) extends KvOp[Unit]
}

object CacheStore {
  sealed trait CacheOp[A]
  case class Get[T](key: String) extends CacheOp[Option[T]]
  case class Put[T](key: String, value: T) extends CacheOp[Unit]
  case class Delete[T](key: String) extends CacheOp[Unit]
}

type WriteThruCache[A] = Coproduct[KvStore.KvOp, CacheStore.CacheOp, A]

class KvOps[F[_]](implicit I: Inject[KvStore.KvOp, F]) {
  import KvStore._
  def get[T](key: String): Free[F, Option[T]] = Free.inject[KvOp, F](Get(key))
  def put[T](key: String, value: T): Free[F, Unit] = Free.inject[KvOp, F](Put(key, value))
  def delete[T](key: String): Free[F, Unit] = Free.inject[KvOp, F](Delete(key))
}

object KvOps {
  implicit def kvOps[F[_]](implicit I: Inject[KvStore.KvOp, F]): KvOps[F] = new KvOps[F]
}

class CacheOps[F[_]](implicit I: Inject[CacheStore.CacheOp, F]) {
  import CacheStore._
  def get[T](key: String): Free[F, Option[T]] = Free.inject[CacheOp, F](Get(key))
  def put[T](key: String, value: T): Free[F, Unit] = Free.inject[CacheOp, F](Put(key, value))
  def delete[T](key: String): Free[F, Unit] = Free.inject[CacheOp, F](Delete(key))
}

object CacheOps {
  implicit def cacheOps[F[_]](implicit I: Inject[CacheStore.CacheOp, F]): CacheOps[F] = new CacheOps[F]
}

def valueWriteOperation[T](implicit Kv: KvOps[WriteThruCache], Cache: CacheOps[WriteThruCache]): ((String, T) => Free[WriteThruCache, Unit]) = {
  (key: String, value: T)  =>
    for {
      _ <- Kv.put(key, value)
      _ <- Cache.put(key, value)
    } yield ()
}

// This is where I'm stuck
// desired behavior: If the value isn't in the cache, load it from the kv store and put it in the cache
def valueGetOperation[T](implicit Kv: KvOps[WriteThruCache], Cache: CacheOps[WriteThruCache]): ((String) => Free[WriteThruCache, Option[T]]) = {
  (key: String) =>
    for {
      cacheOption <- Cache.get[T](key)
      kvOption <- Kv.get[T](key) if cacheOption.isEmpty // value withFilter is not a member of cats.free.Free[A$A39.this.WriteThruCache,Option[T]]
    } yield cacheOption.orElse(kvOption)
}

正如你在知道for理解,当你使用if它是由编译器脱调用withFilter方法,如果它不能访问它退到filter方法。 如果未实现它们,您将收到编译器错误。

然而,你可以简单地使用if else

for {
  booleanValue <- myfreeAlbebra.checkCondidtion(arg1, arg2)
  valueToReturn <- if (booleanValue) {
    myfreeAlbebra.someValue
  } else {
    myfreeAlbebra.someOtherValue
  }
} yield valueToReturn

或者,您可以执行以下操作:

for {
  booleanValue  <- myfreeAlbebra.checkCondidtion(arg1, arg2)
  valueToReturnOpt <- myfreeAlbebra.someValue
  fallbackValue <- myfreeAlbebra.someOtherValue
} yield valueToReturnOpt.getOrElse(fallbackValue)

形式将根据booleanValue将值分配给valueToReturn 这样,将仅解释一个分支。 后者将评估两个值并根据valueToReturnOpt是否为空返回其中之一。

我个人会尝试类似的方法:

def valueGetOperation[T](implicit Kv: KvOps[WriteThruCache], Cache: CacheOps[WriteThruCache]): ((String) => Free[WriteThruCache, Option[T]]) = {
  (key: String) =>
    for {
      cacheOption <- Cache.get[T](key)
      returnedValue <- if (cacheOption.isEmpty) Cache.get[T](key) else Kv.get[T](key)
    } yield returnedValue
}

遵循Mateusz的建议,这是我想到的:

def withFallback[A[_], T](loadedValue: Option[T], fallback: => Free[A, Option[T]]): Free[A, Option[T]] = {
  if(loadedValue.isDefined) {
    Free.pure[A, Option[T]](loadedValue)
  } else {
    fallback
  }
}

def valueGetOperation[T](implicit Kv: KvOps[WriteThruCache], Cache: CacheOps[WriteThruCache]): ((String) => Free[WriteThruCache, Option[T]]) = {
  (key: String) =>
    for {
      cachedOption <- Cache.get[T](key)
      actualValue <- withFallback[WriteThruCache, T](cachedOption, fallback = Kv.get[T](key))
    } yield actualValue
}

如果有一个标准构造要用Fallback实现,我很高兴知道这一点。

您也可以使用OptionT#orElse

import cats.data.OptionT

type KV[A] = Free[WriteThruCache, A]

def valueGetOperation[T](
  implicit
  Kv: KvOps[WriteThruCache], 
  Cache: CacheOps[WriteThruCache]
): String => KV[Option[T]] =
  key => OptionT[KV, T](Cache.get[T](key)).orElse(OptionT[KV, T](Kv.get[T](key))).value

OptionT#orElseF

def valueGetOperation[T](
  implicit
  Kv: KvOps[WriteThruCache], 
  Cache: CacheOps[WriteThruCache]
): String => KV[Option[T]] = 
  key => OptionT[KV, T](Cache.get[T](key)).orElseF(Kv.get[T](key)).value

请注意,在Scala 2.12中使用-Ypartial-unification标志,您不需要KV类型别名,并且可以编写OptionT(...)而不是OptionT[KV, T](...)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM