- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在制作一个用于组合重试执行策略的 Monoid,RetryExecutor[T] 是基础类型。我定义了以下基本类型和一个幺半群:
trait RetryExecutor[C] {
def retry[T](f: C => T)(context: C): T
def predicate: Option[Throwable]
def application: Unit
val retryEligible: PartialFunction[Throwable, Boolean]
}
object RetryExecutor {
implicit def retryExecutorMonoid[A] = new Monoid[RetryExecutor[A]] {
...
}
和一些基本类型,如:
case class LinearDelayingRetryExecutor[C](delayInMillis: Long)(val retryEligible: PartialFunction[Throwable, Boolean]) extends RetryExecutor[C] {
override def predicate: Option[Throwable] = None
override def application = Thread.sleep(delayInMillis)
}
case class RetryWithCountExecutor[C](maximumRetries: Int)(val retryEligible: PartialFunction[Throwable, Boolean])(implicit val logger: Logger) extends RetryExecutor[C] {
var remainingTries = maximumRetries + 1
override def application: Unit = {
remainingTries = remainingTries - 1
}
override def predicate: Option[Throwable] = {
if (remainingTries > 0) None
else Some(RetryingException("Retry count of " + maximumRetries + " exceeded for operation"))
}
}
我可以手动组合它们:
val valid: PartialFunction[Throwable, Boolean] = { case x: TestException => true }
val monoid = RetryExecutor.retryExecutorMonoid[Int]
val x = monoid.append(RetryWithCountExecutor[Int](3)(valid), LinearDelayingRetryExecutor(100)(valid))
但是当我尝试使用追加运算符时:
val x = RetryWithCountExecutor[Int](3)(valid) |+| LinearDelayingRetryExecutor(100)(valid)
我得到一个编译错误:
[error] /Users/1000306652a/work/src/test/scala/com/foo/bar/RetryExecutorSpec.scala:25: value |+| is not a member of com.foo.bar.retry.RetryWithCountExecutor[Int]
[error] val k: RetryExecutor[Int] = RetryWithCountExecutor[Int](3)(valid) |+| BackingOffRetryExecutor[Int](100)(valid)
最佳答案
这是一个简单得多的案例中的相同问题:
scala> import scalaz._, Scalaz._
import scalaz._
import Scalaz._
scala> Option(1) |+| Option(2)
res0: Option[Int] = Some(3)
scala> Monoid[Option[Int]].append(Some(1), Some(2))
res1: Option[Int] = Some(3)
scala> Some(1) |+| Some(2)
<console>:14: error: value |+| is not a member of Some[Int]
Some(1) |+| Some(2)
^
问题(这不是真正的问题,更多的是设计决策)是 Monoid
不是协变的——有一个 Monoid[Option[Int]]
并不意味着我有一个 Monoid[Some[Int]]
。
理想的解决方案是为返回值类型作为父类(super class)型的子类型提供构造函数。继续我们的 Option
示例,Scalaz 将这些构造函数提供为 some
和 none
:
scala> some(1) |+| some(2)
res3: Option[Int] = Some(3)
scala> some(1) |+| none
res4: Option[Int] = Some(1)
当然,您也可以显式地向上转换值,但是如果您可以避免使用子类型,那么您的生活将会简单得多。
关于scala - 子类型的 Monoid 应用程序不使用追加运算符编译,但在显式调用时工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26184250/
也许这些陈述都不是绝对精确的,但是单子(monad)通常被定义为“内仿函数类别中的一个幺半群”; Haskell Alternative 被定义为“应用仿函数上的幺半群”,其中 applicative
Applicative 是一个 Monoidal Functor : mappend :: f -> f -> f $ :: (a -> b) -> a ->
假设我有一个函数列表 E => Either[Exception, Unit] 在事件 E 上调用并累积错误以返回 Either[List [异常(exception)],单位]。 type Even
在 Scala 函数式编程一书中,我看到了以下签名: def productMonoid[A,B](A: Monoid[A], B: Monoid[B]): Monoid[(A,B)] 描述是这样说的
从下面的例子中,我认为说 String 在串联操作下定义了一个幺半群是正确的,因为它是一个关联二元操作,而 String 恰好有一个标识元素,它是一个空字符串 "" 。 scala> ("" + "J
Typeclassopedia提出以下练习: Implement pure and () in terms of unit and (**), and vice versa. 这是Monoidal和M
从下面的例子中,我认为说 String 在串联操作下定义了一个幺半群是正确的,因为它是一个关联二元操作,而 String 恰好有一个标识元素,它是一个空字符串 "" 。 scala> ("" + "J
我之前定义了一个函数,它接受一个列表 Maybe s 并将其变成 Maybe一个列表,像这样: floop :: [Maybe a] -> Maybe [a] floop [] = Just [] f
HLearn 的自述文件指出 Monoid 类型类用于并行批量训练。我在几个文件中看到了 trainMonoid ,但我很难剖析这个巨大的代码库。有人可以用初学者友好的术语解释它是如何工作的吗?我猜这
假设我有如下的 Monoid 特征: trait Monoid[A] { def combine(a1: A, a2: A): A def identity: A } 现在如果我想为此写一个
在 haskell IO 类型中有 Monoid 的实例: instance Monoid a => Monoid (IO a) where mempty = pure empty 如果我有三
trait Monoid[A] { def op(a1: A, a2: A): A def zero: A } def mapMergeMonoid[K, V](V: Monoid[V]):
我正在研究这个 Functional Programming in Scala练习: // But what if our list has an element type that doesn't
我试图以更实际的方式理解 monads 和 monoids 之间的关系。如果这个问题没有意义,我提前道歉,我仍在挣扎。 假设例如,我有: trait Monoid[T] { def zero: T
我想写一个方法mergeKeys将值分组在 Iterable[(K, V)] 中由 key 。例如,我可以写: def mergeKeysList[K, V](iter: Iterable[(K,
我正在阅读一本书,内容如下: sealed trait Currency case object USD extends Currency ... other currency types case
我在具有 SceneGraph 类型字段“_scene”的记录上使用下面的代码。我使用 makeLenses 为它创建了镜头。 inputGame :: Input -> Game -> Game i
我有一个数据类型是 Monoid 的实例所以我可以得到很好的值(value)观组合: data R a = R String (Maybe (String → a)) instance Monoid
根据定义或幺半群,二元运算符必须是关联的,例如A op (B op C) == (A op B) op C . base mconcat definition在 haskell 是: mconcat
这个问题在这里已经有了答案: Why MonadPlus and not Monad + Monoid? (4 个回答) 6年前关闭。 我对 Monads 和 Monoids 都很陌生,最近还了解了
我是一名优秀的程序员,十分优秀!