gpt4 book ai didi

ScalaCheck 生成 BST

转载 作者:行者123 更新时间:2023-12-03 20:19:24 24 4
gpt4 key购买 nike

我正在尝试使用 ScalaCheck 为 BST 创建一个 Gen,但是当我调用 .sample 方法时,它给了我 java.lang.NullPointerException。我哪里错了?

sealed trait Tree

case class Node(left: Tree, right: Tree, v: Int) extends Tree

case object Leaf extends Tree

import org.scalacheck._
import Gen._
import Arbitrary.arbitrary

case class GenerateBST() {

def genValue(left: Tree, right: Tree): Gen[Int] = (left, right) match {
case (Node(_, _, min), Node(_, _, max)) => arbitrary[Int].suchThat(x => x > min && x < max)
case (Node(_, _, min), Leaf) => arbitrary[Int].suchThat(x => x > min)
case (Leaf, Node(_, _, max)) => arbitrary[Int].suchThat(x => x < max)
case (Leaf, Leaf) => arbitrary[Int]
}


val genNode = for {
left <- genTree
right <- genTree
v <- genValue(left, right)
} yield Node(left, right, v)

def genTree: Gen[Tree] = oneOf(const(Leaf), genNode)
}

GenerateBST().genTree.sample

最佳答案

由于您为递归数据类型递归定义生成器的方式,您需要使用 Gen.lzy在顶部:

def genTree: Gen[Tree] = Gen.lzy(oneOf(const(Leaf), genNode))

作为一个不相关的旁注,使用 suchThat在您的生成器定义中,通常应该只是最后的手段。这意味着 sample经常会失败(大约三分之一的时间是你的代码的固定版本),更重要的是,如果你有一天想要创建导致 Tree 的任意函数,你会看到很多可怕的 org.scalacheck.Gen$RetrievalError: couldn't generate value异常(exception)。

在这种情况下,您可以避免 suchThat使用 Gen.chooseNum 很容易如果它们的顺序错误,请交换左右两侧:
sealed trait Tree
case class Node(left: Tree, right: Tree, v: Int) extends Tree
case object Leaf extends Tree

import org.scalacheck.{ Arbitrary, Gen }

object GenerateBST {
def swapIfNeeded(l: Tree, r: Tree): (Tree, Tree) = (l, r) match {
// If the two trees don't have space between them, we bump one and recheck:
case (Node(_, _, x), n @ Node(_, _, y)) if math.abs(x - y) <= 1 =>
swapIfNeeded(l, n.copy(v = y + 1))
// If the values are in the wrong order, swap:
case (Node(_, _, x), Node(_, _, y)) if x > y => (r, l)
// Otherwise do nothing:
case (_, _) => (l, r)
}

def genValue(left: Tree, right: Tree): Gen[Int] = (left, right) match {
case (Node(_, _, min), Node(_, _, max)) => Gen.chooseNum(min + 1, max - 1)
case (Node(_, _, min), Leaf) => Gen.chooseNum(min + 1, Int.MaxValue)
case (Leaf, Node(_, _, max)) => Gen.chooseNum(Int.MinValue, max - 1)
case (Leaf, Leaf) => Arbitrary.arbitrary[Int]
}

val genNode = for {
l0 <- genTree
r0 <- genTree
(left, right) = swapIfNeeded(l0, r0)
v <- genValue(left, right)
} yield Node(left, right, v)

def genTree: Gen[Tree] = Gen.lzy(Gen.oneOf(Gen.const(Leaf), genNode))
}

现在您可以使用 Arbitrary[Whatever => Tree]无需担心发电机故障:
scala> implicit val arbTree: Arbitrary[Tree] = Arbitrary(GenerateBST.genTree)
arbTree: org.scalacheck.Arbitrary[Tree] = org.scalacheck.ArbitraryLowPriority$$anon$1@606abb0e

scala> val f = Arbitrary.arbitrary[Int => Tree].sample.get
f: Int => Tree = org.scalacheck.GenArities$$Lambda$7109/289518656@13eefeaf

scala> f(1)
res0: Tree = Leaf

scala> f(2)
res1: Tree = Node(Leaf,Leaf,-20313200)

scala> f(3)
res2: Tree = Leaf

scala> f(4)
res3: Tree = Node(Node(Leaf,Leaf,-850041807),Leaf,-1)

关于ScalaCheck 生成 BST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54946728/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com