为什么scalacheck Gen上没有foldLeft或foreach

时间:2016-08-07 09:57:57

标签: scala scalacheck

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

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

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

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

但这不起作用。

1 个答案:

答案 0 :(得分:1)

这是一个非常简单,不可变的数据结构,配有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

相关问题