免费Monad的条件行为

时间:2017-01-15 09:41:55

标签: scala free-monad scala-cats

我按照此处的教程: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)
}

3 个答案:

答案 0 :(得分:4)

正如您在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)

formar将根据valueToReturn将值分配给booleanValue。因此,只会解释一个分支。后者将评估这两个值并返回其中一个值,具体取决于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
}

答案 1 :(得分:0)

根据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
}

如果有一个标准的构造来实现withFallback,我很高兴知道它。

答案 2 :(得分:0)

您也可以使用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](...)