为什么 scalacheck Gen 上没有 foldLeft 或 foreach

Why is there no foldLeft or foreach on scalacheck Gen

我在 scala 中有一个不可变的数据结构,其中包含一个对象列表,我想用 scalacheck 对其进行测试。

在此对象上插入 returns 一个新对象。如何在此数据结构中插入多个对象?换句话说,如何为我的数据结构的随机实例编写生成器?

如果类型是 H[A],其中 A 是对象的类型,我尝试做类似的事情:

var heap = empty
arbitrary[A] map (x => heap = insert(x, heap))

但这没有用。

这是一个非常简单、不可变的数据结构,配备了 insert 操作:

final class WrappedList[A] private (underlying: List[A]) {

  def insert[B>:A](elem: B): WrappedList[B] = new WrappedList(elem :: underlying)

  override def toString: String = "WrappedList(" + underlying.mkString(", ") + ")"

}

object WrappedList {

  def empty[A]: WrappedList[A] = new WrappedList(Nil)

}

作为一个简单的例子,让我们看看如何定义 WrappedList[String] 的生成器。首先,为 List[String]:

定义一个生成器
val genStrings: Gen[List[String]] =
    Gen.listOfN[String](10, Gen.oneOf("foo", "bar", "baz", "qux", "quux"))

然后,定义一个 WrappedList[String] 的生成器,它接受 genStrings 生成的字符串列表,并折叠该列表以将每个元素插入最初为空的 WrappedList[String]:

val genWrappedListOfStrings = genStrings
    .flatMap { _.foldRight(WrappedList.empty[String]) { (string, wrappedList) =>
      wrappedList insert string }}

大功告成!

scala> genWrappedListOfStrings.sample.head
res0: WrappedList[String] = WrappedList(qux, bar, bar, baz, quux, foo, qux, qux, foo, qux)

scala> genWrappedListOfStrings.sample.head
res1: WrappedList[String] = WrappedList(qux, foo, qux, baz, qux, qux, quux, quux, qux, quux)

请注意,不需要 var